Hi Ziba!



 ziba:

I have a multiclass classification problem and for it I have a convolutional neural network that has Linear layer in its last layer.


Your final Linear layer will produce* a set of raw-score logits
(unnormalized log-odds-ratios), one for each of the classes.  These
are related to the probabilities that the network predicts for the sample
in question being in each of the classes, and, specifically, the class
probabilities are given by softmax() of the predicted logits.
*)  Your network produces such values in essence because you train
it to produce such values.


pred = CNN(x)
_, label_1 = torch.max(pred)

pred_soft = F.softmax(pred)
_, label_2 = torch.max(pred_soft )



Here you want _, label_1 = torch.max (pred, dim = 1) (assuming
that pred has shape [nBatch, nClass]).  As written, your code will
produce an error.  (Similarly, you want torch.max (pred_soft, dim = 1).)


1- Why getting the torch.max() from this prediction will give us the label, I mean why for desired label our model produce bigger values?


With the corrected expression, torch.max() will return both the max(),
which gets assigned to the variable _ (used stylistically in python as a
“throw-away” variable), and the argmax() (the index of the maximum
element), which gets assigned to label_1.  The largest logit corresponds
to the largest probability, and the index of the largest logit is the class
label for what the network is predicting as the most probable class.


2- why getting the torch.max() from this prediction and from F.softmax() will give use same results?


The logits, pred, and the probabilities, F.softmax (pred), are different
numbers, but the largest logit and the largest probability correspond to
one another (as do the second largest, and so on).  So the index of the
largest logit (argmax (pred)) and the index of the largest probability
(argmax (F.softmax (pred))) are the same – that is, they both give
you the same predicted class label.
Best.
K. Frank