Regularization in Deep Learning with Python Code

shubham.jain Last Updated : 03 Dec, 2024
12 min read

One of the most common challenges encountered by data science professionals is the issue of overfitting. Have you ever experienced a scenario where your machine learning model or regression model excelled on the training data but struggled to make accurate predictions on unseen test data? This is often a trade-off between high bias (underfitting) and high variance (overfitting), impacting the model’s ability to generalize effectively. Overfitting can lead to poor performance on new data, especially in the presence of outliers or noise in the training set. If so, this article addresses your concerns by exploring techniques such as regularization in deep learning and essential concepts of bagging, boosting, and stacking in ensemble learning to improve model generalization.

Avoiding overfitting can single-handedly improve our model’s performance.

In this article, you will delve into the concept of regularization in neural networks, discover different regularization techniques, and understand how these approaches can greatly enhance model performance while preventing overfitting.

Note: This article assumes that you have basic knowledge of neural networks and their implementation in Keras. If not, you can refer to the below articles first

What is Regularization?

Regularization is a technique used in machine learning and deep learning to prevent overfitting and improve a model’s generalization performance. It involves adding a penalty term to the loss function during training.

This penalty discourages the model from becoming too complex or having large parameter values, which helps in controlling the model’s ability to fit noise in the training data. Regularization in deep learning methods includes L1 and L2 regularization, dropout, early stopping, and more. By applying regularization for deep learning, models become more robust and better at making accurate predictions on unseen data.

Also Read: Regularization in Machine Learning

Before we deep dive into the topic, take a look at this image:

Regularization in deep learning

Have you seen this image before? As we move towards the right in this image, our model tries to learn too well the details and the noise from the training data, ultimately resulting in poor performance on the unseen data.

In other words, while going toward the right, the complexity of the model increases such that the training error reduces but the testing error doesn’t. This is shown in the image below:

training vs. Test Set Error

If you’ve built a neural network before, you know how complex they are. This makes them more prone to overfitting.

regularization in deep learning

Regularization is a technique that modifies the learning algorithm slightly so that the model generalizes better. This, in turn, improves the model’s performance on unseen data as well.

How does Regularization help Reduce Overfitting?

Let’s consider a neural network that is overfitting on the training data as shown in the image below:

Regularization reduce overfitting

If you have studied the concept of regularization in machine learning, you will have a fair idea that regularization penalizes the coefficients. In deep learning, it penalizes the weight matrices of the nodes.

Assume that our regularization coefficient is so high that some of the weight matrices are nearly equal to zero.

Under- fitting


This will result in a much simpler linear network and slight underfitting of the training data.

Such a large value of the regularization coefficient is not that useful. We need to optimize the value of the regularization coefficient to obtain a well-fitted model as shown in the image below:

Appropriate- fitting

Also Read: How to avoid Over-fitting using Regularization?

Different Regularization Techniques in Deep Learning

Now that we understand how regularization helps reduce overfitting, we’ll learn a few different techniques for applying regularization in deep learning.

L2&L1 Regularization

L1 and L2 are the most common types of regularization deep learning. These update the general cost function by adding another term known as the regularization term.

  • Cost function = Loss (say, binary cross entropy) + Regularization term

Due to the addition of this regularization term, the values of weight matrices decrease because it assumes that a neural network with smaller weight matrices leads to simpler models. Therefore, it will also reduce overfitting to quite an extent.

However, this regularization term differs in L1 and L2.

For L2:

What is Regularization?

For L1:

What is Regularization?

In L2, we have: ||w||^2 = Σ w_i^2. This is known as ridge regression, where lambda is the regularization parameter. It is the hyperparameter whose value is optimized for better results. L2 regularization is also known as weight decay as it forces the weights to decay towards zero (but not exactly zero).

In L1, we having: ||w||=Σ |w_i|. In this, we penalize the absolute value of the weights. Unlike L2, the weights may be reduced to zero here. L1 regularization is also called lasso regression. Hence, it is very useful when we are trying to compress our model. Otherwise, we usually prefer L2 over it.

In keras, we can directly apply regularization for deep learning to any layer using the regularizers.

Below is the sample code to apply L2 regularization to a Dense layer:

from keras import regularizers

model.add(Dense(64, input_dim=64,
                kernel_regularizer=regularizers.l2(0.01)))

Note: Here the value 0.01 is the value of regularization parameter, i.e., lambda, which we need to optimize further. We can optimize it using the grid-search method.

Similarly, we can also apply L1 regularization deep learning. We will look at this in more detail in a case study later in this article.

Dropout

This is one of the most interesting types of regularization techniques. It also produces very good results and is consequently the most frequently used regularization technique in the field of deep learning.

To understand dropout, let’s say our neural network structure is akin to the one shown below:

regularization in deep learning

So what does dropout do? At every iteration, it randomly selects some nodes and removes them along with all of their incoming and outgoing connections as shown below:

regularization in deep learning

Each iteration has a different set of nodes, which results in a different set of outputs. This can also be thought of as an ensemble technique in machine learning.

Ensemble models usually perform better than a single model as they capture more randomness. Similarly, dropout models also perform better than normal neural network models.

This probability of choosing how many nodes should be dropped is the hyperparameter of the dropout function. As seen in the image above, dropout can be applied to both the hidden layers as well as the input layers.

nodes , regularization in deep learning
Source: chatbotslife

Due to these reasons, dropout is usually preferred when we have a large neural network structure to introduce more randomness.

In Keras, we can implement dropout using the Keras core layer. Below is the Python code for it:

from keras.layers.core import Dropout

model = Sequential([
 Dense(output_dim=hidden1_num_units, input_dim=input_num_units, activation='relu'),
 Dropout(0.25),

Dense(output_dim=output_num_units, input_dim=hidden5_num_units, activation='softmax'),
 ])

As you can see, we have defined 0.25 as the probability of dropping. We can tune it further for better results using the grid search method.

Data Augmentation

The simplest way to reduce overfitting is to increase the training data size. In machine learning, however, increasing the training data size was impossible as the labeled data was too costly.

But now, let’s consider we are dealing with images. In this case, there are a few ways of increasing the size of the training data—rotating the image, flipping, scaling, shifting, etc. In the image below, some transformation has been done on the handwritten digits dataset.

Data Augmentation , regularization in deep learning

This technique is known as data augmentation. It usually provides a big leap in improving the accuracy of the model, and it can be considered a mandatory trick to improve our predictions.

In keras, we can perform all of these transformations using ImageDataGenerator. It has a big list of arguments that you can use to pre-process your training data.

Below is the sample code to implement it:

from keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(horizontal flip=True)
datagen.fit(train)

Early Stopping

Early stopping is a cross-validation strategy in which we keep one part of the training set as the validation set. When we see that the performance on the validation set is getting worse, we immediately stop the training on the model.

cross - validation

In the above image, we will stop training at the dotted line since, after that, our model will start overfitting on the training data.

In keras, we can apply early stopping using the callbacks function. Below is the sample code for it.

from keras.callbacks import EarlyStopping

EarlyStopping(monitor='val_err', patience=5)

Here, monitor refers to the quantity that you need to keep track of, and ‘val_err’ refers to the validation error.

Patience denotes the number of epochs with no further improvement, after which training stops. For a better understanding, let’s look at the above image again. After the dotted line, each epoch will result in a higher validation error value. Therefore, our model will stop 5 epochs after the dotted line (since our patience equals 5) because it sees no further improvement

Note: After 5 epochs (the value generally defined for patience), the model might start improving again, and the validation error may decrease. Therefore, we need to take extra care while tuning this hyperparameter.

Also Read: Prevent Overfitting Using Regularization Techniques

A Case Study on MNIST Data with Keras

By this point, you should theoretically understand the different techniques we have gone through. We will now apply this knowledge to our deep learning practice problem – Identify the digits. Once you have downloaded the dataset, start following the below code! First, we’ll import some of the basic libraries.

%pylab inline

import numpy as np
import pandas as pd
from scipy.misc import imread
from sklearn.metrics import accuracy_score

from matplotlib import pyplot

import tensorflow as tf
import keras

# To stop potential randomness
seed = 128
rng = np.random.RandomState(seed)

Now, let’s load the dataset.

root_dir = os.path.abspath('/Users/shubhamjain/Downloads/AV/identify the digits/')
data_dir = os.path.join(root_dir, 'data')
sub_dir = os.path.join(root_dir, 'sub')

## reading train file only
train = pd.read_csv(os.path.join(data_dir, 'Train', 'train.csv'))
train.head()

Output:

A Case Study on MNIST Data with keras

Take a look at some of our images now.

img_name = rng.choice(train.filename)

filepath = os.path.join(data_dir, 'Train', 'Images', 'train', img_name)

img = imread(filepath, flatten=True)

pylab.imshow(img, cmap='gray')
pylab.axis('off')
pylab.show()
A Case Study on MNIST Data with keras
#storing images in numpy arrays
temp = []
for img_name in train.filename:
 image_path = os.path.join(data_dir, 'Train', 'Images', 'train', img_name)
 img = imread(image_path, flatten=True)
 img = img.astype('float32')
 temp.append(img)
 
x_train = np.stack(temp)

x_train /= 255.0
x_train = x_train.reshape(-1, 784).astype('float32')

y_train = keras.utils.np_utils.to_categorical(train.label.values)

Create a validation dataset to optimize our model for better scores. We will use a 70:30 train-validation dataset ratio.

split_size = int(x_train.shape[0]*0.7)

x_train, x_test = x_train[:split_size], x_train[split_size:]
y_train, y_test = y_train[:split_size], y_train[split_size:]

First, let’s build a simple neural network with 5 hidden layers, each with 500 nodes.

# import keras modules
from keras.models import Sequential
from keras.layers import Dense

# define vars
input_num_units = 784
hidden1_num_units = 500
hidden2_num_units = 500
hidden3_num_units = 500
hidden4_num_units = 500
hidden5_num_units = 500
output_num_units = 10

epochs = 10
batch_size = 128

model = Sequential([
 Dense(output_dim=hidden1_num_units, input_dim=input_num_units, activation='relu'),
 Dense(output_dim=hidden2_num_units, input_dim=hidden1_num_units, activation='relu'),
 Dense(output_dim=hidden3_num_units, input_dim=hidden2_num_units, activation='relu'),
 Dense(output_dim=hidden4_num_units, input_dim=hidden3_num_units, activation='relu'),
 Dense(output_dim=hidden5_num_units, input_dim=hidden4_num_units, activation='relu'),

Dense(output_dim=output_num_units, input_dim=hidden5_num_units, activation='softmax'),
 ])

Also Read: Train-Test-Validation Split in 2024

Evaluating the Model

Note that we are just running it for 10 epochs. Let’s quickly check the performance of our model.

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

trained_model_5d = model.fit(x_train, y_train, nb_epoch=epochs, batch_size=batch_size, validation_data=(x_test, y_test))

Output:

Evaluating the Model | regularization in deep learning

Now, let’s try the L2 regularizer over it and check whether it gives better results than a simple neural network model.

from keras import regularizers

model = Sequential([
 Dense(output_dim=hidden1_num_units, input_dim=input_num_units, activation='relu',
 kernel_regularizer=regularizers.l2(0.0001)),
 Dense(output_dim=hidden2_num_units, input_dim=hidden1_num_units, activation='relu',
 kernel_regularizer=regularizers.l2(0.0001)),
 Dense(output_dim=hidden3_num_units, input_dim=hidden2_num_units, activation='relu',
 kernel_regularizer=regularizers.l2(0.0001)),
 Dense(output_dim=hidden4_num_units, input_dim=hidden3_num_units, activation='relu',
 kernel_regularizer=regularizers.l2(0.0001)),
 Dense(output_dim=hidden5_num_units, input_dim=hidden4_num_units, activation='relu',
 kernel_regularizer=regularizers.l2(0.0001)),

Dense(output_dim=output_num_units, input_dim=hidden5_num_units, activation='softmax'),
 ])

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

trained_model_5d = model.fit(x_train, y_train, nb_epoch=epochs, batch_size=batch_size, validation_data=(x_test, y_test))

Output:

Evaluating the Model

Note that the value of lambda is equal to 0.0001. Great! We just obtained an accuracy that is greater than our previous NN model.

Now, let’s try the L1 regularization technique.

## l1

model = Sequential([
 Dense(output_dim=hidden1_num_units, input_dim=input_num_units, activation='relu',
 kernel_regularizer=regularizers.l1(0.0001)),
 Dense(output_dim=hidden2_num_units, input_dim=hidden1_num_units, activation='relu',
 kernel_regularizer=regularizers.l1(0.0001)),
 Dense(output_dim=hidden3_num_units, input_dim=hidden2_num_units, activation='relu',
 kernel_regularizer=regularizers.l1(0.0001)),
 Dense(output_dim=hidden4_num_units, input_dim=hidden3_num_units, activation='relu',
 kernel_regularizer=regularizers.l1(0.0001)),
 Dense(output_dim=hidden5_num_units, input_dim=hidden4_num_units, activation='relu',
 kernel_regularizer=regularizers.l1(0.0001)),

Dense(output_dim=output_num_units, input_dim=hidden5_num_units, activation='softmax'),
 ])

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
trained_model_5d = model.fit(x_train, y_train, nb_epoch=epochs, batch_size=batch_size, validation_data=(x_test, y_test))

Output:

Evaluating the Model | regularization in deep learning

This doesn’t show any improvement over the previous model. Let’s jump to the dropout technique.

## dropout

from keras.layers.core import Dropout
model = Sequential([
 Dense(output_dim=hidden1_num_units, input_dim=input_num_units, activation='relu'),
 Dropout(0.25),
 Dense(output_dim=hidden2_num_units, input_dim=hidden1_num_units, activation='relu'),
 Dropout(0.25),
 Dense(output_dim=hidden3_num_units, input_dim=hidden2_num_units, activation='relu'),
 Dropout(0.25),
 Dense(output_dim=hidden4_num_units, input_dim=hidden3_num_units, activation='relu'),
 Dropout(0.25),
 Dense(output_dim=hidden5_num_units, input_dim=hidden4_num_units, activation='relu'),
 Dropout(0.25),

Dense(output_dim=output_num_units, input_dim=hidden5_num_units, activation='softmax'),
 ])

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
trained_model_5d = model.fit(x_train, y_train, nb_epoch=epochs, batch_size=batch_size, validation_data=(x_test, y_test))
## dropout

from keras.layers.core import Dropout
model = Sequential([
 Dense(output_dim=hidden1_num_units, input_dim=input_num_units, activation='relu'),
 Dropout(0.25),
 Dense(output_dim=hidden2_num_units, input_dim=hidden1_num_units, activation='relu'),
 Dropout(0.25),
 Dense(output_dim=hidden3_num_units, input_dim=hidden2_num_units, activation='relu'),
 Dropout(0.25),
 Dense(output_dim=hidden4_num_units, input_dim=hidden3_num_units, activation='relu'),
 Dropout(0.25),
 Dense(output_dim=hidden5_num_units, input_dim=hidden4_num_units, activation='relu'),
 Dropout(0.25),

Dense(output_dim=output_num_units, input_dim=hidden5_num_units, activation='softmax'),
 ])
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
trained_model_5d = model.fit(x_train, y_train, nb_epoch=epochs, batch_size=batch_size, validation_data=(x_test, y_test))

Output:

Evaluating the Model

Not bad! Dropout also gives us a little improvement over our simple NN model.

Now, let’s try data augmentation.

from keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(zca_whitening=True)

# loading data
train = pd.read_csv(os.path.join(data_dir, 'Train', 'train.csv'))

temp = []
for img_name in train.filename:
 image_path = os.path.join(data_dir, 'Train', 'Images', 'train', img_name)
 img = imread(image_path, flatten=True)
 img = img.astype('float32')
 temp.append(img)
 
x_train = np.stack(temp)

X_train = x_train.reshape(x_train.shape[0], 1, 28, 28)

X_train = X_train.astype('float32')

Now, fit the training data in order to augment.

# fit parameters from data
datagen.fit(X_train)

Here, I have used zca_whitening as the argument, which highlights the outline of each digit as shown in the image below:

Evaluating the Model | regularization in deep learning
## splitting
y_train = keras.utils.np_utils.to_categorical(train.label.values)
split_size = int(x_train.shape[0]*0.7)

x_train, x_test = X_train[:split_size], X_train[split_size:]
y_train, y_test = y_train[:split_size], y_train[split_size:]
## reshaping
x_train=np.reshape(x_train,(x_train.shape[0],-1))/255
x_test=np.reshape(x_test,(x_test.shape[0],-1))/255
## structure using dropout
from keras.layers.core import Dropout
model = Sequential([
 Dense(output_dim=hidden1_num_units, input_dim=input_num_units, activation='relu'),
 Dropout(0.25),
 Dense(output_dim=hidden2_num_units, input_dim=hidden1_num_units, activation='relu'),
 Dropout(0.25),
 Dense(output_dim=hidden3_num_units, input_dim=hidden2_num_units, activation='relu'),
 Dropout(0.25),
 Dense(output_dim=hidden4_num_units, input_dim=hidden3_num_units, activation='relu'),
 Dropout(0.25),
 Dense(output_dim=hidden5_num_units, input_dim=hidden4_num_units, activation='relu'),
 Dropout(0.25),

Dense(output_dim=output_num_units, input_dim=hidden5_num_units, activation='softmax'),
 ])
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
trained_model_5d = model.fit(x_train, y_train, nb_epoch=epochs, batch_size=batch_size, validation_data=(x_test, y_test))

Output:

Evaluating the Model | regularization in deep learning

Wow! We made a big leap in the accuracy score. The good thing is that it works every time. We just need to select a proper argument based on the images in our dataset.

Now, let’s try our final technique – early stopping.

from keras.callbacks import EarlyStopping

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
trained_model_5d = model.fit(x_train, y_train, nb_epoch=epochs, batch_size=batch_size, validation_data=(x_test, y_test)
 , callbacks = [EarlyStopping(monitor='val_acc', patience=2)])

Output:

Evaluating the Model | regularization in deep learning

Our model stops after only 5 iterations as the validation accuracy does not improve. It gives good results in cases where we run it for a larger number of epochs, so it’s a technique to optimize the number of epochs.

Also Read: Techniques To Prevent Overfitting In Neural Networks

Conclusion

I hope you now understand regularization deep learning techniques in deep learning and the different methods required to implement them, not just for linear regression models but also for complex neural networks like CNN. Regularization is crucial when training deep learning models on large datasets to prevent overfitting and improve generalization. You can effectively employ techniques like L1/L2 regularization, dropout, data augmentation, and early stopping using gradient descent optimization and appropriate tuning of hyperparameters like the learning rate.

Elastic net regularization, which combines L1 and L2 penalties, is another powerful approach worth exploring. I highly recommend applying these regularization deep learning methods when dealing with a deep learning task, as they will help you expand your horizons, better understand the topic, and build more robust machine learning algorithms.

Hope you like the article and better understand regularization in deep learning. Regularisation techniques, such as L1 and L2 regularization, dropout, and data augmentation, prevent overfitting, and improve model performance.

Frequently Asked Questions

Q1. What is regularization in deep learning?

A. Regularization in deep learning is a technique used to prevent overfitting and improve neural network generalization. It involves adding a regularization term to the loss function, which penalizes large weights or complex model architectures. Regularization methods such as L1 and L2 regularization, dropout, and batch normalization help control model complexity and improve neural network generalization to unseen data.

Q2. What is L1 regularization and L2 regularization?

A. L1 regularization, also known as Lasso regularization, is a method in deep learning that adds the sum of absolute values of the weights to the loss function. It encourages sparsity by driving some weights to zero, resulting in feature selection. L2 regularization, also called Ridge regularization, adds the sum of squared weights to the loss function, promoting smaller but non-zero weights and preventing extreme values.

Q3. What is dropout in neural network?

A. Dropout serves as a regularization technique in neural networks to prevent overfitting. During training, the model randomly “drops out” a subset of neurons by setting their outputs to zero with a certain probability. This forces the network to learn more robust and independent features, as it cannot rely on specific neurons. Dropout improves generalization and reduces the risk of overfitting.

Q4.What is L1 and L2 regularization technique?

L1 and L2 regularization prevent overfitting by adding penalties to model complexity:
L1 (Lasso): Reduces some coefficients to zero, helping with feature selection.
L2 (Ridge): Shrinks all coefficients but keeps them non-zero, reducing their impact without eliminating features.

Responses From Readers

Clear

Pramod
Pramod

Thanks a lot bro. Immensely helpful.

Chetan
Chetan

Could we achieve the same validation accuracy with much lesser architecture ? If so, then our Hyperparameters (especially the L2 lambda) must be quite high (say 0.05). Isn't it ? Or, is there any other way of combating this ??

Florido Meacci
Florido Meacci

Thank you for the great article. I'm trying to differentiate between different font weigths of the same font and have been hitting a wall. Was wondering if you could share some insights and if you have had experience with this? What would you recommended as for the best settings, and how big of a dataset would you recommend.

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details