An epoch is one complete pass of the entire training dataset through the model. During each epoch, every training sample is processed, the model calculates the prediction error using a loss function and updates its weights through backpropagation and an optimization algorithm.
- Enable the model to gradually learn complex patterns from the training data.
- Improve model performance by refining weights after each complete pass.
- Help the optimizer converge toward an optimal solution.
- Allow monitoring of training and validation performance after every epoch.
Example: Suppose a training dataset contains 1,000 images and the batch size is 100. The model processes the dataset in 10 batches, updating its weights after each batch. After all 10 batches have been processed, one epoch is completed.

Working
The following steps illustrate how one epoch is completed during deep learning model training.
- Step 1: Split the Training Dataset The complete training dataset is divided into smaller batches based on the selected batch size.
- Step 2: Process One Batch The first batch is passed through the neural network, where the model performs a forward pass to generate predictions.
- Step 3: Calculate the Loss The predicted outputs are compared with the actual labels using a loss function to measure the prediction error.
- Step 4: Update Model Weights Using backpropagation and an optimizer such as SGD or Adam, the model updates its weights to minimize the calculated loss.
- Step 5: Repeat for All Batches The same process is repeated for each remaining batch until every sample in the training dataset has been processed once. After all batches have been processed, one epoch is completed and the next epoch begins if further training is required.
Relationship Between Epochs, Batch Size and Iterations
Training a deep learning model involves three closely related concepts: epoch, batch size and iteration. An epoch represents one complete pass through the entire training dataset, while a batch is a subset of the dataset processed at one time. An iteration refers to one weight update after processing a single batch.

As shown in the figure, consider a dataset containing 1,000 training samples:
- When the batch size is 1000, the entire dataset is processed in a single batch, resulting in 1 iteration per epoch.
- When the batch size is 200, the dataset is divided into 5 batches, resulting in 5 iterations per epoch.
- When the batch size is 100, the dataset is divided into 10 batches, resulting in 10 iterations per epoch.
- This shows that reducing the batch size increases the number of iterations required to complete one epoch, while increasing the batch size decreases the number of iterations.
- However, regardless of the batch size, one epoch is completed only after every sample in the training dataset has been processed exactly once.
Relationship: Iterations per Epoch = Total Training Samples ÷ Batch Size
Early Stopping and Learning Rate Scheduling
Selecting an appropriate number of epochs is essential for training an effective deep learning model.
- Training for too few epochs may result in underfitting, where the model fails to learn meaningful patterns from the training data.
- On the other hand, training for too many epochs can lead to overfitting, causing the model to memorize the training data and perform poorly on unseen data.
- To overcome these challenges, deep learning models commonly use Early Stopping and Learning Rate Scheduling during training.
1. Early Stopping
Early Stopping is a regularization technique that automatically stops training when the model's performance on the validation dataset stops improving. Instead of training for a fixed number of epochs, it continuously monitors a validation metric, such as validation loss or validation accuracy.
- If the validation metric does not improve for a predefined number of consecutive epochs (called patience), the training process is terminated.
- This helps prevent overfitting while reducing unnecessary computation.
- Reduces training time and computational cost.
- Automatically selects an appropriate number of epochs.
- Improves the model's ability to generalize to unseen data.
2. Learning Rate Scheduling
The learning rate determines how much the model's weights are updated after each iteration. A fixed learning rate may not produce the best results throughout training. Therefore, many deep learning models use Learning Rate Scheduling, which gradually reduces the learning rate as training progresses.
Common learning rate scheduling strategies include:
- Step Decay: Reduces the learning rate after a fixed number of epochs.
- Exponential Decay: Gradually decreases the learning rate after every epoch.
- ReduceLROnPlateau: Reduces the learning rate when the validation performance stops improving.
Implementation of Epochs in TensorFlow/Keras
In TensorFlow/Keras, the number of epochs is specified using the epochs parameter of the model.fit() function. During training, the model processes the entire training dataset once in each epoch and updates its weights to minimize the loss.
Step 1: Import Required Libraries
Import the libraries required to build, train and monitor the deep learning model.
- TensorFlow/Keras provides the APIs for creating and training neural networks.
- MNIST is used as the sample handwritten digit dataset.
- EarlyStopping is imported to automatically stop training when the validation performance no longer improves.
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.datasets import mnist
from tensorflow.keras.callbacks import EarlyStopping
Step 2: Load and Preprocess the Dataset
Here, we use the MNIST handwritten digit dataset and normalize the pixel values to improve training.
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train / 255.0
x_test = x_test / 255.0
Step 3: Create the Neural Network
Create a simple fully connected neural network for digit classification.
model = Sequential([
Flatten(input_shape=(28, 28)),
Dense(128, activation="relu"),
Dense(10, activation="softmax")
])
Step 4: Compile the Model
Compile the model using the Adam optimizer and Sparse Categorical Crossentropy loss function.
model.compile(
optimizer="adam",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"]
)
Step 5: Configure Early Stopping
Early Stopping monitors the validation loss and stops training if it does not improve for three consecutive epochs.
early_stop = EarlyStopping(
monitor="val_loss",
patience=3,
restore_best_weights=True
)
Step 6: Train the Model Using Multiple Epochs
Train the model for a maximum of 20 epochs. The model processes the entire training dataset once per epoch, updating its weights after each batch of 64 samples.
- epochs=20 sets the maximum number of training epochs.
- batch_size=64 divides the training data into batches of 64 samples for efficient weight updates.
- The EarlyStopping callback monitors the validation loss after each epoch.
- If the validation loss does not improve for 3 consecutive epochs, training stops automatically before reaching 20 epochs.
- In the output shown above, the validation loss continued to improve, so training proceeded beyond Epoch 11.
history = model.fit(
x_train,
y_train,
validation_split=0.2,
epochs=20,
batch_size=64,
callbacks=[early_stop]
)
Output:
Epoch 1/20
750/750 ━━━━━━━━━━━━━━━━━━━━ 5s 5ms/step - accuracy: 0.9089 - loss: 0.3285 - val_accuracy: 0.9502 - val_loss: 0.1776
Epoch 2/20
750/750 ━━━━━━━━━━━━━━━━━━━━ 5s 7ms/step - accuracy: 0.9567 - loss: 0.1490 - val_accuracy: 0.9603 - val_loss: 0.1357
Epoch 3/20
750/750 ━━━━━━━━━━━━━━━━━━━━ 4s 6ms/step - accuracy: 0.9697 - loss: 0.1038 - val_accuracy: 0.9660 - val_loss: 0.1150
...
Epoch 10/20
750/750 ━━━━━━━━━━━━━━━━━━━━ 3s 5ms/step - accuracy: 0.9943 - loss: 0.0220 - val_accuracy: 0.9733 - val_loss: 0.0998
Epoch 11/20
750/750 ━━━━━━━━━━━━━━━━━━━━ 4s 5ms/step - accuracy: 0.9959 - loss: 0.0170 - val_accuracy: 0.9747 - val_loss: 0.0920
You can downlaod the complete code from here.
Applications
- Image Classification: Multiple epochs help CNNs learn increasingly complex visual features for accurate image recognition.
- Object Detection: Successive epochs improve object localization and classification in detection models.
- Natural Language Processing (NLP): Transformer models refine language representations over multiple epochs for downstream NLP tasks.
- Speech Recognition: Repeated training epochs help models capture complex speech patterns and improve transcription accuracy.
- Medical Image Analysis: Deep learning models use multiple epochs to identify subtle diagnostic features in medical scans.
- Recommendation Systems: Multiple epochs improve user-item representations, resulting in more personalized recommendations.
Advantages
- Improves feature learning by repeatedly exposing the model to the training data.
- Enables better weight optimization through continuous parameter updates.
- Increases model accuracy by progressively reducing training loss.
- Works effectively with Early Stopping and Learning Rate Scheduling.
- Helps deep learning models perform better on complex datasets.
Limitations
- Can lead to overfitting when the model memorizes the training data.
- Increases training time and computational resource requirements.
- Requires careful tuning with batch size and learning rate.
- Provides minimal performance improvement after model convergence.