Hi Hashir!



 hash-ir:

I have a classification problem where each sample output consists of four values each of which is one of the 6 classes, i.e y has shape (batchsize, 4). So far, I have created a toy model which outputs a logits tensor out having the shape (batchsize, 6(=num_classes), 4).
…
I want to know if this is the correct workaround for multivariate classification


If I understand what you are trying to do here (and I am not at all
sure that I do), then torch.nn.CrossEntropyLoss 25 should do what
you want without further manipulation.  (Look at the parts where
the documentation talks about the “K-dimensional case.”)
Let the output of your model be a tensor of shape (batchsize, 6, 4).
I will call this tensor prediction.  I will call the last dimension (the
“4” dimension) the “channel.”  (So in your use case you have four
“channels.”)  For a given sample in the batch and a given channel,
your prediction consists of six logits, one for each of your six classes.
You use your loss function to compare your prediction with your
target.  These are the known class labels that you use for training.
target will be a tensor of shape (batchsize, 4), and for a given
sample within the batch and for a given channel (the “4” dimension)
you will have a single value that is an integer class label ranging
from 0 to 5.
(Note that prediction and target are not of the same shape:
prediction carries all of the batchsize, class, and channel dimensions,
while target, because it uses a single integer class label per item,
carries only the batchsize and channel dimensions.)
By default, CrossEntropyLoss will average your loss over both the
batchsize and channel dimensions.  If you prefer the sum, then pass
in the optional reduction = 'sum' argument when you construct
your CrossEntropyLoss loss-function object.
Best.
K. Frank