Lensa ML
Lensa ML

Epochs & Batching

Step 1 of 6

What Is an Epoch?

One complete pass through the entire dataset

Dataset (16 samples)12345678910111213141516
Jump to sampleSample 1
EPOCH
1
CURRENT SAMPLE
1 / 16
TOTAL SEEN
1

An epoch is one complete pass through every sample in the dataset. The scanner processes each sample once. After all 16 samples are seen, epoch 1 is complete and the next one begins.

Step 1 of 6: What Is an Epoch?

One complete pass through the entire dataset

Dataset (16 samples)12345678910111213141516
Jump to sampleSample 1
EPOCH
1
CURRENT SAMPLE
1 / 16
TOTAL SEEN
1

An epoch is one complete pass through every sample in the dataset. The scanner processes each sample once. After all 16 samples are seen, epoch 1 is complete and the next one begins.

Step 2 of 6: Stochastic Gradient Descent

Updating weights one sample at a time

minSGD (batch=1)
Learning rate0.080
STEP
0
LOSS
15.813
GRAD MAG
23.412

Stochastic Gradient Descent updates weights after each single sample. The gradient estimate is noisy because one sample cannot represent the full dataset. Notice the jittery, zigzag path -- each update pulls in a slightly different direction.

Step 3 of 6: Mini-batch Gradient Descent

The practical middle ground between SGD and full-batch

minBatch = 4
Batch size4
Learning rate0.080
STEP
0
LOSS
15.813
GRAD MAG
23.412
STEPS / EPOCH
16

Batch size 4 averages gradients over 4 samples per update. The path is smoother than SGD while still updating 16 times per epoch. This is the sweet spot used in practice.

Step 4 of 6: Full-batch Gradient Descent

Using the whole dataset for each update

Full-batchBatch=16
Batch size16
MEMORY
Medium
CONVERGENCE
Balanced
FINAL LOSS
0.020

Full-batch gradient descent uses every sample to compute each gradient -- the smoothest path but the slowest per-step (and most memory hungry). Compared to the faded full-batch reference, smaller batches are noisier but update weights more frequently per epoch. The tradeoff: memory vs speed.

Step 5 of 6: Shuffling Matters

Why randomizing data order improves training

EpochsLossShuffledUnshuffled
Jump to epochEpoch 1
SHUFFLED LOSS
2.200
UNSHUFFLED LOSS
2.200
EPOCH
1

Shuffling the dataset each epoch ensures that mini-batches are diverse. The model sees samples in a different order every time, preventing it from memorizing patterns in the ordering. The loss curve is smoother and converges to a lower value.

Step 6 of 6: Putting It Together

Epochs times batch size equals training

EpochsLossEpochs
Epochs10
Batch size16
TOTAL UPDATES
0
SAMPLES SEEN
0
FINAL LOSS
2.500

Training = 10 epochs x 4 steps/epoch = 40 total weight updates. Each epoch processes all 64 samples in batches of 16. Over 10 epochs, the model sees 640 total samples. This is a reasonable number of epochs for most tasks.