Section 4 (Week 4)

Xavier Initialization and Regularization

Training a deep neural network involves finding the right values for the weights: we would like to end with a model with good generalization error. Our model should perform well on the full, real-world data distribution, and should neither underfit nor overfit.

In this section, we’ll analyze two methods, initialization and regularization, and show how they help us train models more effectively.

Xavier Initialization

Last week, we discussed backpropagation and gradient descent for deep learning models. All deep learning optimization methods involve an initialization of the weight parameters.

Let’s explore the first visualization in this article to gain some intuition on the effect of different initializations.

What makes a good or bad initialization? How can different magnitudes of initializations lead to exploding and vanishing gradients?

If we initialize weights to all zeros or the same value, what problem arises?

Visualizing the effects of different initializations. (source)

The goal of Xavier Initialization is to initialize the weights such that the variance of the activations are the same across every layer. This constant variance helps prevent the gradient from exploding or vanishing.

To help derive our initialization values, we will make the following simplifying assumptions:

  • Weights and inputs are centered at zero
  • Weights and inputs are independent and identically distributed
  • Biases are initialized as zeros
  • We use the tanh() activation function, which is approximately linear with small inputs:

Let’s derive Xavier Initialization now, step by step.

Our full derivation gives us the following initialization rule, which we apply to all weights:

Xavier initialization is designed to work well with tanh or sigmoid activation functions. For ReLU activations, look into He initialization, which follows a very similar derivation.

L1 and L2 Regularization

We know that regularization encourages sparse weights (many zero values), and that regularization encourages small weight values, but why does this happen?

Let’s consider some cost function , a function of weight matrices . Let’s define the following two regularized cost functions:

Let’s derive the gradient updates for these cost functions.

The update for when using is:

The update for when using is:

What do you notice that is different between these two update rules, and how does it affect optimization? What effect does the hyperparameter have?

A histogram of weight values for an unregularized (red) and L1 regularized (blue left) and L2 regularized (blue right) network. (source)

The different effects of and regularization on the optimal parameters are an artifact of the different ways in which they change the original loss landscape. In the case of two parameters ( and ), we can visualize this.

The landscape of a two parameter loss function with L1 regularization (left) and L2 regularization (right). (source)

For a deeper dive into regularization, take a look at this longer blog post, as well as Chapter 3 of The Elements of Statistical Learning.