"dimension out of range" error during training in pytorch-translate.ipynb

Hey everyone,

Just trying to run through the pytorch translate notebook and on train, I’m getting the following error when running:

trainEpochs(encoder, decoder, 10000, print_every=500, lr=0.005)

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-79-f97a70f529f6> in <module>()
----> 1 trainEpochs(encoder, decoder, 10000, print_every=500, lr=0.005)

<ipython-input-77-6ed681638d36> in trainEpochs(encoder, decoder, n_epochs, print_every, lr)
     10         inp = long_t(fra)
     11         targ = long_t(eng)
---> 12         loss = train(inp, targ, encoder, decoder, enc_opt, dec_opt, crit)
     13         loss_total += loss
     14 

<ipython-input-75-b4e644da5528> in train(inp, targ, encoder, decoder, enc_opt, dec_opt, crit)
      7 
      8     for di in range(target_length):
----> 9         decoder_output, hidden = decoder(decoder_input, hidden, encoder_outputs)
     10         decoder_input = targ[:, di]
     11         loss += crit(decoder_output, decoder_input)

/home/ubuntu/anaconda3/lib/python3.5/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    222         for hook in self._forward_pre_hooks.values():
    223             hook(self, input)
--> 224         result = self.forward(*input, **kwargs)
    225         for hook in self._forward_hooks.values():
    226             hook_result = hook(self, input, result)

<ipython-input-62-6aeb096bc58c> in forward(self, inp, hidden, enc_outputs)
     17         w2h = add(dot(hidden[-1], self.W2), self.b2).unsqueeze(1)
     18         u = F.tanh(add(w1e, w2h))
---> 19         a = mul(self.V,u).sum(2).squeeze(2)
     20         a = F.softmax(a).unsqueeze(2)
     21         Xa = mul(a, enc_outputs).sum(1)

/home/ubuntu/anaconda3/lib/python3.5/site-packages/torch/autograd/variable.py in squeeze(self, dim)
    748 
    749     def squeeze(self, dim=None):
--> 750         return Squeeze.apply(self, dim)
    751 
    752     def squeeze_(self, dim=None):

/home/ubuntu/anaconda3/lib/python3.5/site-packages/torch/autograd/_functions/tensor.py in forward(ctx, input, dim, inplace)
    376         else:
    377             if dim is not None:
--> 378                 result = input.squeeze(dim)
    379             else:
    380                 result = input.squeeze()

RuntimeError: dimension out of range (expected to be in range of [-2, 1], but got 2)

Any guidance? The only change I made was changing:

def Var(*sz): return Parameter(Arr(*sz)).cuda()
to
def Var(*sz): return **nn.**Parameter(Arr(*sz)).cuda()

as Parameter was not defined.

Thanks for any and all help!

What did you do to fix this?
Im getting the same error

Hmmmmm… It’s been a long time.

I think I had to downgrade my version of pytorch to pre-2.0. You could try that and let us know if it works?