Epochs
When training a machine learning model, several hyperparameters play an important role in determining the model’s performance and convergence. Epochs are an essential concept in machine learning, particularly in the training of a model.
An epoch is one complete pass of the entire training dataset. During an epoch, the model processes every sample in the training data once and updates its parameters through backpropagation and gradient descent.
Importance of Epochs in Training
- Epochs define how often the model processes the entire training dataset during the learning phase.
- Training for too few epochs may result in underfitting. In this case, the model fails to capture the dominant trends in the data, leading to errors and poor performance.
- Increasing the number of epochs gives the model additional chances to learn from the data, potentially enhancing its accuracy. However, too many epochs can lead to overfitting. In this case, the model fits the training data a bit too closely and performs poorly on unseen data.
Epochs vs. Iterations vs. Batches
Understanding these concepts helps in effectively training machine learning models and optimizing their performance:
- Epochs: One complete pass of the entire training dataset.
- Iterations: Refer to how many batch updates are required to complete a single pass through the full dataset.
- Batches: A subset of the training dataset used to train the model in one iteration. The batch size controls how many data samples are used at once before updating the model’s weights.
Example
This example shows how to train a neural network model using the Diabetes dataset and visualize the training and validation loss over various epochs:
import numpy as npimport matplotlib.pyplot as pltfrom sklearn.datasets import load_diabetesfrom sklearn.model_selection import train_test_splitfrom sklearn.preprocessing import StandardScalerfrom sklearn.neural_network import MLPRegressorfrom sklearn.metrics import mean_squared_error# Load the Diabetes datasetdiabetes = load_diabetes()X, y = diabetes.data, diabetes.target# Split the loaded dataset into training and test datasetsX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)# Perform data standardizationscaler = StandardScaler()X_train = scaler.fit_transform(X_train)X_test = scaler.transform(X_test)# Build and train the neural network modelmlp = MLPRegressor(hidden_layer_sizes=(128,), max_iter=20, alpha=1e-4,solver='adam', random_state=42, learning_rate_init=0.001)train_loss = []val_loss = []for i in range(5000):mlp.partial_fit(X_train, y_train) # Each partial_fit call processes the entire training data once, simulating one epochtrain_loss.append(mean_squared_error(y_train, mlp.predict(X_train)))val_loss.append(mean_squared_error(y_test, mlp.predict(X_test)))# Find the epoch with the minimum validation lossminimum_validation_loss_epoch = np.argmin(val_loss)# Plot the training and validation loss with logarithmic scalesplt.figure(figsize=(10, 6))plt.plot(train_loss, label='Training Loss', color='blue')plt.plot(val_loss, label='Validation Loss', color='orange')plt.axvline(x=minimum_validation_loss_epoch, linestyle='--', color='red', label='Minimum Validation Loss')plt.xlabel('Epochs')plt.ylabel('Loss')plt.title('Training and Validation Loss')plt.legend()plt.grid(True)plt.yscale('log')plt.show()
The model is trained for 5000 epochs. During each epoch, the training and validation loss are recorded. The epoch with the minimum validation loss is identified to help determine the optimal number of epochs for training.
Here is the output:
This example helps in understanding how the model’s performance evolves over time and helps to determine the optimal number of epochs to prevent underfitting or overfitting.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn more on Codecademy
- Skill path
Data and Programming Foundations for AI
Learn the coding, data science, and math you need to get started as a Machine Learning or AI engineer.Includes 9 CoursesWith CertificateBeginner Friendly39 hours - Free course
Intro to Deep Learning with TensorFlow
Build basic deep learning models in TensorFlow.Intermediate4 hours