When I run the following lines:
embeddings = model.encode(sentences, bsize=128, tokenize=False, verbose=True)
print('nb sentences encoded : {0}'.format(len(embeddings)))
I get the following error:
RuntimeError Traceback (most recent call last)
----> 1 embeddings = model.encode(sentences, bsize=128, tokenize=False, verbose=True)
2 print('nb sentences encoded : {0}'.format(len(embeddings)))
/home/leena/Downloads/InferSent-master/encoder/models.py in encode(self, sentences, bsize, tokenize, verbose)
177 if self.use_cuda:
178 batch = batch.cuda()
--> 179 batch = self.forward((batch, lengths[stidx:stidx + bsize])).data.cpu().numpy()
180 embeddings.append(batch)
181 embeddings = np.vstack(embeddings)
/home/leena/Downloads/InferSent-master/encoder/models.py in forward(self, sent_tuple)
48
49 # Un-sort by length
---> 50 idx_unsort = torch.from_numpy(idx_unsort).cuda() if self.use_cuda else torch.from_numpy(idx_sort)
51 sent_output = sent_output.index_select(1, Variable(idx_unsort))
52
RuntimeError: from_numpy expects an np.ndarray but got torch.LongTensor
Version details:
'3.6.0 |Anaconda custom (64-bit)| (default, Dec 23 2016, 12:22:00) \n[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]'
In [ ]:
torch-0.1.12.post2
There is a small bug. Line no. 50 should be:
idx_unsort = torch.from_numpy(idx_unsort).cuda() if self.use_cuda else torch.from_numpy(idx_unsort)
instead of,
idx_unsort = torch.from_numpy(idx_unsort).cuda() if self.use_cuda else torch.from_numpy(idx_sort)
In the else condition, it should be torch.from_numpy(idx_unsort)
rather than torch.from_numpy(idx_sort)
. idx_sort
is a torch.LongTensor. See line no. 41.
Indeed, thanks Wasi. Solved in 9357c2b6c6eb38cd5bdfd69ce6ab88a39973a399 (bug coming from recent commit 260bfd45c915529dd5ab75a55d3aa3b94432dee0).
This is working now. Thank you.
Most helpful comment
There is a small bug. Line no. 50 should be:
idx_unsort = torch.from_numpy(idx_unsort).cuda() if self.use_cuda else torch.from_numpy(idx_unsort)
instead of,
idx_unsort = torch.from_numpy(idx_unsort).cuda() if self.use_cuda else torch.from_numpy(idx_sort)
In the else condition, it should be
torch.from_numpy(idx_unsort)
rather thantorch.from_numpy(idx_sort)
.idx_sort
is a torch.LongTensor. See line no. 41.