0
0
PyTorchml~8 mins

Optimizers (SGD, Adam) in PyTorch - Model Metrics & Evaluation

Choose your learning style9 modes available
Metrics & Evaluation - Optimizers (SGD, Adam)
Which metric matters for Optimizers and WHY

When using optimizers like SGD or Adam, the key metric to watch is the training loss. This tells us how well the model is learning from data step-by-step. A good optimizer helps the loss go down smoothly and quickly.

We also look at validation loss and accuracy to check if the model is improving on new data, not just memorizing training data.

Confusion Matrix Example

While optimizers do not directly affect confusion matrices, the model trained with them produces predictions that can be evaluated with a confusion matrix.

      Confusion Matrix (Example):
      --------------------------
      |         | Pred Pos | Pred Neg |
      |---------|----------|----------|
      | True Pos|   80     |   20     |
      | True Neg|   10     |   90     |
      --------------------------
    

This matrix helps calculate precision and recall, which show how well the optimizer helped the model learn to classify.

Precision vs Recall Tradeoff with Optimizers

Optimizers affect how fast and well a model learns, which impacts precision and recall.

SGD often requires careful tuning and may learn slower, possibly leading to lower recall if training stops early.

Adam adapts learning rates and often reaches better precision and recall faster, but can sometimes overfit if not monitored.

Example: For a spam filter, high precision means fewer good emails marked as spam. Adam might help reach this faster, but SGD can be more stable with tuning.

Good vs Bad Metric Values for Optimizers

Good: Training loss steadily decreases, validation loss decreases or stays stable, accuracy improves, precision and recall balance well.

Bad: Training loss stuck or bouncing, validation loss increasing (overfitting), accuracy not improving, precision or recall very low.

For example, if after many epochs the loss does not improve, the optimizer settings may be wrong.

Common Pitfalls with Optimizer Metrics
  • Accuracy Paradox: High accuracy but poor recall or precision means the model is not truly good.
  • Data Leakage: If validation data leaks into training, loss and accuracy look better but model fails in real use.
  • Overfitting: Training loss goes down but validation loss goes up, optimizer may be too aggressive or learning rate too high.
  • Ignoring Learning Rate: Wrong learning rate can cause loss to not improve or diverge.
Self Check

Your model trained with Adam optimizer has 98% accuracy but only 12% recall on fraud cases. Is it good for production?

Answer: No. The low recall means the model misses most fraud cases, which is dangerous. Despite high accuracy, the model is not reliable for fraud detection. You should improve recall, possibly by tuning the optimizer or model.

Key Result
Training loss and validation metrics show how well optimizers like SGD and Adam help the model learn effectively.