facebookresearch / InferSent

InferSent sentence embeddings
Other
2.28k stars 471 forks source link

RuntimeError: tried to construct a tensor from a int sequence, but found an item of type numpy.int64 at index (0) #2

Closed wasiahmad closed 7 years ago

wasiahmad commented 7 years ago

I tried to run the train_nli.py file to train your model but got the following error.

Traceback (most recent call last):
  File "train_nli.py", line 283, in <module>
    train_acc = trainepoch(epoch)
  File "train_nli.py", line 176, in trainepoch
    output = nli_net((s1_batch, s1_len), (s2_batch, s2_len))
  File "/if5/wua4nw/anaconda3/lib/python3.5/site-packages/torch/nn/modules/module.py", line 206, in __call__
    result = self.forward(*input, **kwargs)
  File "/net/if5/wua4nw/wasi/academic/research_with_prof_chang/fb_research_repos/InferSent/models.py", line 731, in forward
    u = self.encoder(s1)
  File "/if5/wua4nw/anaconda3/lib/python3.5/site-packages/torch/nn/modules/module.py", line 206, in __call__
    result = self.forward(*input, **kwargs)
  File "/net/if5/wua4nw/wasi/academic/research_with_prof_chang/fb_research_repos/InferSent/models.py", line 44, in forward
    idx_sort = torch.cuda.LongTensor(idx_sort) if self.use_cuda else torch.LongTensor(idx_sort)
RuntimeError: tried to construct a tensor from a int sequence, but found an item of type numpy.int64 at index (0)

Any guess why I am getting this error? I am using python 3.5, can it be a reason?

wasiahmad commented 7 years ago

I solved the issue by using .tolist() for numpy array as follows.

idx_sort = torch.cuda.LongTensor(idx_sort.tolist()) if self.use_cuda else torch.LongTensor(idx_sort)

line 53. suffers from same issue.

aconneau commented 7 years ago

Glad you solved the issue. I'm a bit blind here because I'm using python 2.7 but can you try and replace this line with: "idx_sort = torch.from_numpy(idx_sort).cuda() if self.use_cuda else torch.from_numpy(idx_sort)" and let me know if that fixes the problem? Thanks

wasiahmad commented 7 years ago

Yes, this solves the issue because torch.from_numpy() returns a torch.LongTensor and converting it to cuda suffices. Thanks.

aconneau commented 7 years ago

For the record, is this the only thing you needed to change in the models.py to make the model work in python 3.5?

wasiahmad commented 7 years ago

Yes. No other changes are required :)

aconneau commented 7 years ago

260bfd45c915529dd5ab75a55d3aa3b94432dee0 : fixed bug in models.py using torch.from_numpy for python 3.5.

LeenaShekhar commented 7 years ago

I guess my error is due to that line as well: https://github.com/facebookresearch/InferSent/issues/5

brando90 commented 6 years ago

what does the error mean?

brando90 commented 6 years ago

@wasiahmad is the .tolist() method a numpy or pytorch thing? [its sort of hard to follow whats going on cuz I can't see the code or the type of ur code that lead to these errors]

ImtiazKhanDS commented 6 years ago

just for the record , I too got this error , nothing solved my issue. May be there will be people like me so just giving heads up

When you define a Field in torchtext by default it is torch.LongTensor , so you can pass whatever type you want like this data.Field(sequential=False, use_vocab=False,tensor_type=torch.cuda.FloatTensor)

ikanez commented 6 years ago

defining the data.Field as mentioned by @imtiazBDSgit solved my error. thanks.

lunjiaqi commented 6 years ago

Traceback (most recent call last): File "train_nli.py", line 291, in train_acc = trainepoch(epoch) File "train_nli.py", line 179, in trainepoch output = nli_net((s1_batch, s1_len), (s2_batch, s2_len)) File "/home/zjt2/anaconda2/lib/python2.7/site-packages/torch/nn/modules/module.py", line 491, in call result = self.forward(*input, *kwargs) File "/mnt/merry/zjt2/jiaqi/InferSent/models.py", line 826, in forward u = self.encoder(s1) File "/home/zjt2/anaconda2/lib/python2.7/site-packages/torch/nn/modules/module.py", line 491, in call result = self.forward(input, **kwargs) File "/mnt/merry/zjt2/jiaqi/InferSent/models.py", line 64, in forward sent = sent.index_select(1, Variable(idx_sort)) RuntimeError: Expected object of type torch.cuda.LongTensor but found type torch.LongTensor for argument #3 'index' I have some issues about it.

Moeinh77 commented 5 years ago

torch.from_numpy()

it actually returns doubleTensor