Expected object of type torch.cuda.LongTensor but found type torch.LongTensor for argument #3 'index'


(Dev Sharma) #1

Hey guys!
Need some help with debugging the code. I am trying to train an LSTM to classify if the email will be replied or not after reading the email.

I’ve written this LSTM and getting this error.
The code is here: https://github.com/clairett/pytorch-sentiment-classification/blob/master/train_batch.py?fbclid=IwAR0Vs1QxGyvZy3kjt2qbi8KiDsySTK3vzlvp3IpaODnG5264W6MOk88YURI

Can you please help?
error is

RuntimeError                              Traceback (most recent call last)
<ipython-input-11-877900cdf097> in <module>()
    208     os.makedirs(out_dir)
    209 for epoch in range(EPOCHS):
--> 210     avg_loss, acc = train_epoch_progress(model, train_iter, loss_function, optimizer, text_field, label_field, epoch)
    211     tqdm.write('Train: loss %.2f acc %.1f' % (avg_loss, acc*100))
    212     dev_acc = evaluate(model, dev_iter, loss_function, 'Dev')

<ipython-input-11-877900cdf097> in train_epoch_progress(model, train_iter, loss_function, optimizer, text_field, label_field, epoch)
     66         model.batch_size = len(label.data)
     67         model.hidden = model.init_hidden()
---> 68         pred = model(sent).cuda()
     69         pred_label = pred.data.max(1)[1].numpy()
     70         pred_res += [x for x in pred_label]

C:\Python\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    475             result = self._slow_forward(*input, **kwargs)
    476         else:
--> 477             result = self.forward(*input, **kwargs)
    478         for hook in self._forward_hooks.values():
    479             hook_result = hook(self, input, result)

<ipython-input-10-6b61a008efbf> in forward(self, sentence)
     21 
     22     def forward(self, sentence):
---> 23         x = self.embeddings(sentence).view(len(sentence), self.batch_size, -1).type(torch.cuda.LongTensor)
     24         lstm_out, self.hidden = self.lstm(x, self.hidden)
     25         y = self.hidden2label(lstm_out[-1])

C:\Python\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    475             result = self._slow_forward(*input, **kwargs)
    476         else:
--> 477             result = self.forward(*input, **kwargs)
    478         for hook in self._forward_hooks.values():
    479             hook_result = hook(self, input, result)

C:\Python\lib\site-packages\torch\nn\modules\sparse.py in forward(self, input)
    108         return F.embedding(
    109             input, self.weight, self.padding_idx, self.max_norm,
--> 110             self.norm_type, self.scale_grad_by_freq, self.sparse)
    111 
    112     def extra_repr(self):

C:\Python\lib\site-packages\torch\nn\functional.py in embedding(input, weight, padding_idx, max_norm, norm_type, scale_grad_by_freq, sparse)
   1108         with torch.no_grad():
   1109             torch.embedding_renorm_(weight, input, max_norm, norm_type)
-> 1110     return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
   1111 
   1112 

RuntimeError: Expected object of type torch.cuda.LongTensor but found type torch.LongTensor for argument #3 'index'


(Arul) #2

Normally

This error occurs when you do not have all the data in GPU (for example, sent).
Try to send the data to GPU (sent = sent.cuda()) and try again.