Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Summary with embeddings #42

Open
siddBanPsu opened this issue Oct 17, 2018 · 12 comments
Open

Summary with embeddings #42

siddBanPsu opened this issue Oct 17, 2018 · 12 comments

Comments

@siddBanPsu
Copy link

class TextCNN(nn.Module):
    def __init__(self, nb_words, embed_dim, embedding_matrix, max_seq_len, num_filters, num_classes):
        super(TextCNN, self).__init__()
        self.num_filters=num_filters
        self.embed = nn.Embedding(nb_words, embed_dim)
        self.dropout = nn.Dropout(0.3)
        self.conv = nn.Conv1d(embed_dim, num_filters, kernel_size=2, stride=1)
        self.fc1 = nn.Linear(num_filters, 32)
        self.fc2 = nn.Linear(32, num_classes)
        self.logsigmoid = nn.Sigmoid()

    def forward(self, x):
        x = self.embed(x)
        x = x.permute(0, 2, 1)
        x = self.dropout(x)
        x = self.conv(x).permute(0, 2, 1).max(1)[0]
        x = self.fc1(x)
        x = F.relu(x)
        x = self.dropout(x)
        x = self.fc2(x)
        return self.logsigmoid(x)
model = TextCNN(1000, 100, [], 10, 20, 3)
data = torch.from_numpy(np.array([[1,4,5], [7,7,9]]))
output = model(data)
print(output)
print(model)
print(data[0].shape)
print(summary(model, (1, 10)))

Gives error:

RuntimeError: Expected tensor for argument #1 'indices' to have scalar type Long; but got CPUFloatTensor instead (while checking arguments for embedding)

@meshiguge
Copy link

same problem

@BerenLuthien
Copy link

BerenLuthien commented Jan 4, 2019

RuntimeErrorTraceback (most recent call last)
<ipython-input-243-70bbb59e2bd0> in <module>()
     26 print(model)
     27 print(data[0].shape)
---> 28 print(summary(model, (1, 10)))
...
RuntimeError: Expected tensor for argument #1 'indices' to have scalar type Long; but got CUDAFloatTensor instead (while checking arguments for embedding)

@LittleStars666
Copy link

I also encountered this problem. Has anyone solved it?

@alexandru-dinu
Copy link

alexandru-dinu commented Mar 18, 2019

One quick-fix is to change the dtype from FloatTensor to LongTensor, in the source file where the package is installed:

if device == "cuda" and torch.cuda.is_available():
    # dtype = torch.cuda.FloatTensor
    dtype = torch.cuda.LongTensor
else:
    # dtype = torch.FloatTensor
    dtype = torch.LongTensor

@alfaijmansuri
Copy link

same problem and also changing the data types from floattensor to long is not workng

@alexandru-dinu
Copy link

Not working is not satisfactory. What is the error message?

@wassimseif
Copy link

Hey I'm having the same issue

class BiLSTMBaseline(nn.Module):
    
    def __init__(self,hidden_dim,emb_dim=300,
                recurrent_dropout=0.1,num_linear=1):
        super().__init__()
        self.embedding = nn.Embedding(len(TEXT.vocab),emb_dim)
        self.encoder = nn.LSTM(emb_dim,hidden_dim,num_linear,dropout = recurrent_dropout)
        self.linear_layers = []
        for _ in range(num_linear - 1):
            self.linear_layers.append(nn.Linear(hidden_dim,hidden_dim))
        self.linear_layers = nn.ModuleList(self.linear_layers)
        self.predictor =   nn.Linear(hidden_dim,6)
    
    def forward(self,seq):
        embeddings = self.embedding(seq)
        hdn, _  = self.encoder(embeddings)
        feature = hdn[-1,:,:]
        for layer in self.linear_layers:
            feature = layer(feature)
        preds = self.predictor(feature)
        return preds
        
summary(model,input_size=(402, 64),device='cpu')

Error :

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-44-ad1f81b1c254> in <module>
----> 1 summary(model,input_size=(402, 64),device='cpu')

<ipython-input-43-4e60852cb827> in summary(model, input_size, batch_size, device)
     72 #     print(x.shape)
     73 
---> 74     model(*x)
     75 
     76     # remove these hooks

~/anaconda3/envs/sent-analy/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    487             result = self._slow_forward(*input, **kwargs)
    488         else:
--> 489             result = self.forward(*input, **kwargs)
    490         for hook in self._forward_hooks.values():
    491             hook_result = hook(self, input, result)

<ipython-input-12-e2f879788e90> in forward(self, seq)
     13 
     14     def forward(self,seq):
---> 15         embeddings = self.embedding(seq)
     16         hdn, _  = self.encoder(embeddings)
     17         feature = hdn[-1,:,:]

~/anaconda3/envs/sent-analy/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    487             result = self._slow_forward(*input, **kwargs)
    488         else:
--> 489             result = self.forward(*input, **kwargs)
    490         for hook in self._forward_hooks.values():
    491             hook_result = hook(self, input, result)

~/anaconda3/envs/sent-analy/lib/python3.6/site-packages/torch/nn/modules/sparse.py in forward(self, input)
    116         return F.embedding(
    117             input, self.weight, self.padding_idx, self.max_norm,
--> 118             self.norm_type, self.scale_grad_by_freq, self.sparse)
    119 
    120     def extra_repr(self):

~/anaconda3/envs/sent-analy/lib/python3.6/site-packages/torch/nn/functional.py in embedding(input, weight, padding_idx, max_norm, norm_type, scale_grad_by_freq, sparse)
   1452         # remove once script supports set_grad_enabled
   1453         _no_grad_embedding_renorm_(weight, input, max_norm, norm_type)
-> 1454     return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
   1455 
   1456 

RuntimeError: Expected tensor for argument #1 'indices' to have scalar type Long; but got torch.FloatTensor instead (while checking arguments for embedding)

@ruathudo
Copy link

ruathudo commented Feb 6, 2020

Is there any fix for this? I'm still getting this error.

@RaffaeleGalliera
Copy link

One quick-fix is to change the dtype from FloatTensor to LongTensor, in the source file where the package is installed:

    if device == "cuda" and torch.cuda.is_available():
        # dtype = torch.cuda.FloatTensor
        dtype = torch.cuda.LongTensor
    else:
        # dtype = torch.FloatTensor
        dtype = torch.LongTensor

I was having the same issue and this quick fix worked for me

@turbolt
Copy link

turbolt commented Jun 2, 2020

Hey @siddBanPsu, thanks for starting this thread - I was wondering if you've fixed your issue - if yes, do you mind sharing how you fix it? thanks a lot!

@nikhilkarnwal
Copy link

Hi, I also encountered same issue and as a hack, I changed the data type of tensor in the forward method where I am calling embedding module to long just for testing purpose. It worked for me.

@santteegt
Copy link

to solve this I used the torchsummaryX package that allows you to specify a sample input tensor:

!pip install torchsummaryX

Example:

from torchsummaryX import summary as summaryx

# sample input tensor
input_size = (1, 80, 108)
x_sample = torch.zeros(input_size, dtype=torch.long, device=torch.device('cuda'))

print(summaryx(model, x_sample))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests