Hi,
I use training code of
model.zero_grad()
out = model()
print(y)
print(out)
loss = criterion(out, y)
loss.backward(retain_graph = True)
optimizer.step()
This code outputs as follows. (y is onehot encoded label)
[0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]
tensor([0.0303, 0.0090, 0.0182, 0.2649, 0.2079, 0.0842, 0.4543, 0.0255, 0.0294,
0.8613], grad_fn=<MulBackward0>)
I meet an error of;
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-110-56f054f4ae5c> in <module>()
21 print(y)
22 print(out)
---> 23 loss = criterion(out, y)
24 loss.backward(retain_graph = True)
25 optimizer.step()
3 frames
/usr/local/lib/python3.6/dist-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)
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/loss.py in forward(self, input, target)
940 def forward(self, input, target):
941 return F.cross_entropy(input, target, weight=self.weight,
--> 942 ignore_index=self.ignore_index, reduction=self.reduction)
943
944
/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction)
2054 if size_average is not None or reduce is not None:
2055 reduction = _Reduction.legacy_get_string(size_average, reduce)
-> 2056 return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
2057
2058
/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in log_softmax(input, dim, _stacklevel, dtype)
1348 dim = _get_softmax_dim('log_softmax', input.dim(), _stacklevel)
1349 if dtype is None:
-> 1350 ret = input.log_softmax(dim)
1351 else:
1352 ret = input.log_softmax(dim, dtype=dtype)
IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)
Does this mean of that loss function expects scalar “out” ?
(NOT yet use softmax for “out” on output-layer)