Implementing Early Stopping in TensorFlow to Prevent Overfitting

Introduction

Welcome back! In our previous lessons, we preprocessed the Iris dataset and built a multi-class classification model using TensorFlow. Now, we're going to explore the concept of Early Stopping and learn how to implement it in TensorFlow.

In machine learning, early stopping is a form of regularization that helps us prevent overfitting by stopping the training process once the model's performance on validation data starts showing signs of degradation. The goal of this lesson is to provide you with a deeper understanding of early stopping and guide you step-by-step on how to include Early Stopping in your model training process using TensorFlow.

Understanding Early Stopping

Before we get into the code, it's important to understand what early stopping is and why it is vital.

Overfitting occurs when a model performs exceptionally well on the training data but fails to generalize well to unseen data. In other words, it has learned the training data too well, including its noise and outliers aspects. On the contrary, underfitting is when the model does not perform well even on the training data because it has not learned the underlying pattern of the data.

Early stopping provides a straightforward solution to overfitting by keeping a tab on the model's performance on the validation data during model training. If it sees the model's performance degrading (indicating overfitting), it stops the training process. This technique prevents the model from learning the training data’s noise and outliers too precisely, which results in a robust model that can generalize well to unseen data.

Recap: Loading Data and Defining the Model

Before we dive into implementing early stopping in our model, let's quickly recap the steps we took to preprocess, load our data and define the model in the previous lessons. Here’s the code we used to preprocess the Iris dataset:

Python
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, OneHotEncoder

def load_preprocessed_data():
    # Load the Iris dataset
    iris = load_iris()
    X, y = iris.data, iris.target

    # Split the dataset into training and testing sets
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, stratify=y, random_state=42)

    # Scale the features
    scaler = StandardScaler().fit(X_train)
    X_train_scaled = scaler.transform(X_train)
    X_test_scaled = scaler.transform(X_test)

    # One-hot encode the targets
    encoder = OneHotEncoder(sparse_output=False).fit(y_train.reshape(-1, 1))
    y_train_encoded = encoder.transform(y_train.reshape(-1, 1))
    y_test_encoded = encoder.transform(y_test.reshape(-1, 1))

    return X_train_scaled, X_test_scaled, y_train_encoded, y_test_encoded

Following that, we loaded the data and defined a model designed to fit it:

Python
import tensorflow as tf
from data_preprocessing import load_preprocessed_data

# Load preprocessed data
X_train, X_test, y_train, y_test = load_preprocessed_data()

# Define the model
model = tf.keras.Sequential([
    tf.keras.layers.Input(shape=(4,)),
    tf.keras.layers.Dense(10, activation='relu'),
    tf.keras.layers.Dense(10, activation='relu'),
    tf.keras.layers.Dense(3, activation='softmax')
])

# Compile the model
model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

In summary:

  1. We started by loading the preprocessed data using our custom function load_preprocessed_data() to obtain our training and testing datasets: X_train, X_test, y_train, and y_test.
  2. We defined a sequential model using TensorFlow's Keras API, with input shapes matching our dataset and various dense layers featuring ReLU and Softmax activations.
  3. The model was compiled with the Adam optimizer and categorical crossentropy loss function, and we included accuracy as a metric.

Now that we are refreshed on the data loading and model definition steps, let's proceed to implementing early stopping in TensorFlow.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal