Find a course

Module 4: Training Networks WellLesson 8 of 1115 min

Training in Practice

Learn what epochs, batches and iterations mean, why data is split into training, validation and test sets, and how to prepare inputs and weights.

Step 1 of 15Learn

Epochs, batches and iterations

When people describe a training run, three words come up constantly.

  • A batch (or mini-batch) is a small group of training examples processed together, often 32 to 512 of them. The network does a forward pass on the whole batch, averages the loss, backpropagates and updates the weights once.
  • An iteration (or step) is one such update.
  • An epoch is one full pass through the entire training set.

So the number of iterations per epoch is the dataset size divided by the batch size. With 50,000 training images and a batch size of 100, one epoch is 500 iterations. Train for 20 epochs and the weights are updated 10,000 times, and every image has been seen 20 times.

Why not use one example at a time, or the whole dataset at once? A single example gives a very noisy gradient, and a GPU processing one example at a time sits mostly idle. The whole dataset gives a precise gradient but needs enormous memory and makes only one update per pass, which is slow progress. Mini-batches sit in the sweet spot: enough examples for a reasonably accurate gradient, small enough to fit in memory, and many updates per epoch.

Step 2 of 15Practice · Work it out

A training set has 60,000 images and the batch size is 32. How many iterations (weight updates) make up one epoch?

Step 3 of 15Diagram

Epoch, batch, iteration

One epoch over 60,000 images with batch size 32.

Epoch, batch, iterationHierarchy diagram: One epoch: all 60,000 images, Batch 1: 32 images, Batch 2: 32 images, …, Batch 1,875: 32 images.One epoch: all60,000 imagesBatch 1: 32imagesBatch 2: 32images…Batch 1,875:32 images

Tap a shape with to see more.

One epoch: all 60,000 images

Every training example is seen once.

Batch 1: 32 images

Forward pass, loss, backward pass, one weight update = one iteration.

Batch 2: 32 images
…
Batch 1,875: 32 images

60,000 ÷ 32 = 1,875 iterations per epoch.

Step 4 of 15Learn

Three flavors of gradient descent

The batch size defines three variants that you'll see named in papers and documentation.

Batch gradient descent computes the gradient on the full training set before each update. The direction is exact, but each step is expensive, and for modern datasets it doesn't fit in memory.

Stochastic gradient descent (SGD), in its original meaning, updates after every single example. Updates are cheap and frequent but very noisy; the loss zigzags.

Mini-batch gradient descent uses batches of tens to hundreds of examples. It is what practically everyone uses, and confusingly it is often still called "SGD."

The noise of mini-batches is not just a necessary evil. It helps the optimizer escape saddle points and sharp dips, and some research suggests it nudges networks toward flatter minima that generalize better. If you increase the batch size a lot, you usually need to raise the learning rate too, since each step is more reliable.

Plain SGD is rarely used alone today. Momentum keeps a running average of past gradients, so the weights build up speed in consistent directions and damp out zigzags, like a ball rolling downhill. Adam combines momentum with a per-weight step size based on how large each weight's recent gradients were. Adam with a learning rate around 0.001 is a common starting point.

Step 5 of 15Compare

Batch, stochastic and mini-batch gradient descent

AspectBatch GDStochastic GDMini-batch GD
Examples per updateAll of themOneTypically 32 to 512
Gradient qualityExactVery noisyFairly accurate
Updates per epochOneOne per exampleDataset size ÷ batch size
Hardware useNeeds huge memoryLeaves the GPU mostly idleUses the GPU efficiently
Used in practiceSmall problems onlyRarely in pure formAlmost always

Step 6 of 15Learn

Training, validation and test sets

A network that scores 99% on the data it trained on may score 70% on new data. To know how good a model really is, you must measure it on examples it never learned from. The standard approach splits the data three ways:

  • Training set (often 70–80%): the only data used to compute gradients and update weights.
  • Validation set (often 10–15%): used during development to compare settings such as learning rate, number of layers or when to stop training. These settings, chosen by you rather than learned, are called hyperparameters.
  • Test set (often 10–15%): locked away and used once, at the end, to report final performance.

Why keep validation and test separate? Every time you pick the setting that scores best on the validation set, a little information about that set leaks into your choices. After dozens of experiments, your validation score is slightly optimistic. The untouched test set gives an honest final number.

Watch out for data leakage: information from the evaluation data sneaking into training. A medical model trained on scans where the same patient appears in both training and test sets can learn to recognize the patient instead of the disease, and its test score will be misleadingly high. Split by patient, by customer, or by time when examples are related.

Step 7 of 15Practice · Sort into groups

Which data split should be used for each activity?

Tap an item, then tap its group (or drag it there).

Step 8 of 15Practice · Chart

One common way to split a dataset

Tap the slice you should evaluate on only once, at the very end.

Tap the slice that answers the question, then press Check.

One common way to split a datasetDonut chart with 3 categories: Training: 70% (70%); Validation: 15% (15%); Test: 15% (15%).70%15%15%100%Total
Illustrative split; 80/10/10 is also common, and huge datasets often use far smaller validation and test shares.
One common way to split a dataset
ItemShare of data
Training70%
Validation15%
Test15%

Step 9 of 15Learn

Preparing inputs and starting weights

Two details before training starts make a surprising difference.

Normalize the inputs. If one feature is house size in square feet (around 2,000) and another is the number of bathrooms (around 2), the first dominates the weighted sums, and gradient descent zigzags through a stretched, uneven loss surface. The usual fix is standardization: subtract each feature's mean and divide by its standard deviation, so every feature has mean 0 and standard deviation 1. Pixel values are often simply divided by 255 so they fall between 0 and 1.

Initialize the weights randomly, at the right scale. If every weight starts at zero, every neuron in a layer computes the same output and receives the same gradient, so they stay identical forever. Randomness breaks this symmetry. But the scale matters too: weights that are too large make activations explode layer after layer, and weights that are too small make them shrink to nothing. Xavier (Glorot) initialization and He initialization pick the random range based on the number of inputs to each neuron; He initialization is designed for ReLU networks. Biases are usually started at zero, which is fine because the random weights already break the symmetry.

Step 10 of 15Note

Step 11 of 15Practice · Select all that apply

Which of these are good training practices? Select all that apply.

Select all that apply.

Step 12 of 15Learn

Watching training happen

During training you track learning curves: the training loss and the validation loss (and usually accuracy) after each epoch. Reading them is a core skill.

A healthy run shows both losses falling quickly at first and then flattening. A large learning rate shows up as a jagged or rising curve. A training loss that stays flat from the very first epoch points to a bug: labels that don't match their inputs, a learning rate that is far too small, or data that was never normalized.

A handy check from the previous module: for a K-class problem, the initial cross-entropy loss should be close to ln(K), since an untrained network guesses roughly evenly. If it's far higher, the initial weights or output layer are probably wrong.

Practitioners also use a sanity check before a long run: train on a tiny subset, say 20 examples, and confirm the network can drive the training loss close to zero. If a model can't even memorize 20 examples, something in the code is broken, and no amount of extra data or epochs will fix it. Only after the tiny test works is it worth launching the full, expensive training run.

Step 13 of 15Chart

Learning curves of a healthy run

Learning curves of a healthy runLine chart with 10 categories: 1: Training loss 2.3, Validation loss 2.3; 2: Training loss 1.1, Validation loss 1.15; 3: Training loss 0.72, Validation loss 0.8; 4: Training loss 0.55, Validation loss 0.64; 5: Training loss 0.46, Validation loss 0.57; 6: Training loss 0.4, Validation loss 0.53; 7: Training loss 0.36, Validation loss 0.51; 8: Training loss 0.33, Validation loss 0.5; 9: Training loss 0.31, Validation loss 0.49; 10: Training loss 0.29, Validation loss 0.49.00.511.522.5loss12345678910epoch
Illustrative curves for a 10-class problem: both start near ln(10) ≈ 2.30, fall fast, then flatten.
Learning curves of a healthy run
epochTraining lossValidation loss
12.32.3
21.11.15
30.720.8
40.550.64
50.460.57
60.40.53
70.360.51
80.330.5
90.310.49
100.290.49

Step 14 of 15Practice · What would you do?

What would you do?

Sam trains a 10-class image classifier. After 30 epochs, the training loss is still 2.30, exactly where it started, and accuracy is about 10%. What is the most likely explanation?

Step 15 of 15Practice · Choose one

Why is training data usually shuffled before each epoch?