devforfu / pytorch_playground

PyTorch experiments, demos and tutorials
60 stars 41 forks source link

Index out of range #1

Open carloandrea opened 5 years ago

carloandrea commented 5 years ago

Hi, I've just cloned this nice repo but I'm having issues in running the movielens noteboot out-of-the-box.

When I try to run the training code I get the error below, can you help me in finding the reason and fix it? I suspect it has something to do with embeddings size. Many thanks!

`RuntimeError Traceback (most recent call last)

in 32 # compute gradients only during 'train' phase 33 with torch.set_grad_enabled(training): ---> 34 outputs = net(x_batch[:, 1], x_batch[:, 0], minmax) 35 loss = criterion(outputs, y_batch) 36 ~\AppData\Local\Continuum\anaconda3\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs) 491 result = self._slow_forward(*input, **kwargs) 492 else: --> 493 result = self.forward(*input, **kwargs) 494 for hook in self._forward_hooks.values(): 495 hook_result = hook(self, input, result) in forward(self, users, movies, minmax) 61 62 def forward(self, users, movies, minmax=None): ---> 63 features = torch.cat([self.u(users), self.m(movies)], dim=1) 64 x = self.drop(features) 65 x = self.hidden(x) ~\AppData\Local\Continuum\anaconda3\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs) 491 result = self._slow_forward(*input, **kwargs) 492 else: --> 493 result = self.forward(*input, **kwargs) 494 for hook in self._forward_hooks.values(): 495 hook_result = hook(self, input, result) ~\AppData\Local\Continuum\anaconda3\lib\site-packages\torch\nn\modules\sparse.py in forward(self, input) 115 return F.embedding( 116 input, self.weight, self.padding_idx, self.max_norm, --> 117 self.norm_type, self.scale_grad_by_freq, self.sparse) 118 119 def extra_repr(self): ~\AppData\Local\Continuum\anaconda3\lib\site-packages\torch\nn\functional.py in embedding(input, weight, padding_idx, max_norm, norm_type, scale_grad_by_freq, sparse) 1504 # remove once script supports set_grad_enabled 1505 _no_grad_embedding_renorm_(weight, input, max_norm, norm_type) -> 1506 return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse) 1507 1508 RuntimeError: index out of range at ..\aten\src\TH/generic/THTensorEvenMoreMath.cpp:193 `
SunyChung commented 5 years ago

I found this code today and ran into the same issue.

If you change the indexing in "In [37]" outputs = net(x_batch[:, 1], x_batch[:, 0], minmax) into outputs = net(x_batch[:, 0], x_batch[:, 1], minmax)

, it works fine. Hope it helps.