Hello Neo!



 neoSilex:

I’d like to use the cross-entropy loss function
number of classes=2
output.shape=[4,2,224,224]


As an aside, for a two-class classification problem, you will be
better off treating this explicitly as a binary problem, rather than
as a two-class instance of the more general multi-class problem.
To do so you would use BCEWithLogitsLoss (“Binary Cross
Entropy”), rather than the multi-class CrossEntropyLoss.
But you can certainly treat this as a general multi-class problem,
and I will answer your question in this context.


output_min=tensor(-1.9295)]
output_max=tensor(2.6400)]


So your outputs are raw-score logits, rather than probabilities
that lie between 0.0 and 1.0.  Good.


number of channels=3
target.shape=[4,3,224,224]


I assume that your first dimension, 4, is your batch size.  This is
fine.
It probably does not make sense to have a channels dimension in
your  target.  (If you think it does, you should further explain your
use case.)  In any event, as it stands, this target shape won’t match
your output shape.
If your output shape is [nBatch, nClass, height, width],
then (for CrossEntropyLoss) your target shape must be
[nBatch, height, width], with no nClass dimension.


targets_max=tensor(-2.1008)]
targets_min=tensor(-2.1179)]


This is wrong (for CrossEntropyLoss).  Your target values must
be integer (long) class labels that run from 0 to nClass - 1,
so in your two-class case, that take on the values 0 and 1.
If you could explain a little more where target comes from, and
what the numbers actually mean, we can help sort this out.
Best.
K. Frank