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

Solving Multiple Classification use cases Using H2O
In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.

Human Activity Recognition Using Multiclass Classification in Python
In this human activity recognition project, we use multiclass classification machine learning techniques to analyse fitness dataset from a smartphone tracker.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

Sequence Classification with LSTM RNN in Python with Keras
In this project, we are going to work on Sequence to Sequence Prediction using IMDB Movie Review Dataset​ using Keras in Python.

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.