Why following code:
import torch
import torch.nn as nn
input_tensor = torch.tensor([[2.0]])
target_tensor = torch.tensor([0], dtype=torch.long)
loss_function = nn.CrossEntropyLoss()
loss = loss_function(input_tensor, target_tensor)
print(loss)
input_tensor = torch.tensor([[2.0]])
target_tensor = torch.tensor([1], dtype=torch.long)
loss_function = nn.CrossEntropyLoss()
loss = loss_function(input_tensor, target_tensor)
print(loss)
showing following error:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Cell In[214], line 14
12 target_tensor = torch.tensor([1], dtype=torch.long)
13 loss_function = nn.CrossEntropyLoss()
---> 14 loss = loss_function(input_tensor, target_tensor)
15 print(loss)
File ~/jupyter-env-3.10/lib/python3.10/site-packages/torch/nn/modules/module.py:1110, in Module._call_impl(self, *input, **kwargs)
1106 # If we don't have any hooks, we want to skip the rest of the logic in
1107 # this function, and just call forward.
1108 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1109 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1110 return forward_call(*input, **kwargs)
1111 # Do not call functions when jit is used
1112 full_backward_hooks, non_full_backward_hooks = [], []
File ~/jupyter-env-3.10/lib/python3.10/site-packages/torch/nn/modules/loss.py:1163, in CrossEntropyLoss.forward(self, input, target)
1162 def forward(self, input: Tensor, target: Tensor) -> Tensor:
-> 1163 return F.cross_entropy(input, target, weight=self.weight,
1164 ignore_index=self.ignore_index, reduction=self.reduction,
1165 label_smoothing=self.label_smoothing)
File ~/jupyter-env-3.10/lib/python3.10/site-packages/torch/nn/functional.py:2996, in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction, label_smoothing)
2994 if size_average is not None or reduce is not None:
2995 reduction = _Reduction.legacy_get_string(size_average, reduce)
-> 2996 return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)
IndexError: Target 1 is out of bounds.
This is because input_tensor
is a class probability. And if the target class is 1
then input_tensor
should have length of at least two (probability of the class 0 and probability of the class 1)
import torch
import torch.nn as nn
predicted_class_probs = torch.tensor([[2.0]])
target_tensor = torch.tensor([0], dtype=torch.long)
loss_function = nn.CrossEntropyLoss()
loss = loss_function(predicted_class_probs, target_tensor)
print(loss)
predicted_class_probs = torch.tensor([[2.0, 3.3]])
target_tensor = torch.tensor([1], dtype=torch.long)
loss_function = nn.CrossEntropyLoss()
loss = loss_function(predicted_class_probs, target_tensor)
print(loss)