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

Created on 6 Jul 2017  路  13Comments  路  Source: facebookresearch/InferSent

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?

All 13 comments

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.

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

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

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?

Yes. No other changes are required :)

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

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

what does the error mean?

@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]

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)

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

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.

torch.from_numpy()

it actually returns doubleTensor

Was this page helpful?
0 / 5 - 0 ratings

Related issues

8carlosf picture 8carlosf  路  6Comments

nstfk picture nstfk  路  9Comments

DrappierTechnologies picture DrappierTechnologies  路  7Comments

dami23 picture dami23  路  9Comments

LeenaShekhar picture LeenaShekhar  路  3Comments