HiLAP icon indicating copy to clipboard operation
HiLAP copied to clipboard

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

Open Lydiasc34 opened this issue 4 years ago • 4 comments

Hi, thank you for sharing the rcv data source and I managed to request data and preprocessed it. I used my preprocessed rcv text and its id as X_train/X_test, train_id/text_id in readData_rcv1. I chose TextCNN as base model and no pkl file exists so I started from the training.

And when processing data in load_data_rcv1, I noticed X_train = [[[word_index[word] for word in sent] for sent in doc] for doc in X_train]. So the type for X_train is list. Then the script works fine until it comes to the train_taxo() and below is the error I got.

(For the mode in config, I set it as hilap. And I use gpu to run.)

0it [00:00, ?it/s]
Traceback (most recent call last):
  File "C:/Users/HiLAP/main.py", line 816, in <module>
    train_taxo()
  File "C:/HiLAP/main.py", line 277, in train_taxo
    _, flat_probs = forward_step_sl(tokens, doc_ids, flat_probs_only=(args.global_ratio == 1))
  File "C:/HiLAP/main.py", line 134, in forward_step_sl
    probs = policy.base_model(tokens, True)
  File "C:\Local\Continuum\anaconda3\envs\HiLAP\lib\site-packages\torch\nn\modules\module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "C:\HiLAP\TextCNN.py", line 36, in forward
    x = self.embed(x)  # (N, W, D)
  File "C:\Local\Continuum\anaconda3\envs\HiLAP\lib\site-packages\torch\nn\modules\module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "C:\Local\Continuum\anaconda3\envs\HiLAP\lib\site-packages\torch\nn\modules\sparse.py", line 126, in forward
    self.norm_type, self.scale_grad_by_freq, self.sparse)
  File "C:\Local\Continuum\anaconda3\envs\HiLAP\lib\site-packages\torch\nn\functional.py", line 1852, 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)

It seems like the error is about input type. So I was trying to convert the tokens input to LongTensor but it keeps getting me errors. So I would like to show you the very first error I got to see if it was indeed the issue about tersor type. I've been researching and trying solutions but they did not work.

Looking forward to your reply. Thanks!

Lydiasc34 avatar Dec 07 '20 15:12 Lydiasc34

Also, even if I converted tokens to LongTensor ( and also some other variables such as mini_batch, cur_class_batch and next_classes_batch in the generate_logits function from model.py, that needs LongTensor type otherwise same error occurs), I got another error that:

0it [00:01, ?it/s] Traceback (most recent call last): File "C:/HiLAP/main.py", line 822, in train_taxo() File "C:/HiLAP/main.py", line 331, in train_taxo finish_episode(policy, update=True) File "C:/HiLAP/main.py", line 89, in finish_episode entropy = torch.cat(policy.entropy_l).mean() RuntimeError: zero-dimensional tensor (at position 0) cannot be concatenated

Have you ever had similar issues? Any thoughts would be appreciated!

Lydiasc34 avatar Dec 07 '20 20:12 Lydiasc34

I searched the first error, and the web suggested there might be some difference when running on Win and Linux. Sry I never tried on Win but I think the code should be runnable on Linux as ppl in other issues have reproduced the results.

morningmoni avatar Dec 11 '20 02:12 morningmoni

Hello,can you share your file"doc.txt"?

Perfect0530 avatar Nov 13 '22 12:11 Perfect0530

Hello,can you share your file"doc.txt"?

Hi, can you refer to https://github.com/morningmoni/HiLAP#data?

morningmoni avatar Nov 13 '22 22:11 morningmoni