How to add regularization to regression in keras?

How to add regularization to regression in keras?

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.

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

Relevant Projects

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.