How to add regularization to regression in keras?

This recipe helps you add regularization to regression in keras

Recipe Objective

Adding regularization in keras

Regularization generally reduces the overfitting of a model, it helps the model to generalize. It penalizes the model for having more weightage. There are two types of regularization parameters:- * L1 (Lasso) * L2 (Ridge) We will consider L1 for our example.

Learn About the Application of ARCH and GARCH models in Real-World

Step-1 Importing Libraries.

from sklearn.datasets import make_circles from keras.layers import Dense from keras.models import Sequential from keras.regularizers import l1 from keras.layers import Activation

Step 2- Prepare the Dataset.

# generate 2d classification dataset X, y = make_circles(n_samples=50) # split into train and test train = 15 X_train, X_test = X[:train, :], X[train:, :] y_train, y_test = y[:train], y[train:]

Step 3- Creating a Neural Network model.

We will create the Neural Network model and add the Regularizer in the input layer.

# define model model = Sequential() model.add(Dense(312, input_dim=2, activation='linear', activity_regularizer=l1(0.01))) model.add(Activation('relu')) model.add(Dense(2, activation='relu')) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['mse']) model.summary()

Model: "sequential_2"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_4 (Dense)              (None, 312)               936       
_________________________________________________________________
activation_2 (Activation)    (None, 312)               0         
_________________________________________________________________
dense_5 (Dense)              (None, 2)                 626       
=================================================================
Total params: 1,562
Trainable params: 1,562
Non-trainable params: 0

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Build Real Estate Price Prediction Model with NLP and FastAPI
In this Real Estate Price Prediction Project, you will learn to build a real estate price prediction machine learning model and deploy it on Heroku using FastAPI Framework.

Build Classification Algorithms for Digital Transformation[Banking]
Implement a machine learning approach using various classification techniques in Python to examine the digitalisation process of bank customers.

Recommender System Machine Learning Project for Beginners-2
Recommender System Machine Learning Project for Beginners Part 2- Learn how to build a recommender system for market basket analysis using association rule mining.

MLOps AWS Project on Topic Modeling using Gunicorn Flask
In this project we will see the end-to-end machine learning development process to design, build and manage reproducible, testable, and evolvable machine learning models by using AWS

Learn to Build a Siamese Neural Network for Image Similarity
In this Deep Learning Project, you will learn how to build a siamese neural network with Keras and Tensorflow for Image Similarity.

Image Classification Model using Transfer Learning in PyTorch
In this PyTorch Project, you will build an image classification model in PyTorch using the ResNet pre-trained model.

MLOps using Azure Devops to Deploy a Classification Model
In this MLOps Azure project, you will learn how to deploy a classification machine learning model to predict the customer's license status on Azure through scalable CI/CD ML pipelines.

PyCaret Project to Build and Deploy an ML App using Streamlit
In this PyCaret Project, you will build a customer segmentation model with PyCaret and deploy the machine learning application using Streamlit.

Abstractive Text Summarization using Transformers-BART Model
Deep Learning Project to implement an Abstractive Text Summarizer using Google's Transformers-BART Model to generate news article headlines.