I have a NN that ends with the following linear layers
dense = nn.Linear(input_size, 1)
if I use CrossEntropyLoss
as loss function (as I’m y
is supposed to be the class number) I get the following error
RuntimeError Traceback (most recent call last)
<ipython-input-39-72a754e03ca3> in <module>()
1 lr = 2e-2
2 learner = SimpleLearner([train_dl, test_dl], model, loss_func)
----> 3 history = learner.fit(10)
<ipython-input-37-121ec7440a76> in fit(self, epochs, lr)
26 losses = []
27 for x,y in self.data[0]:
---> 28 losses.append(self.update(x, y , lr))
29 history['losses'].append(np.mean(losses))
30 return history
<ipython-input-37-121ec7440a76> in update(self, x, y, lr)
10 for p in model.parameters(): w2 += (p**2).sum()
11 # add to regular loss
---> 12 loss = loss_func(y_hat, y) + w2 * self.wd
13 loss.backward()
14 with torch.no_grad():
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
477 result = self._slow_forward(*input, **kwargs)
478 else:
--> 479 result = self.forward(*input, **kwargs)
480 for hook in self._forward_hooks.values():
481 hook_result = hook(self, input, result)
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/loss.py in forward(self, input, target)
865 def forward(self, input, target):
866 return F.cross_entropy(input, target, weight=self.weight,
--> 867 ignore_index=self.ignore_index, reduction=self.reduction)
868
869
/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction)
1778 if size_average is not None or reduce is not None:
1779 reduction = _Reduction.legacy_get_string(size_average, reduce)
-> 1780 return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
1781
1782
/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce, reduction)
1623 .format(input.size(0), target.size(0)))
1624 if dim == 2:
-> 1625 ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
1626 elif dim == 4:
1627 ret = torch._C._nn.nll_loss2d(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
RuntimeError: Expected object of scalar type Long but got scalar type Float for argument #2 'target'
What should be the loss (similarly for the accuracy) function that I should be using? if CrossEntropyLoss
is the good one that should I do with the output of the linear layer?