Hi Akib!



 akib62:

if I want to use the MSE loss instead of cross_entropy then how I have to write the code?


It is a BAD idea to use MSELoss for classification.  Here is the
reason: MSELoss cares about how far the prediction is from the
target.  But in a classification problem you generally don’t have
that notion of distance.
If a tree is 60 years old, I have done better if I predict it to be 55
years old than if I predict it to be 40 years old.  Both predictions
are wrong, but the 40-years-old prediction is worse.  MSELoss
takes this into account.
But if I classify a bird as fish, that’s wrong, but no better or worse
than classifying the bird as a reptile.  There’s no sense of distance
or some concept of a class being closer to some classes and farther
from others.  (Conceptually there could be, but there is not in the
typical pytorch classification use case.)
However …
If you want to use MSELoss for classification, you would convert
your integer class labels to floats (so target would be a FloatTensor
of shape [nBatch], with no nClass dimension).  Your model would
have a single output so it would output batch predictions of shape
[nBatch, 1] which you would squeeze() to get shape [nBatch].
Your (floating-point) target would have values running from 0.0
to nClass - 1.  If your target value were, say, 2.0, then a predicted
value of 2.0 would be perfect, and MSELoss would return 0.0.  A
prediction of 1.95 or 2.05 would be quite good, while a prediction
of 0.0 (or even -1.0) or 4.0 would be worse.
But for a classification problem, for a target of 2.0, would you
really consider a prediction of 0.0 to be worse than a prediction of
1.0, or are they both just wrong because they both predict the wrong
class?
Best.
K. Frank