i have a problem with NLLLoss()
ValueError Traceback (most recent call last)
7 target_variable = training_pair[1]
8 # Run the train function
----> 9 loss = train(input_variable, target_variable, encoder, decoder, encoder_optimizer, decoder_optimizer, criterion)
10
11 # Keep track of loss
48 print(decoder_output[0])
49 print(target_variable.view(-1,1).size())
---> 50 loss += criterion(decoder_output[0], target_variable[di].view(-1,1))
51
52 # Get most likely word index (highest value) from output
/home/a20112128/anaconda3/lib/python3.6/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)
/home/a20112128/anaconda3/lib/python3.6/site-packages/torch/nn/modules/loss.py in forward(self, input, target)
130 _assert_no_grad(target)
131 return F.nll_loss(input, target, self.weight, self.size_average,
--> 132 self.ignore_index)
133
134
/home/a20112128/anaconda3/lib/python3.6/site-packages/torch/nn/functional.py in nll_loss(input, target, weight, size_average, ignore_index)
674 return _functions.thnn.NLLLoss2d.apply(input, target, weight, size_average, ignore_index)
675 else:
--> 676 raise ValueError('Expected 2 or 4 dimensions (got {})'.format(dim))
677
678
ValueError: Expected 2 or 4 dimensions (got 1)
I have the same issue, do you know how to fix this?
In
loss += criterion(decoder_output[0], target_variable[di].view(-1,1))
replace decoder_output[0]
with decoder_output
Should be fixed by #89 from @tejaslodaya
Most helpful comment
In
loss += criterion(decoder_output[0], target_variable[di].view(-1,1))
replace
decoder_output[0]
withdecoder_output