Hi Amin!



 amin:

I create y-label with 3 classes and also the output of my network.


No, this is not quite right.


y_label = [[[1, 0, 2],
            [1, 1, 0],
            [2, 0, 0],
            [1, 0, 0]]]
y_label = torch.FloatTensor(y_label)



Yes, this is almost correct (assuming that the y_label are your
ground-truth labels, provided externally, rather than predicted
by your network).  The shape and values are fine, but this needs
to be a LongTensor (rather than a FloatTensor).  Thus:
y_label_list = [[[1, 0, 2],
            [1, 1, 0],
            [2, 0, 0],
            [1, 0, 0]]]
y_label = torch.LongTensor (y_label_list)
# or
y_label = torch.tensor (y_label_list, dtype = torch.int64)
# or just
y_label = torch.tensor (y_label_list)

(This will be the second argument passed to a CrossEntropyLoss
object and is referred to in the CrossEntropyLoss documentation
as target.)


y_target = [[[0.950, 0.210, 0.890],
             [0.875, 0.254, 0.248],
             [0.875, 0.012, 0.148],
             [0.542, 0.143, 0.002]]]
y_target = torch.FloatTensor(y_target)



If this is meant to be the prediction made by your network (your
network’s output), then it isn’t right.
(As a matter of nomenclature, I would not call the output of your
network “target” as “target” is normally used to denote the
ground-truth labels.  I like to call it “prediction.”  This becomes the
first argument to your CrossEntropyLoss object and is referred
to in the documentation as input.  Thus the output of your network
is the “prediction” and is the input to CrossEntropyLoss.)
Let me use y_pred as the name for what you call y_target.
Your y_pred is missing its nClass dimension.  That is, it should
have shape [nBatch, nClass, H, W], so, using the values in your
example, [1, 3, 4, 3].
For each “pixel” in your predicted “image” you have three (nClass)
logits that are the raw scores for that pixel being in each of your
three classes.  Thus y_pred[0, :, 2, 1] is the 1d vector of three
logits that predicts to which of your three classes your “2-1” pixel
belongs.


loss_func = nn.CrossEntropyLoss()
loss = loss_func(y_label, y_target)



Based on what I think your variables are supposed to be, you have
your two arguments to loss_func passed in the wrong order.
Using my preferred y_pred) (instead of your y_target), this should
be:
loss_func = nn.CrossEntropyLoss()
loss = loss_func (y_pred, y_label)

Thus:
>>> import torch
>>> torch.__version__
'1.9.0'
>>> _ = torch.manual_seed (2021)
>>> y_label = [[[1, 0, 2],
...             [1, 1, 0],
...             [2, 0, 0],
...             [1, 0, 0]]]
>>> y_label = torch.tensor (y_label)
>>> y_pred = torch.randn (1, 3, 4, 3)
>>> loss_func = torch.nn.CrossEntropyLoss()
>>> loss = loss_func (y_pred, y_label)
>>> loss
tensor(1.5173)

Best.
K. Frank