How to add regularization to regression in keras?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

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

Churn Prediction in Telecom using Machine Learning in R
Estimating churners before they discontinue using a product or service is extremely important. In this ML project, you will develop a churn prediction model in telecom to predict customers who are most likely subject to churn.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Build a Similar Images Finder with Python, Keras, and Tensorflow
Build your own image similarity application using Python to search and find images of products that are similar to any given product. You will implement the K-Nearest Neighbor algorithm to find products with maximum similarity.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

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.

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

NLP and Deep Learning For Fake News Classification in Python
In this project you will use Python to implement various machine learning methods( RNN, LSTM, GRU) for fake news classification.

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.