The shapes of log_p and target look alright.
Could you check each target batch for its min and max value, as apparently some value is out of bounds before calling the criterion:
print(target.min(), target.max())




 Remote_Senser:

I’m confused when you say to pass the original output and target with a non-reducing criterion then zero out the mask. Isn’t the output of the loss function is a scalar value, how would I zero the mask?


You’ll get a scalar values, since the default reduction is set to 'mean'.
If you pass 'none', you’ll get an output of the same shape as target:
output = torch.randn(10, 10 , requires_grad=True)
target = torch.randint(0, 10, (10,))

loss = F.cross_entropy(output, target, reduction='none')
print(loss.shape)
> torch.Size([10])

Anyway, since your shapes seem to be OK, you could check the range of each target batch and find the batch which causes the trouble.