1. The Core Philosophy of Machine Learning Optimization
A neural network is initialized with random weights and biases . 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:
- A Loss Function (): A mathematical scorekeeper that outputs a single real number representing how bad the model's current predictions are.
- 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 and actual targets :
- Penalizes large outlier errors quadratically.
- The gradient is smooth and proportional to the error magnitude: .
B. Cross-Entropy Loss — for Classification
Measures the divergence between the true one-hot distribution and the predicted probability distribution :
In binary classification, this reduces to:
- Heavily penalizes confident wrong predictions (as when , ).
- 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 points in the direction of steepest increase. Therefore, stepping in the negative gradient direction decreases the loss:
Where is the learning rate:
- Too small : Training takes forever and can get trapped in shallow local minima.
- Too large : 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.