Theory ofIntelligence
Index
Optimization & TrainingBook Index: Letter L

Loss Functions & Gradient Descent: How Networks Learn

Understanding how loss functions quantify prediction error and how gradient descent navigates high-dimensional weight landscapes to minimize loss.

functionsMathematical Representation
w(t+1)=w(t)−η ∇wL(w)\mathbf{w}^{(t+1)} = \mathbf{w}^{(t)} - \eta \, \nabla_{\mathbf{w}} \mathcal{L}(\mathbf{w})

1. The Core Philosophy of Machine Learning Optimization

A neural network is initialized with random weights W\mathbf{W} and biases b\mathbf{b}. At first, its predictions are completely meaningless noise.

Learning is the process of adjusting these parameters so that the model's outputs get progressively closer to the ground truth. To achieve this, two components are required:

  1. A Loss Function (L\mathcal{L}): A mathematical scorekeeper that outputs a single real number representing how bad the model's current predictions are.
  2. An Optimization Algorithm: A mathematical rule that guides how to nudge each parameter to decrease the loss.

2. Standard Loss Functions

A. Mean Squared Error (MSE) — for Regression

Measures the average squared difference between predictions y^i\hat{y}_i and actual targets yiy_i:

MSE=1N∑i=1N(yi−y^i)2\text{MSE} = \frac{1}{N} \sum_{i=1}^{N} (y_i - \hat{y}_i)^2

  • Penalizes large outlier errors quadratically.
  • The gradient is smooth and proportional to the error magnitude: ∂MSE∂y^i=−2N(yi−y^i)\frac{\partial \text{MSE}}{\partial \hat{y}_i} = -\frac{2}{N}(y_i - \hat{y}_i).

B. Cross-Entropy Loss — for Classification

Measures the divergence between the true one-hot distribution y\mathbf{y} and the predicted probability distribution y^\hat{\mathbf{y}}:

LCE=−∑k=1Kyklog⁡(y^k)\mathcal{L}_{\text{CE}} = -\sum_{k=1}^{K} y_k \log(\hat{y}_k)

In binary classification, this reduces to: LBCE=−[ylog⁡(y^)+(1−y)log⁡(1−y^)]\mathcal{L}_{\text{BCE}} = -[y \log(\hat{y}) + (1 - y) \log(1 - \hat{y})]

  • Heavily penalizes confident wrong predictions (as y^→0\hat{y} \to 0 when y=1y = 1, −log⁡(y^)→∞-\log(\hat{y}) \to \infty).
  • Standard loss function for language models predicting next-token probabilities.

3. Gradient Descent: Walking Downhill

Imagine being blindfolded on a foggy mountain and wanting to reach the bottom of the valley. At each step, you feel the slope of the ground beneath your feet and take a step in the direction of steepest descent.

Mathematically, the gradient vector ∇wL\nabla_{\mathbf{w}} \mathcal{L} points in the direction of steepest increase. Therefore, stepping in the negative gradient direction decreases the loss:

w(t+1)=w(t)−η ∇wL\mathbf{w}^{(t+1)} = \mathbf{w}^{(t)} - \eta \, \nabla_{\mathbf{w}} \mathcal{L}

Where η>0\eta > 0 is the learning rate:

  • Too small η\eta: Training takes forever and can get trapped in shallow local minima.
  • Too large η\eta: The parameters overshoot the minimum, oscillating wildly or diverging to infinity.

Modern Optimizers: Beyond Plain SGD

  • Momentum: Accelerates gradient vectors in the right direction, dampening oscillations like a heavy ball rolling downhill.
  • Adam (Adaptive Moment Estimation): The standard optimizer in modern deep learning and LLMs. Dynamically tracks exponentially decaying moving averages of past gradients and squared gradients, adapting the learning rate individually for every single parameter.