0
0
PyTorchml~8 mins

Loss functions (MSELoss, CrossEntropyLoss) in PyTorch - Model Metrics & Evaluation

Choose your learning style9 modes available
Metrics & Evaluation - Loss functions (MSELoss, CrossEntropyLoss)
Which metric matters for Loss functions and WHY

Loss functions like MSELoss and CrossEntropyLoss measure how far the model's predictions are from the true answers during training.

MSELoss (Mean Squared Error) is used for tasks where the output is a number, like predicting house prices. It calculates the average of the squared differences between predicted and actual values. Smaller loss means better predictions.

CrossEntropyLoss is used for classification tasks, like telling if an image is a cat or dog. It measures how well the predicted probabilities match the true class labels. Lower loss means the model is more confident and correct.

Tracking loss helps us know if the model is learning or not. It guides the model to improve step by step.

Confusion matrix or equivalent visualization

Loss functions do not produce a confusion matrix directly, but for classification with CrossEntropyLoss, we can look at a confusion matrix to understand errors:

      Confusion Matrix Example (3 classes):

          Predicted
          C1  C2  C3
    True C1 50   2   3
         C2  4  45   1
         C3  2   3  48

    Total samples = 50+2+3+4+45+1+2+3+48 = 158
    

This matrix helps us see where the model confuses classes, complementing the loss value.

Precision vs Recall tradeoff with examples

Loss functions like CrossEntropyLoss do not directly measure precision or recall, but they influence them by improving prediction probabilities.

For example, in a cancer detector:

  • We want high recall (catch all cancer cases). CrossEntropyLoss helps by pushing the model to assign higher probabilities to true cancer cases.
  • If the loss is low but recall is low, the model might be confident but missing cases.

In regression with MSELoss, the tradeoff is between fitting the data closely (low loss) and avoiding overfitting (too perfect on training data but bad on new data).

What "good" vs "bad" metric values look like for Loss functions

Good loss values:

  • MSELoss: Close to 0 means predictions are very close to true values.
  • CrossEntropyLoss: Close to 0 means the model predicts the correct class with high confidence.

Bad loss values:

  • High MSELoss means predictions are far from true values.
  • High CrossEntropyLoss means the model is uncertain or wrong about classes.

Note: Loss values depend on the problem scale and data, so compare loss over training steps or between models.

Metrics pitfalls
  • Ignoring scale: MSELoss can be large if target values are large; always compare relative improvements.
  • Overfitting: Loss may be very low on training data but high on new data, meaning the model memorized instead of learned.
  • Data leakage: If test data leaks into training, loss looks artificially low but model fails in real use.
  • Misinterpreting loss: Loss is not accuracy; a low loss does not always mean perfect predictions.
Self-check question

Your model has a training loss (CrossEntropyLoss) of 0.1 but on test data, the loss is 1.5. Is this good?

Answer: No, this means the model learned well on training data but performs poorly on new data. It likely overfitted and needs better generalization.

Key Result
Loss functions measure how far predictions are from true values; lower loss means better model learning.