Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Error while running encoder/play.ipynb #5

Closed
LeenaShekhar opened this issue Jul 7, 2017 · 3 comments
Closed

Error while running encoder/play.ipynb #5

LeenaShekhar opened this issue Jul 7, 2017 · 3 comments

Comments

@LeenaShekhar
Copy link

LeenaShekhar commented Jul 7, 2017

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:

Nb words kept : 128201/130068 (98.56 %)

RuntimeError Traceback (most recent call last)
in ()
----> 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

@wasiahmad
Copy link

wasiahmad commented Jul 7, 2017

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.

@aconneau
Copy link
Contributor

aconneau commented Jul 7, 2017

Indeed, thanks Wasi. Solved in 9357c2b (bug coming from recent commit 260bfd4).

@aconneau aconneau closed this as completed Jul 7, 2017
@LeenaShekhar
Copy link
Author

This is working now. Thank you.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants