Viqus Logo Viqus Logo
Home
Categories
Language Models Generative Imagery Hardware & Chips Business & Funding Ethics & Society Science & Robotics
Resources
AI Glossary Academy CLI Tool Labs
About Contact
Back to Glossary
Technical Concepts Intermediate Also: Cost Function, Objective Function, Error Function

Loss Function

Definition

A mathematical function that quantifies the difference between a model's predictions and the true values — the signal that guides the learning process by telling the model how wrong it is and in which direction to improve.

In Depth

The loss function is the signal that tells a machine learning model how badly it is performing. During training, the model makes predictions; the loss function computes a scalar value measuring the discrepancy between predictions and ground truth labels. The optimizer (gradient descent) then adjusts model parameters to minimize this loss value. In this sense, the loss function is literally what the model is trying to optimize — it defines the learning objective.

Different tasks require different loss functions. For regression (predicting continuous values), Mean Squared Error (MSE) is standard: it penalizes large errors heavily, as the square amplifies discrepancies. Mean Absolute Error (MAE) is more robust to outliers. For binary classification, Binary Cross-Entropy measures how well the predicted probability matches the true label — rewarding confident correct predictions and heavily penalizing confident wrong ones. For multi-class classification, Categorical Cross-Entropy generalizes this. For object detection, specialized losses like Focal Loss address class imbalance by down-weighting easy examples.

Choosing the right loss function is a critical design decision. A loss that penalizes the wrong things will train a model that optimizes the wrong objective — even if the model achieves low loss, it may not perform well on the task that matters. For example, accuracy is a poor loss function for imbalanced datasets (a model that always predicts 'no fraud' achieves 99.9% accuracy on data where fraud is rare, but is useless). Cross-entropy and F1-score-based losses are better aligned with the true objective in such cases.

Key Takeaway

The loss function is what a model is literally trying to minimize during training — get it wrong, and the model optimizes for the wrong thing, regardless of how sophisticated the architecture is.

Real-World Applications

01 Image classification training: cross-entropy loss guiding a CNN to minimize the difference between predicted and true class probabilities.
02 Regression modeling: MSE loss training a neural network to predict house prices, minimizing squared deviations from actual sale prices.
03 GAN training: adversarial loss functions where the generator minimizes and the discriminator maximizes the same objective simultaneously.
04 Language model pre-training: next-token prediction cross-entropy loss across trillions of tokens to train GPT-scale models.
05 Object detection: focal loss addressing class imbalance by giving more weight to hard-to-classify examples during training.

Frequently Asked Questions

What is the difference between a loss function and an evaluation metric?

A loss function is what the model optimizes during training — it must be differentiable for gradient descent to work. An evaluation metric is what you care about in practice (e.g., accuracy, F1 score). They're often different: you might train with cross-entropy loss but evaluate with accuracy. The loss function is for the optimizer; the metric is for you.

Which loss function should I use?

For regression: Mean Squared Error (MSE) or Mean Absolute Error (MAE). For binary classification: Binary Cross-Entropy. For multi-class classification: Categorical Cross-Entropy. For ranking: Contrastive loss or Triplet loss. For imbalanced classes: Focal Loss or weighted cross-entropy. The choice should match your prediction type, and in some cases, domain-specific losses can significantly improve results.

What happens if you choose the wrong loss function?

The model optimizes what you measure — choosing the wrong loss function means optimizing the wrong objective. A regression model using MAE will be robust to outliers but imprecise; using MSE will be precise but sensitive to outliers. In classification, using unweighted loss on imbalanced data trains the model to predict the majority class. The loss function shapes what the model learns to care about.