question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

🐛 Bug

  File "C:\Users\temp\Aida\aida\agents\bertbot\Bert\bert_intent_classifier_pytorch.py", line 298, in process
    logits = self.model(prediction_inputs, token_type_ids=None, attention_mask=prediction_masks)
  File "C:\Users\temp\Anaconda3\envs\fresh\lib\site-packages\torch\nn\modules\module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "C:\Users\temp\Anaconda3\envs\fresh\lib\site-packages\transformers\modeling_bert.py", line 897, in forward
    head_mask=head_mask)
  File "C:\Users\temp\Anaconda3\envs\fresh\lib\site-packages\torch\nn\modules\module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "C:\Users\temp\Anaconda3\envs\fresh\lib\site-packages\transformers\modeling_bert.py", line 624, in forward
    embedding_output = self.embeddings(input_ids, position_ids=position_ids, token_type_ids=token_type_ids)
  File "C:\Users\temp\Anaconda3\envs\fresh\lib\site-packages\torch\nn\modules\module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "C:\Users\temp\Anaconda3\envs\fresh\lib\site-packages\transformers\modeling_bert.py", line 167, in forward
    words_embeddings = self.word_embeddings(input_ids)
  File "C:\Users\temp\Anaconda3\envs\fresh\lib\site-packages\torch\nn\modules\module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "C:\Users\temp\Anaconda3\envs\fresh\lib\site-packages\torch\nn\modules\sparse.py", line 114, in forward
    self.norm_type, self.scale_grad_by_freq, self.sparse)
  File "C:\Users\temp\Anaconda3\envs\fresh\lib\site-packages\torch\nn\functional.py", line 1484, in embedding
    return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
RuntimeError: Expected tensor for argument #1 'indices' to have scalar type Long; but got torch.cuda.IntTensor instead (while checking arguments for embedding)

Issue

Hi everyone when I run the line:

outputs = model(input_ids = b_input_ids, attention_mask=b_input_mask, labels=b_labels)

with model defined as,

model = BertForSequenceClassification.from_pretrained("bert-base-uncased", num_labels=numlabels)

It returns the stated error. However this only happens when I am on my windows computer. When I run the exact same code with the same python version and libraries it works perfectly fine. I have the most up to date version of pytorch (1.4) and transformers installed.

Any help would be greatly appreciated

Information

Using the latest version of pytorch and transformers Model I am using (Bert, XLNet …): BertForSequenceClassification Language I am using the model on (English, Chinese …): English

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

19reactions
chintanckgcommented, May 1, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Had similar issue: Young Sheldon’s solution on below stackoverflow thread worked well.

https://stackoverflow.com/questions/56360644/pytorch-runtimeerror-expected-tensor-for-argument-1-indices-to-have-scalar-t

5reactions
aryamansriramcommented, Jun 17, 2020

Having the same issue, funny thing is the whole model worked for training, but while running inference on test data the error automatically showed up

Read more comments on GitHub >

github_iconTop Results From Across the Web

Expected tensor for argument #1 'indices' to have scalar type ...
Runtime Error : Expected tensor for argument #1 'indices' to have scalar type Long; but got CUDAType instead (while checking arguments for ...
Read more >
Expected tensor for argument #1 'indices' to ... - PyTorch Forums
Expected tensor for argument #1 'indices' to have scalar type Long; but got CPUFloatTensor instead (while checking arguments for embedding).
Read more >
Expected tensor for argument #1 'indices' to have scalar type ...
I am trying to re-execute a GitHub project on my computer for recommendation using embedding, the goal is to first embed the user...
Read more >
"Expected tensor for argument #1 'indices' to have scalar type ...
Error message: RuntimeError: Expected tensor for argument #1 'indices' to have scalar type Long; but got torch.FloatTensor instead (while ...
Read more >
runtimeerror: expected scalar type long but found float - You.com
PyTorch essentially defines nine CPU tensor types and nine GPU tensor types: ... IntTensor ║ ║ 64-bit integer (signed) ║ torch.int64 or torch.long...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found