Please enable JavaScript to use CodeHS

AI Explained

Making training work

Deep networks are easy to build but hard to train. Getting them to learn, not just memorize, requires the right activations, optimizers, normalization, and regularization.

AI ~13 min High school
Activations

Activations and gradients

A neural network without an activation function is just a big linear function. No matter how many layers it has, it can only draw straight lines. Activations introduce non-linearity, which lets the network model curves and complex shapes.

But activations also control how gradients flow. The sigmoid activation squishes large inputs, so gradients can become very small. In deep networks this becomes the vanishing gradient problem: the early layers stop learning.

ReLU (Rectified Linear Unit) fixes this for positive inputs: it is just max(0, x). It is simple, fast, and keeps gradients strong. The downside is the dying ReLU problem: a neuron whose input is always negative will always output zero and stop learning.

Core idea: choose an activation that keeps gradients alive. ReLU is the default because it usually works; variants like Leaky ReLU and GELU fix edge cases.


Optimizers

Better optimizers

Plain gradient descent uses the same learning rate for every weight. That can be slow. Modern optimizers adapt the step size per weight based on history.

  • Momentum: keep moving in the direction that was working, like a ball rolling downhill.
  • Adam: combine momentum with per-weight adaptive learning rates. It is the most common default today.
  • Learning rate schedules: start with large steps and make them smaller as training progresses.

Good optimizers can train networks in hours instead of weeks, but they are not magic. If the loss landscape is full of bad local minima, the optimizer still has to deal with them.


Stability

Normalization

As a network gets deeper, the numbers passing through it can become huge or tiny. This makes gradients unstable and training slow. Normalization keeps the numbers in a healthy range.

  • Batch normalization: normalize activations across a mini-batch of examples.
  • Layer normalization: normalize across the features for one example. Used in transformers.

Normalization also has a small side effect: it acts like regularization, because the statistics change slightly from batch to batch, preventing the network from memorizing exact numbers.


Interactive

Overfitting and regularization

A model with enough parameters can memorize every training point. That gives a perfect training score but a terrible test score. This is called overfitting.

The widget below shows 10 data points from a quadratic pattern with noise. Try increasing the polynomial degree: a low degree underfits, a high degree overfits, and the right degree plus a little regularization captures the pattern.

2
0.00
0.8
Train loss—
Test loss—
Best degree—

Code

Training with weight decay

Tricks like weight decay help a model generalize. This PyTorch example adds a small penalty to large weights while it trains.


Check yourself
?

Why is a very high-degree polynomial usually bad?

Even if it passes through or near every training point, what happens when it sees a new point?

High-degree polynomials can wiggle wildly between points to fit the noise. On new data, those wild swings produce large errors. Regularization or a simpler model keeps the curve smooth and generalizes better.


Quick quiz

Three questions

1. What problem does ReLU solve compared to sigmoid?

2. What is overfitting?

3. What does regularization do?

Keep learning

Once a model trains well, the next question is how to make it fast. Modern AI systems use sparse experts, clever attention tricks, and efficient inference. That is where efficient models come in.

Part of the AI Explained tutorial series.