Hello Patxi!



 Patxi_Iriarte:

The output size coincides with my target tensor size.


This is your error.  NLLLoss 3 expects a target (your y) with one less
dimension that the input (your out) – that is, without the nClass
dimension.


However, when calling NLLLoss(out, y), the following error occurs:
ValueError: Expected target size (250, 4), got torch.Size([250, 20, 4])



In the so-called “K-dimensional case” with K = 1, NLLLoss expects
an input of shape [nBatch, nClass, d_1] and a target of shape
[nBatch, d_1].  target does not have an nClass dimension; instead,
the values of target are integer class labels that run from 0 to
nClass - 1.
I’m guessing that you want to pass in an input (your out) of shape
[nBatch = 250, nClass = 4, d_1 = nSeq = 20], and therefore
have a target with a shape of [250, 20] whose values are in the
range [0, 3] (inclusive).
Best.
K. Frank