Regularization in Deep Neural Networks
In this chapter we look at the training aspects of DNNs and investigate schemes that can help us avoid overfitting.L2 regularization
This is perhaps the most common form of regularization. It can be implemented by penalizing the squared magnitude of all parameters directly in the objective. where is the hidden layer index and is the weight tensor. The L2 regularization has the intuitive interpretation of heavily penalizing peaky weight vectors and preferring diffuse weight vectors. Due to multiplicative interactions between weights and inputs this has the appealing property of encouraging the network to use all of its inputs a little rather than some of its inputs a lot. The following figure presents a computational graph of a regularized DNN.
L1 regularization
This is another relatively common form of regularization, where for each weight we add the term to the objective. The L1 regularization has the intriguing property that it leads the weight vectors to become sparse during optimization (i.e. exactly zero). In other words, neurons with L1 regularization end up using only a sparse subset of their most important inputs and become nearly invariant to the โnoisyโ inputs. In comparison, final weight vectors from L2 regularization are usually diffuse, small numbers. In practice, if you are not concerned with explicit feature selection, L2 regularization can be expected to give superior performance over L1. Tools that do model size optimization (e.g. quantization of the model parameters) are typically involved and close to zero parameters are eliminated.Example of applying L2 regularization
In the CNN MNIST example below notice that the application of L2 regualization is simply done by adding theweight_decay parameter to the optimizer.
Code
Code
Code
Code
Dropout
This is an extremely effective, simple regularization technique by Srivastava et al. in Dropout: A Simple Way to Prevent Neural Networks from Overfitting that complements the other methods (L1, L2). While training, dropout is implemented by only keeping a neuron active with some probability (a hyperparameter), or setting it to zero otherwise.
Code
Code
train_step function we have performed dropout twice: on the first hidden layer and on the second hidden layer. It is also possible to perform dropout right on the input layer, in which case we would also create a binary mask for the input . The backward pass remains unchanged, but of course has to take into account the generated masks .
Crucially, note that in the predict function we are not dropping anymore, but we are performing a scaling of both hidden layer outputs by . This is important because at test time all neurons see all their inputs, so we want the outputs of neurons at test time to be identical to their expected outputs at training time. For example, in case of , the neurons must halve their outputs at test time to have the same output as they had during training time (in expectation). To see this, consider an output of a neuron before dropout, lets call it . With dropout, the expected output from this neuron will become , because the neuronโs output will be set to zero with probability . At test time, when we keep the neuron always active, we must adjust to keep the same expected output.
It can also be shown that performing this attenuation at test time can be related to the process of iterating over all the possible binary masks (and therefore all the exponentially many sub-networks) and computing their ensemble prediction.
Code
Code
Code
Code
Early stopping
In these notes we focused on approaches that have some conceptual depth. We avoid treating extensively techniques that belong to the category of experiment management. For example early stopping is based on the experiment manager that is monitoring the validation loss and stops training when it observes that the validation error increased while at the same time retrieves the best model that has been trained to the data scientist. This does not stop the approach being one of the most popular regularization approaches as it can be seen as an L2 regularizer as shown below.
Code
Code
PyTorch reference
Found an error on this page? Report an issue.

