• March 20, 2025

Loss Function vs Accuracy

Both loss function and accuracy are used to evaluate machine learning models, but they measure performance differently.


1️⃣ Loss Function

🔹 Purpose:

  • Measures how well the model’s predictions match the true values.
  • Used to train the model by minimizing the error.
  • Provides a continuous, differentiable value, making it useful for gradient-based optimization.

🔹 Examples:

  • Regression Losses:
    • Mean Squared Error (MSE)
    • Mean Absolute Error (MAE)
  • Classification Losses:
    • Cross-Entropy Loss
    • Hinge Loss (SVM)

🔹 Example (Cross-Entropy Loss in PyTorch):

import torch.nn as nn

loss_fn = nn.CrossEntropyLoss()
y_pred = torch.tensor([[2.0, 0.5, 0.1]]) # Predicted logits
y_true = torch.tensor([0]) # True label

loss = loss_fn(y_pred, y_true)
print(f"Loss: {loss.item()}")

2️⃣ Accuracy

🔹 Purpose:

  • Measures the percentage of correct predictions.
  • Used to evaluate model performance, but not for training (because it’s non-differentiable).
  • Works well for classification tasks but not regression.

🔹 Formula: Accuracy=Number of Correct PredictionsTotal Predictions×100\text{Accuracy} = \frac{\text{Number of Correct Predictions}}{\text{Total Predictions}} \times 100Accuracy=Total PredictionsNumber of Correct Predictions​×100

🔹 Example (Accuracy in PyTorch):

import torch

y_pred = torch.tensor([1, 0, 2, 1]) # Model predictions (class indices)
y_true = torch.tensor([1, 0, 2, 0]) # True labels

accuracy = (y_pred == y_true).sum().item() / len(y_true)
print(f"Accuracy: {accuracy * 100:.2f}%")

🔑 Key Differences

FeatureLoss FunctionAccuracy
DefinitionMeasures prediction errorMeasures correct predictions
Type of ValueContinuous (e.g., 0.5, 1.2)Discrete (e.g., 85%)
Used for Training?✅ Yes (Optimized by gradient descent)❌ No (Not differentiable)
Works forRegression & ClassificationClassification only
Common FunctionsMSE, Cross-EntropyAccuracy Score

🛠️ When to Use Each?

  • Use a loss function to train and optimize the model.
  • Use accuracy to evaluate the model’s performance.

🚀 Final Thought

Loss function helps the model learn, while accuracy tells you how well it’s performing.

Let me know if you need further clarification! 🚀

Leave a Reply

Your email address will not be published. Required fields are marked *