Neural Network L2 Regularization using Python

I wrote an article titled “Neural Network L2 Regularization using Python” in the September 2017 issue of Visual Studio Magazine. See

You can think of a neural network as a complicated mathematical prediction equation. To compute the constants (called weights and biases) that determine the behavior of the equation, you use a set of training data. The training data has known, correct input and output values. You use an algorithm (most often back-propagation) to find values for the NN constants so that computed output values closely match the correct output values in the training data.

A challenge when training a NN is called over-fitting. If you train a network too well, you will get very low error or equivalently, high accuracy) on the training data. But when you apply your NN model to new, previously unseen data, your accuracy is very low.

There are several ways to try and limit NN over-fitting. One technique is called regularization. As it turns out, an over-fitted NN model often has constants that are very large in magnitude. Regularization keeps the values of the NN constants small.

There are two main forms of regularization, L1 and L2. L1 regularization penalizes the sum of the magnitudes of all the NN weights. L2 regularization penalizes the sum of the squared weights. My article explains exactly how L2 regularization works, and compares L1 and L2 regularization.

From a developer’s point, L2 regularization generally (but not always) works a bit better than L1 regularization. And L2 is a tiny bit easier to implement than L1. But L1 sometimes (but not always) automatically prunes away irrelevant predictor variables by setting their associated weight constants to zero.

As often with machine learning, working with regularization is part art and part science and part intuition and part experience.

“A Perfect Fit” (1863) – Luis Ruiperez

This entry was posted in Machine Learning. Bookmark the permalink.

2 Responses to Neural Network L2 Regularization using Python

  1. Peter Boos says:

    Could you please provide a link to the source code of time series regression.
    On your recent MSDN article, the source code links on top of article dont work.
    Your un-stop-able coding, and your readers would like to keep up with you 😉

Comments are closed.