Problem in cross entropy loss

import torch
import torch.nn as nn
loss = nn.CrossEntropyLoss()
inpt = torch.randn(10,3,requires_grad=True)
target = torch.empty(10, dtype=torch.long).random_(5)
output = loss(inpt, target)
print(inpt)
print(target)
print(output)

I get following error don’t know why?

IndexError Traceback (most recent call last)
in ()
4 inpt = torch.randn(10,3,requires_grad=True)
5 target = torch.empty(10, dtype=torch.long).random_(5)
----> 6 output = loss(inpt, target)
7 print(inpt)
8 print(target)

3 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce, reduction)
2113 .format(input.size(0), target.size(0)))
2114 if dim == 2:
-> 2115 ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
2116 elif dim == 4:
2117 ret = torch._C._nn.nll_loss2d(input, target, weight, _Reduction.get_enum(reduction), ignore_index)

IndexError: Target 3 is out of bounds.

Hi,

Your input data has 3 classes [10, 3] but output has labels from 0 to 5 so you get error for labels bigger than 2. .random_(3) can solve your issue.

Bests

1 Like