📉Loss Descent and Gradient Descent

Understanding the mechanics of model training — from loss functions to parameter updates

Training a neural network means making its predictions as close to the truth as possible. This process is quantified by a metric called a loss function, and optimized step by step via an algorithm called gradient descent. This document explains three things: what loss is, how it decreases, and how to read a loss curve.

Sample training log

Epoch 1/50: loss=0.6821, acc=0.0512, val_loss=0.6912
Epoch 2/50: loss=0.6512, acc=0.0842, val_loss=0.6589
Epoch 3/50: loss=0.5987, acc=0.1234, val_loss=0.6210
...

Loss Function: Measuring How Wrong the Model Is

A loss function takes two inputs — the model's prediction and the true label — and outputs a single number representing the gap between them. Larger values mean the model is worse; smaller values mean it's closer to the correct answer.

01

Mean Squared Error (MSE)

For regression tasks. Computes the average of squared differences between predictions and truths. Squaring amplifies large errors and prevents positive/negative errors from canceling out.

02

Cross-Entropy (CE)

For classification tasks. As the predicted probability of the correct class approaches 1, the loss approaches 0. This site's number prediction falls into this category.

03

Binary Cross-Entropy (BCE)

A special case for binary classification — yes/no questions like spam detection or medical diagnosis.

Why not use MSE for classification? Because MSE combined with softmax output causes vanishing gradients when predictions are far off, making training extremely slow. Cross-entropy has a simpler, more stable gradient form — which is why it's the standard choice for classification.

Gradient Descent: Pushing Loss Down Step by Step

Imagine standing in a foggy valley, trying to reach the bottom. You can only see a few meters around you, and every step requires deciding which direction to go. Gradient descent is this process: the mountain height is the loss, your position is the current model parameters, the direction is opposite the gradient, and the step size is the learning rate.

Parameter update rule

θ_new = θ_old - η * ∇L(θ_old)

θ is the model parameter, η is the learning rate, ∇L is the gradient of the loss at the current point. Each iteration moves the parameters one small step opposite the gradient.

Where does the gradient come from? A neural network is a composite function — the loss gradient with respect to each layer's parameters can't be written out directly. Backpropagation uses the chain rule to compute them layer by layer: starting from the output, propagating backward to the input, multiplying gradients of each layer. This is exactly what happens inside model.fit() — each iteration performs one forward pass and one backward pass.

Learning Rate: The Most Critical Hyperparameter

The learning rate determines how far each step goes. It's the most sensitive hyperparameter in training — set it wrong, and training fails completely.

Learning RateEffect
Too small (1e-5)Loss drops extremely slowly — dozens of epochs barely change. Wastes compute.
Just right (1e-3)Loss decreases steadily and converges to a low value.
Too big (1.0)Loss oscillates, diverges, or becomes NaN. The model learns nothing.

This site uses the Adam optimizer, whose default learning rate is 0.001. Adam improves on basic gradient descent with momentum and adaptive learning rates: momentum smooths the direction, while adaptive rates shrink the step when gradients are large and expand it when they're small. This makes it less sensitive to the learning rate.

Three Variants of Gradient Descent

Depending on how much data is used per gradient computation, gradient descent has three variants.

Batch Gradient Descent

Uses the entire training set per step. Most accurate direction, but slow and memory-hungry — impractical for large datasets.

Stochastic Gradient Descent

Uses a single sample per step. Very fast but noisy — the loss oscillates wildly and the direction is unstable.

Mini-batch Gradient Descent

Uses one batch (e.g. 32 samples) per step. Balances stability and efficiency — this is what practically everyone uses, including this site.

How to Read the Loss Curve

Open this site's Loss Curve modal and you'll see two lines: a red solid line for training loss (one point per batch) and a blue dashed line for validation loss (one point per epoch). The shape of these lines tells you whether training is going well.

Curve ShapeMeaningAction
Smooth descentTraining is healthy — the model is learningKeep training
Not decreasingLearning rate too small, model too small, or data issueIncrease LR, enlarge model, check data
Wildly oscillatingLearning rate too large, batch too smallLower LR, increase batch size
Train down, val upOverfitting — the model is memorizing noiseShrink model, add dropout, early stop
Both stay highUnderfitting — model too simple or data has no learnable patternEnlarge model, train longer, use a richer architecture

Applied to This Site

Open the Loss Curve modal to see separate curves for the red-ball and blue-ball models. Watch for this: when loss drops to a level and then plateaus, the model has learned everything it can. For lottery data, plateauing quickly is normal — there simply isn't a pattern to learn. This isn't a flaw; it's the nature of this kind of data.

Note

Understanding loss and gradient descent is the foundation for understanding all deep learning training. Whether it's a small LSTM like this site's or a large model like GPT, the underlying training mechanism is the same. But one caveat: low training loss doesn't mean the model has predictive power — especially for random data, overfitting to noise is common.