This course is a deep dive into the details of deep learning architectures with a focus on learning end-to-end models for these tasks, particularly image classification
This page contains my solutions and approaches for the assignment All source codes of my solutions are available on GitHub
Batch Normalization
One way to make deep networks easier to train is to use more sophisticated optimization procedures such as SGD+momentum, RMSProp, or Adam. Another strategy is to change the architecture of the network to make it easier to train. One idea along these lines is batch normalization, proposed by 1 in 2015.
To understand the goal of batch normalization, it is important to first recognize that machine learning methods tend to perform better with input data consisting of uncorrelated features with zero mean and unit variance. When training a neural network, we can preprocess the data before feeding it to the network to explicitly decorrelate its features. This will ensure that the first layer of the network sees data that follows a nice distribution. However, even if we preprocess the input data, the activations at deeper layers of the network will likely no longer be decorrelated and will no longer have zero mean or unit variance, since they are output from earlier layers in the network. Even worse, during the training process the distribution of features at each layer of the network will shift as the weights of each layer are updated.
The authors of 1 hypothesize that the shifting distribution of features inside deep neural networks may make training deep networks more difficult. To overcome this problem, they propose to insert into the network layers that normalize batches. At training time, such a layer uses a minibatch of data to estimate the mean and standard deviation of each feature. These estimated means and standard deviations are then used to center and normalize the features of the minibatch. A running average of these means and standard deviations is kept during training, and at test time these running averages are used to center and normalize features.
It is possible that this normalization strategy could reduce the representational power of the network, since it may sometimes be optimal for certain layers to have features that are not zero-mean or unit variance. To this end, the batch normalization layer includes learnable shift and scale parameters for each feature dimension.
=========== You can safely ignore the message below if you are NOT working on ConvolutionalNetworks.ipynb ===========
You will need to compile a Cython extension for a portion of this assignment.
The instructions to do this will be given in a section of the notebook below.
# Load the (preprocessed) CIFAR-10 data.data = get_CIFAR10_data()for k, v inlist(data.items()):print(f"{k}: {v.shape}")
In the file cs231n/layers.py, implement the batch normalization forward pass in the function batchnorm_forward. Once you have done so, run the following to test your implementation.
Referencing the paper linked to above in 1 may be helpful!
def batchnorm_forward(x, gamma, beta, bn_param):""" Forward pass for batch normalization. During training the sample mean and (uncorrected) sample variance are computed from minibatch statistics and used to normalize the incoming data. During training we also keep an exponentially decaying running mean of the mean and variance of each feature, and these averages are used to normalize data at test-time. At each timestep we update the running averages for mean and variance using an exponential decay based on the momentum parameter: running_mean = momentum * running_mean + (1 - momentum) * sample_mean running_var = momentum * running_var + (1 - momentum) * sample_var Note that the batch normalization paper suggests a different test-time behavior: they compute sample mean and variance for each feature using a large number of training images rather than using a running average. For this implementation we have chosen to use running averages instead since they do not require an additional estimation step; the torch7 implementation of batch normalization also uses running averages. Input: - x: Data of shape (N, D) - gamma: Scale parameter of shape (D,) - beta: Shift paremeter of shape (D,) - bn_param: Dictionary with the following keys: - mode: 'train' or 'test'; required - eps: Constant for numeric stability - momentum: Constant for running mean / variance. - running_mean: Array of shape (D,) giving running mean of features - running_var Array of shape (D,) giving running variance of features Returns a tuple of: - out: of shape (N, D) - cache: A tuple of values needed in the backward pass """ mode = bn_param["mode"] eps = bn_param.get("eps", 1e-5) momentum = bn_param.get("momentum", 0.9) N, D = x.shape running_mean = bn_param.get("running_mean", np.zeros(D, dtype=x.dtype)) running_var = bn_param.get("running_var", np.zeros(D, dtype=x.dtype)) out, cache =None, Noneif mode =="train":######################################################################## TODO: Implement the training-time forward pass for batch norm. ## Use minibatch statistics to compute the mean and variance, use ## these statistics to normalize the incoming data, and scale and ## shift the normalized data using gamma and beta. ## ## You should store the output in the variable out. Any intermediates ## that you need for the backward pass should be stored in the cache ## variable. ## ## You should also use your computed sample mean and variance together ## with the momentum variable to update the running mean and running ## variance, storing your result in the running_mean and running_var ## variables. ## ## Note that though you should be keeping track of the running ## variance, you should normalize the data based on the standard ## deviation (square root of variance) instead! ## Referencing the original paper (https://arxiv.org/abs/1502.03167) ## might prove to be helpful. ######################################################################### *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****""" my approach mini_batch_mean = np.mean(x, axis=0) mini_batch_variance = np.var(x, axis=0) normalized_x = (x-mini_batch_mean) / (np.sqrt(mini_batch_variance + eps)) scaled_and_shifted_x = normalized_x*gamma + beta out = scaled_and_shifted_x running_mean = momentum * running_mean + (1 - momentum) * mini_batch_mean running_var = momentum * running_var + (1 - momentum) * mini_batch_variance """""" more understandable approach taken from: https://kratzert.github.io/2016/02/12/understanding-the-gradient-flow-through-the-batch-normalization-layer.html two approach gives almost same results but I found Frederik's more understandable. """ N,D = x.shape#step1: calculate mean mu =1./N * np.sum(x, axis=0)#step2: substract mean from all datapoints xmu = x - mu#step3: squaring xmu from 2nd degree sq = xmu**2#step4: calculate variance var =1./N * np.sum(sq, axis=0)#step5: add eps for numerical stability, then take square-root from 2nd degree sqrtvar = np.sqrt(var + eps)#step6: invert sqrtvar ivar =1./ sqrtvar#step7: normalize the datapoints xhat = xmu * ivar#step8: scale the normalized data with gamma gammax = gamma * xhat#step9: shift the scaled data out = gammax + beta#store intermediate cache = (xhat,gamma,xmu,ivar,sqrtvar,var,eps)#calculate running means running_mean = momentum * running_mean + (1- momentum) * mu running_var = momentum * running_var + (1- momentum) * var# *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****######################################################################## END OF YOUR CODE ########################################################################elif mode =="test":######################################################################## TODO: Implement the test-time forward pass for batch normalization. ## Use the running mean and variance to normalize the incoming data, ## then scale and shift the normalized data using gamma and beta. ## Store the result in the out variable. ######################################################################### *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)***** normalized_x = (x-running_mean) / (np.sqrt(running_var + eps)) scaled_and_shifted_x = normalized_x*gamma + beta out = scaled_and_shifted_x# *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****######################################################################## END OF YOUR CODE ########################################################################else:raiseValueError('Invalid forward batchnorm mode "%s"'% mode)# Store the updated running means back into bn_param bn_param["running_mean"] = running_mean bn_param["running_var"] = running_varreturn out, cache
# Check the training-time forward pass by checking means and variances# of features both before and after batch normalization # Simulate the forward pass for a two-layer network.np.random.seed(231)N, D1, D2, D3 =200, 50, 60, 3X = np.random.randn(N, D1)W1 = np.random.randn(D1, D2)W2 = np.random.randn(D2, D3)a = np.maximum(0, X.dot(W1)).dot(W2)print('Before batch normalization:')print_mean_std(a,axis=0)gamma = np.ones((D3,))beta = np.zeros((D3,))# Means should be close to zero and stds close to one.print('After batch normalization (gamma=1, beta=0)')a_norm, _ = batchnorm_forward(a, gamma, beta, {'mode': 'train'})print_mean_std(a_norm,axis=0)gamma = np.asarray([1.0, 2.0, 3.0])beta = np.asarray([11.0, 12.0, 13.0])# Now means should be close to beta and stds close to gamma.print('After batch normalization (gamma=', gamma, ', beta=', beta, ')')a_norm, _ = batchnorm_forward(a, gamma, beta, {'mode': 'train'})print_mean_std(a_norm,axis=0)
# Check the test-time forward pass by running the training-time# forward pass many times to warm up the running averages, and then# checking the means and variances of activations after a test-time# forward pass.np.random.seed(231)N, D1, D2, D3 =200, 50, 60, 3W1 = np.random.randn(D1, D2)W2 = np.random.randn(D2, D3)bn_param = {'mode': 'train'}gamma = np.ones(D3)beta = np.zeros(D3)for t inrange(50): X = np.random.randn(N, D1) a = np.maximum(0, X.dot(W1)).dot(W2) batchnorm_forward(a, gamma, beta, bn_param)bn_param['mode'] ='test'X = np.random.randn(N, D1)a = np.maximum(0, X.dot(W1)).dot(W2)a_norm, _ = batchnorm_forward(a, gamma, beta, bn_param)# Means should be close to zero and stds close to one, but will be# noisier than training-time forward passes.print('After batch normalization (test-time):')print_mean_std(a_norm,axis=0)
Now implement the backward pass for batch normalization in the function batchnorm_backward.
To derive the backward pass you should write out the computation graph for batch normalization and backprop through each of the intermediate nodes. Some intermediates may have multiple outgoing branches; make sure to sum gradients across these branches in the backward pass.
Once you have finished, run the following to numerically check your backward pass.
def batchnorm_backward(dout, cache):""" Backward pass for batch normalization. For this implementation, you should write out a computation graph for batch normalization on paper and propagate gradients backward through intermediate nodes. Inputs: - dout: Upstream derivatives, of shape (N, D) - cache: Variable of intermediates from batchnorm_forward. Returns a tuple of: - dx: Gradient with respect to inputs x, of shape (N, D) - dgamma: Gradient with respect to scale parameter gamma, of shape (D,) - dbeta: Gradient with respect to shift parameter beta, of shape (D,) """ dx, dgamma, dbeta =None, None, None############################################################################ TODO: Implement the backward pass for batch normalization. Store the ## results in the dx, dgamma, and dbeta variables. ## Referencing the original paper (https://arxiv.org/abs/1502.03167) ## might prove to be helpful. ############################################################################# *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****""" approach taken from: https://kratzert.github.io/2016/02/12/understanding-the-gradient-flow-through-the-batch-normalization-layer.html """#unpack the cache variables xhat,gamma,xmu,ivar,sqrtvar,var,eps = cache N,D = dout.shape#step9 dbeta =1* np.sum(dout, axis=0) dgammax = dout#step8 dgamma = np.sum(dgammax*xhat, axis=0) dxhat = dgammax * gamma#step7 dxmu1 = dxhat * ivar divar = np.sum(dxhat*xmu, axis=0)#step6 dsqrtvar = divar * (-1/ sqrtvar**2)#step5 dvar =0.5* (1/ np.sqrt(var + eps)) * dsqrtvar#step4 dsq =1./ N * np.ones((N,D)) * dvar#step3 dxmu2 =2* xmu * dsq#step2 dx1 =1* (dxmu1 + dxmu2) dmu =-1* np.sum((dxmu1+dxmu2), axis=0)#step1 dx2 =1./ N * np.ones((N,D)) * dmu#step0 dx = dx1 + dx2# *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****############################################################################ END OF YOUR CODE ############################################################################return dx, dgamma, dbeta
In class we talked about two different implementations for the sigmoid backward pass. One strategy is to write out a computation graph composed of simple operations and backprop through all intermediate values. Another strategy is to work out the derivatives on paper. For example, you can derive a very simple formula for the sigmoid function’s backward pass by simplifying gradients on paper.
Surprisingly, it turns out that you can do a similar simplification for the batch normalization backward pass too!
In the forward pass, given a set of inputs \(X=\begin{bmatrix}x_1\\x_2\\...\\x_N\end{bmatrix}\),
we first calculate the mean \(\mu\) and variance \(v\). With \(\mu\) and \(v\) calculated, we can calculate the standard deviation \(\sigma\) and normalized data \(Y\). The equations and graph illustration below describe the computation (\(y_i\) is the i-th element of the vector \(Y\)).
The meat of our problem during backpropagation is to compute \(\frac{\partial L}{\partial X}\), given the upstream gradient we receive, \(\frac{\partial L}{\partial Y}.\) To do this, recall the chain rule in calculus gives us \(\frac{\partial L}{\partial X} = \frac{\partial L}{\partial Y} \cdot \frac{\partial Y}{\partial X}\).
The unknown/hard part is \(\frac{\partial Y}{\partial X}\). We can find this by first deriving step-by-step our local gradients at \(\frac{\partial v}{\partial X}\), \(\frac{\partial \mu}{\partial X}\), \(\frac{\partial \sigma}{\partial v}\), \(\frac{\partial Y}{\partial \sigma}\), and \(\frac{\partial Y}{\partial \mu}\), and then use the chain rule to compose these gradients (which appear in the form of vectors!) appropriately to compute \(\frac{\partial Y}{\partial X}\).
If it’s challenging to directly reason about the gradients over \(X\) and \(Y\) which require matrix multiplication, try reasoning about the gradients in terms of individual elements \(x_i\) and \(y_i\) first: in that case, you will need to come up with the derivations for \(\frac{\partial L}{\partial x_i}\), by relying on the Chain Rule to first calculate the intermediate \(\frac{\partial \mu}{\partial x_i}, \frac{\partial v}{\partial x_i}, \frac{\partial \sigma}{\partial x_i},\) then assemble these pieces to calculate \(\frac{\partial y_i}{\partial x_i}\).
You should make sure each of the intermediary gradient derivations are all as simplified as possible, for ease of implementation.
After doing so, implement the simplified batch normalization backward pass in the function batchnorm_backward_alt and compare the two implementations by running the following. Your two implementations should compute nearly identical results, but the alternative implementation should be a bit faster.
def batchnorm_backward_alt(dout, cache):""" Alternative backward pass for batch normalization. For this implementation you should work out the derivatives for the batch normalizaton backward pass on paper and simplify as much as possible. You should be able to derive a simple expression for the backward pass. See the jupyter notebook for more hints. Note: This implementation should expect to receive the same cache variable as batchnorm_backward, but might not use all of the values in the cache. Inputs / outputs: Same as batchnorm_backward """ dx, dgamma, dbeta =None, None, None############################################################################ TODO: Implement the backward pass for batch normalization. Store the ## results in the dx, dgamma, and dbeta variables. ## ## After computing the gradient with respect to the centered inputs, you ## should be able to compute gradients with respect to the inputs in a ## single statement; our implementation fits on a single 80-character line.############################################################################# *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****#unpack the cache variables xhat,gamma,xmu,ivar,sqrtvar,var,eps = cache N,D = dout.shape# based on formulations in the paper dxhat = dout * gamma dvar = np.sum((dxhat * xmu *-0.5* ((var+eps)**(-3/2))), axis=0) dmu = np.sum((dxhat *-ivar), axis=0) + (dvar * np.sum((-2*xmu), axis=0) / N) dx = (dxhat * ivar) + (dvar * (2*xmu / N)) + (dmu / N) dgamma = np.sum(dout * xhat, axis=0) dbeta = np.sum(dout, axis=0)# *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****############################################################################ END OF YOUR CODE ############################################################################return dx, dgamma, dbeta
Now that you have a working implementation for batch normalization, go back to your FullyConnectedNet in the file cs231n/classifiers/fc_net.py. Modify your implementation to add batch normalization.
Concretely, when the normalization flag is set to "batchnorm" in the constructor, you should insert a batch normalization layer before each ReLU nonlinearity. The outputs from the last layer of the network should not be normalized. Once you are done, run the following to gradient-check your implementation.
Hint: You might find it useful to define an additional helper layer similar to those in the file cs231n/layer_utils.py.
from builtins importrangefrom builtins importobjectimport numpy as npfrom ..layers import*from ..layer_utils import*class FullyConnectedNet(object):"""Class for a multi-layer fully connected neural network. Network contains an arbitrary number of hidden layers, ReLU nonlinearities, and a softmax loss function. This will also implement dropout and batch/layer normalization as options. For a network with L layers, the architecture will be {affine - [batch/layer norm] - relu - [dropout]} x (L - 1) - affine - softmax where batch/layer normalization and dropout are optional and the {...} block is repeated L - 1 times. Learnable parameters are stored in the self.params dictionary and will be learned using the Solver class. """def__init__(self, hidden_dims, input_dim=3*32*32, num_classes=10, dropout_keep_ratio=1, normalization=None, reg=0.0, weight_scale=1e-2, dtype=np.float32, seed=None, ):"""Initialize a new FullyConnectedNet. Inputs: - hidden_dims: A list of integers giving the size of each hidden layer. - input_dim: An integer giving the size of the input. - num_classes: An integer giving the number of classes to classify. - dropout_keep_ratio: Scalar between 0 and 1 giving dropout strength. If dropout_keep_ratio=1 then the network should not use dropout at all. - normalization: What type of normalization the network should use. Valid values are "batchnorm", "layernorm", or None for no normalization (the default). - reg: Scalar giving L2 regularization strength. - weight_scale: Scalar giving the standard deviation for random initialization of the weights. - dtype: A numpy datatype object; all computations will be performed using this datatype. float32 is faster but less accurate, so you should use float64 for numeric gradient checking. - seed: If not None, then pass this random seed to the dropout layers. This will make the dropout layers deteriminstic so we can gradient check the model. """self.normalization = normalizationself.use_dropout = dropout_keep_ratio !=1self.reg = regself.num_layers =1+len(hidden_dims)self.dtype = dtypeself.params = {}############################################################################# TODO: Initialize the parameters of the network, storing all values in ## the self.params dictionary. Store weights and biases for the first layer ## in W1 and b1; for the second layer use W2 and b2, etc. Weights should be ## initialized from a normal distribution centered at 0 with standard ## deviation equal to weight_scale. Biases should be initialized to zero. ## ## When using batch normalization, store scale and shift parameters for the ## first layer in gamma1 and beta1; for the second layer use gamma2 and ## beta2, etc. Scale parameters should be initialized to ones and shift ## parameters should be initialized to zeros. ############################################################################## *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)***** verbose =Falseif verbose: print('num_layers:', self.num_layers, '\n') len_hidden_dims =len(hidden_dims)for layer_num inrange(1, self.num_layers+1): number_of_nodes =Noneif verbose: print('layer_num:', layer_num)if layer_num ==1: # First Layerif verbose: print('\tfirst_layer')self.params[f"W{layer_num}"] = np.random.normal(0.0, weight_scale, (input_dim, hidden_dims[0]))self.params[f"b{layer_num}"] = np.zeros(hidden_dims[0], )ifself.normalization =="batchnorm":self.params[f"gamma{layer_num}"] = np.ones((hidden_dims[0], ))self.params[f"beta{layer_num}"] = np.zeros((hidden_dims[0], ))elif layer_num ==self.num_layers: #Last Layerif verbose: print('\tlast_layer')self.params[f"W{layer_num}"] = np.random.normal(0.0, weight_scale, (hidden_dims[-1], num_classes))self.params[f"b{layer_num}"] = np.zeros(num_classes, )else: # Hidden Layersif verbose: print('\thidden_layer') hidden_dim_curr = hidden_dims[layer_num-2] hidden_dim_next = hidden_dims[layer_num-1]self.params[f"W{layer_num}"] = np.random.normal(0.0, weight_scale, (hidden_dim_curr, hidden_dim_next))self.params[f"b{layer_num}"] = np.zeros(hidden_dim_next, )ifself.normalization =="batchnorm":self.params[f"gamma{layer_num}"] = np.ones((hidden_dim_next, ))self.params[f"beta{layer_num}"] = np.zeros((hidden_dim_next, ))if verbose: print(f"\tW{layer_num}:", self.params[f"W{layer_num}"].shape)print(f"\tb{layer_num}:", self.params[f"b{layer_num}"].shape)iff"gamma{layer_num}"inself.params:print(f"\tgamma{layer_num}:", self.params[f"gamma{layer_num}"].shape)print(f"\tbeta{layer_num}:", self.params[f"beta{layer_num}"].shape)# *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****############################################################################# END OF YOUR CODE ############################################################################## When using dropout we need to pass a dropout_param dictionary to each# dropout layer so that the layer knows the dropout probability and the mode# (train / test). You can pass the same dropout_param to each dropout layer.self.dropout_param = {}ifself.use_dropout:self.dropout_param = {"mode": "train", "p": dropout_keep_ratio}if seed isnotNone:self.dropout_param["seed"] = seed# With batch normalization we need to keep track of running means and# variances, so we need to pass a special bn_param object to each batch# normalization layer. You should pass self.bn_params[0] to the forward pass# of the first batch normalization layer, self.bn_params[1] to the forward# pass of the second batch normalization layer, etc.self.bn_params = []ifself.normalization =="batchnorm":self.bn_params = [{"mode": "train"} for i inrange(self.num_layers -1)]ifself.normalization =="layernorm":self.bn_params = [{} for i inrange(self.num_layers -1)]# Cast all parameters to the correct datatype.for k, v inself.params.items():self.params[k] = v.astype(dtype)def loss(self, X, y=None):"""Compute loss and gradient for the fully connected net. Inputs: - X: Array of input data of shape (N, d_1, ..., d_k) - y: Array of labels, of shape (N,). y[i] gives the label for X[i]. Returns: If y is None, then run a test-time forward pass of the model and return: - scores: Array of shape (N, C) giving classification scores, where scores[i, c] is the classification score for X[i] and class c. If y is not None, then run a training-time forward and backward pass and return a tuple of: - loss: Scalar value giving the loss - grads: Dictionary with the same keys as self.params, mapping parameter names to gradients of the loss with respect to those parameters. """ X = X.astype(self.dtype) mode ="test"if y isNoneelse"train"# Set train/test mode for batchnorm params and dropout param since they# behave differently during training and testing.ifself.use_dropout:self.dropout_param["mode"] = modeifself.normalization =="batchnorm":for bn_param inself.bn_params: bn_param["mode"] = mode scores =None############################################################################# TODO: Implement the forward pass for the fully connected net, computing ## the class scores for X and storing them in the scores variable. ## ## When using dropout, you'll need to pass self.dropout_param to each ## dropout forward pass. ## ## When using batch normalization, you'll need to pass self.bn_params[0] to ## the forward pass for the first batch normalization layer, pass ## self.bn_params[1] to the forward pass for the second batch normalization ## layer, etc. ############################################################################## *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)***** affine_cache =None bn_cache =None relu_cache =None dropout_cache =None caches = {} input_data = Xfor layer_num inrange(1, self.num_layers): weights =self.params[f"W{layer_num}"] biases =self.params[f"b{layer_num}"] temp_out, affine_cache = affine_forward(input_data, weights, biases)#batch/layer normifself.normalization =="batchnorm": x = temp_out gamma =self.params[f"gamma{layer_num}"] beta =self.params[f"beta{layer_num}"] bn_param =self.bn_params[layer_num-1] temp_out, bn_cache = batchnorm_forward(x, gamma, beta, bn_param) relu_out, relu_cache = relu_forward(temp_out)#dropout input_data = relu_out cache = (affine_cache, bn_cache, relu_cache, dropout_cache) caches[f"cache{layer_num}"] = cache layer_num =self.num_layers weights =self.params[f"W{layer_num}"] biases =self.params[f"b{layer_num}"] affine_out, affine_cache = affine_forward(input_data, weights, biases) caches[f"cache{layer_num}"] = affine_cache scores = affine_out# *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****############################################################################# END OF YOUR CODE ############################################################################## If test mode return early.if mode =="test":return scores loss, grads =0.0, {}############################################################################# TODO: Implement the backward pass for the fully connected net. Store the ## loss in the loss variable and gradients in the grads dictionary. Compute ## data loss using softmax, and make sure that grads[k] holds the gradients ## for self.params[k]. Don't forget to add L2 regularization! ## ## When using batch/layer normalization, you don't need to regularize the ## scale and shift parameters. ## ## NOTE: To ensure that your implementation matches ours and you pass the ## automated tests, make sure that your L2 regularization includes a factor ## of 0.5 to simplify the expression for the gradient. ############################################################################## *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)***** loss, dout = softmax_loss(scores, y) layer_num =self.num_layers w =self.params[f"W{layer_num}"] cache = caches[f"cache{layer_num}"] dx, dw, db = affine_backward(dout, cache) grads[f"W{layer_num}"] = dw + (self.reg * w) grads[f"b{layer_num}"] = db loss +=0.5*self.reg * (np.sum(w * w))for layer_num inrange(self.num_layers-1, 0, -1): cache = caches[f"cache{layer_num}"] w =self.params[f"W{layer_num}"] affine_cache, bn_cache, relu_cache, dropout_cache = cache temp_dout = relu_backward(dx, relu_cache)ifself.normalization =="batchnorm": temp_dout, dgamma, dbeta = batchnorm_backward_alt(temp_dout, bn_cache) dx, dw, db = affine_backward(temp_dout, affine_cache) grads[f"W{layer_num}"] = dw + (self.reg *self.params[f"W{layer_num}"]) grads[f"b{layer_num}"] = dbifself.normalization =="batchnorm": grads[f"gamma{layer_num}"] = dgamma grads[f"beta{layer_num}"] = dbeta loss +=0.5*self.reg * (np.sum(w * w))# *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****############################################################################# END OF YOUR CODE #############################################################################return loss, grads
np.random.seed(231)N, D, H1, H2, C =2, 15, 20, 30, 10X = np.random.randn(N, D)y = np.random.randint(C, size=(N,))# You should expect losses between 1e-4~1e-10 for W, # losses between 1e-08~1e-10 for b,# and losses between 1e-08~1e-09 for beta and gammas.for reg in [0, 3.14]:print('Running check with reg = ', reg) model = FullyConnectedNet([H1, H2], input_dim=D, num_classes=C, reg=reg, weight_scale=5e-2, dtype=np.float64, normalization='batchnorm') loss, grads = model.loss(X, y)print('Initial loss: ', loss)for name insorted(grads): f =lambda _: model.loss(X, y)[0] grad_num = eval_numerical_gradient(f, model.params[name], verbose=False, h=1e-5)print('%s relative error: %.2e'% (name, rel_error(grad_num, grads[name])))if reg ==0: print()
Run the following to visualize the results from two networks trained above. You should find that using batch normalization helps the network to converge much faster.
We will now run a small experiment to study the interaction of batch normalization and weight initialization.
The first cell will train eight-layer networks both with and without batch normalization using different scales for weight initialization. The second layer will plot training accuracy, validation set accuracy, and training loss as a function of the weight initialization scale.
np.random.seed(231)# Try training a very deep net with batchnorm.hidden_dims = [50, 50, 50, 50, 50, 50, 50]num_train =1000small_data = {'X_train': data['X_train'][:num_train],'y_train': data['y_train'][:num_train],'X_val': data['X_val'],'y_val': data['y_val'],}bn_solvers_ws = {}solvers_ws = {}weight_scales = np.logspace(-4, 0, num=20)for i, weight_scale inenumerate(weight_scales):print('Running weight scale %d / %d'% (i +1, len(weight_scales))) bn_model = FullyConnectedNet(hidden_dims, weight_scale=weight_scale, normalization='batchnorm') model = FullyConnectedNet(hidden_dims, weight_scale=weight_scale, normalization=None) bn_solver = Solver(bn_model, small_data, num_epochs=10, batch_size=50, update_rule='adam', optim_config={'learning_rate': 1e-3, }, verbose=False, print_every=200) bn_solver.train() bn_solvers_ws[weight_scale] = bn_solver solver = Solver(model, small_data, num_epochs=10, batch_size=50, update_rule='adam', optim_config={'learning_rate': 1e-3, }, verbose=False, print_every=200) solver.train() solvers_ws[weight_scale] = solver
/content/drive/My Drive/Colab Notebooks/cs231n/assignments/assignment2/cs231n/layers.py:975: RuntimeWarning: overflow encountered in exp
exps = np.exp(x)
/content/drive/My Drive/Colab Notebooks/cs231n/assignments/assignment2/cs231n/layers.py:976: RuntimeWarning: invalid value encountered in true_divide
probs = exps / np.sum(exps, axis=-1, keepdims=True) + eps
# Plot results of weight scale experiment.best_train_accs, bn_best_train_accs = [], []best_val_accs, bn_best_val_accs = [], []final_train_loss, bn_final_train_loss = [], []for ws in weight_scales: best_train_accs.append(max(solvers_ws[ws].train_acc_history)) bn_best_train_accs.append(max(bn_solvers_ws[ws].train_acc_history)) best_val_accs.append(max(solvers_ws[ws].val_acc_history)) bn_best_val_accs.append(max(bn_solvers_ws[ws].val_acc_history)) final_train_loss.append(np.mean(solvers_ws[ws].loss_history[-100:])) bn_final_train_loss.append(np.mean(bn_solvers_ws[ws].loss_history[-100:]))plt.subplot(3, 1, 1)plt.title('Best val accuracy vs. weight initialization scale')plt.xlabel('Weight initialization scale')plt.ylabel('Best val accuracy')plt.semilogx(weight_scales, best_val_accs, '-o', label='baseline')plt.semilogx(weight_scales, bn_best_val_accs, '-o', label='batchnorm')plt.legend(ncol=2, loc='lower right')plt.subplot(3, 1, 2)plt.title('Best train accuracy vs. weight initialization scale')plt.xlabel('Weight initialization scale')plt.ylabel('Best training accuracy')plt.semilogx(weight_scales, best_train_accs, '-o', label='baseline')plt.semilogx(weight_scales, bn_best_train_accs, '-o', label='batchnorm')plt.legend()plt.subplot(3, 1, 3)plt.title('Final training loss vs. weight initialization scale')plt.xlabel('Weight initialization scale')plt.ylabel('Final training loss')plt.semilogx(weight_scales, final_train_loss, '-o', label='baseline')plt.semilogx(weight_scales, bn_final_train_loss, '-o', label='batchnorm')plt.legend()plt.gca().set_ylim(1.0, 3.5)plt.gcf().set_size_inches(15, 15)plt.show()
Inline Question 1:
Describe the results of this experiment. How does the weight initialization scale affect models with/without batch normalization differently, and why?
Answer:
Trainings with Batch Normalizations performs better most of experiments, it is because “Batch Normalization also has a beneficial effect on the gradient flow through the network, by reducing the dependence of gradients on the scale of the parameters or of their initial values. This allows us to use much higher learning rates without the risk of divergence” 1
Batch Normalization and Batch Size
We will now run a small experiment to study the interaction of batch normalization and batch size.
The first cell will train 6-layer networks both with and without batch normalization using different batch sizes. The second layer will plot training accuracy and validation set accuracy over time.
Describe the results of this experiment. What does this imply about the relationship between batch normalization and batch size? Why is this relationship observed?
Answer:
Since batch size increases, Batch Normalization performs better, it is because Batch Normalization depends on sampling quality in certain batch and if you increase the batch size, you can get a higher chance to sample the whole set better.
Layer Normalization
Batch normalization has proved to be effective in making networks easier to train, but the dependency on batch size makes it less useful in complex networks which have a cap on the input batch size due to hardware limitations.
Several alternatives to batch normalization have been proposed to mitigate this problem; one such technique is Layer Normalization [2]. Instead of normalizing over the batch, we normalize over the features. In other words, when using Layer Normalization, each feature vector corresponding to a single datapoint is normalized based on the sum of all terms within that feature vector.
Which of these data preprocessing steps is analogous to batch normalization, and which is analogous to layer normalization?
Scaling each image in the dataset, so that the RGB channels for each row of pixels within an image sums up to 1.
Scaling each image in the dataset, so that the RGB channels for all pixels within an image sums up to 1.
Subtracting the mean image of the dataset from each image in the dataset.
Setting all RGB values to either 0 or 1 depending on a given threshold.
Answer:
1: BN 2: LN 3: LN 4: BN
Layer Normalization: Implementation
Now you’ll implement layer normalization. This step should be relatively straightforward, as conceptually the implementation is almost identical to that of batch normalization. One significant difference though is that for layer normalization, we do not keep track of the moving moments, and the testing phase is identical to the training phase, where the mean and variance are directly calculated per datapoint.
Here’s what you need to do:
In cs231n/layers.py, implement the forward pass for layer normalization in the function layernorm_forward.
Run the cell below to check your results. * In cs231n/layers.py, implement the backward pass for layer normalization in the function layernorm_backward.
Run the second cell below to check your results. * Modify cs231n/classifiers/fc_net.py to add layer normalization to the FullyConnectedNet. When the normalization flag is set to "layernorm" in the constructor, you should insert a layer normalization layer before each ReLU nonlinearity.
Run the third cell below to run the batch size experiment on layer normalization.
def layernorm_forward(x, gamma, beta, ln_param):""" Forward pass for layer normalization. During both training and test-time, the incoming data is normalized per data-point, before being scaled by gamma and beta parameters identical to that of batch normalization. Note that in contrast to batch normalization, the behavior during train and test-time for layer normalization are identical, and we do not need to keep track of running averages of any sort. Input: - x: Data of shape (N, D) - gamma: Scale parameter of shape (D,) - beta: Shift paremeter of shape (D,) - ln_param: Dictionary with the following keys: - eps: Constant for numeric stability Returns a tuple of: - out: of shape (N, D) - cache: A tuple of values needed in the backward pass """ out, cache =None, None eps = ln_param.get("eps", 1e-5)############################################################################ TODO: Implement the training-time forward pass for layer norm. ## Normalize the incoming data, and scale and shift the normalized data ## using gamma and beta. ## HINT: this can be done by slightly modifying your training-time ## implementation of batch normalization, and inserting a line or two of ## well-placed code. In particular, can you think of any matrix ## transformations you could perform, that would enable you to copy over ## the batch norm code and leave it almost unchanged? ############################################################################# *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)***** N,D = x.shape#step1: calculate mean mu =1./D * np.sum(x, axis=1, keepdims=True)#step2: substract mean from all datapoints xmu = x - mu#step3: squaring xmu from 2nd degree sq = xmu**2#step4: calculate variance var =1./D * np.sum(sq, axis=1, keepdims=True)#step5: add eps for numerical stability, then take square-root from 2nd degree sqrtvar = np.sqrt(var + eps)#step6: invert sqrtvar ivar =1./ sqrtvar#step7: normalize the datapoints xhat = xmu * ivar#step8: scale the normalized data with gamma gammax = gamma * xhat#step9: shift the scaled data out = gammax + beta#store intermediate cache = (xhat,gamma,xmu,ivar,sqrtvar,var,eps)# *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****############################################################################ END OF YOUR CODE ############################################################################return out, cachedef layernorm_backward(dout, cache):""" Backward pass for layer normalization. For this implementation, you can heavily rely on the work you've done already for batch normalization. Inputs: - dout: Upstream derivatives, of shape (N, D) - cache: Variable of intermediates from layernorm_forward. Returns a tuple of: - dx: Gradient with respect to inputs x, of shape (N, D) - dgamma: Gradient with respect to scale parameter gamma, of shape (D,) - dbeta: Gradient with respect to shift parameter beta, of shape (D,) """ dx, dgamma, dbeta =None, None, None############################################################################ TODO: Implement the backward pass for layer norm. ## ## HINT: this can be done by slightly modifying your training-time ## implementation of batch normalization. The hints to the forward pass ## still apply! ############################################################################# *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****#unpack the cache variables xhat,gamma,xmu,ivar,sqrtvar,var,eps = cache N,D = dout.shape# based on formulations in the paper dxhat = dout * gamma dvar = np.sum((dxhat * xmu *-0.5* ((var+eps)**(-3/2))), axis=1, keepdims=True) dmu = np.sum((dxhat *-ivar), axis=1, keepdims=True) + (dvar * np.sum((-2*xmu), axis=1, keepdims=True) / D) dx = (dxhat * ivar) + (dvar * (2*xmu / D)) + (dmu / D) dgamma = np.sum(dout * xhat, axis=0) dbeta = np.sum(dout, axis=0)# *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****############################################################################ END OF YOUR CODE ############################################################################return dx, dgamma, dbeta
# Check the training-time forward pass by checking means and variances# of features both before and after layer normalization.# Simulate the forward pass for a two-layer network.np.random.seed(231)N, D1, D2, D3 =4, 50, 60, 3X = np.random.randn(N, D1)W1 = np.random.randn(D1, D2)W2 = np.random.randn(D2, D3)a = np.maximum(0, X.dot(W1)).dot(W2)print('Before layer normalization:')print_mean_std(a,axis=1)gamma = np.ones(D3)beta = np.zeros(D3)# Means should be close to zero and stds close to one.print('After layer normalization (gamma=1, beta=0)')a_norm, _ = layernorm_forward(a, gamma, beta, {'mode': 'train'})print_mean_std(a_norm,axis=1)gamma = np.asarray([3.0,3.0,3.0])beta = np.asarray([5.0,5.0,5.0])# Now means should be close to beta and stds close to gamma.print('After layer normalization (gamma=', gamma, ', beta=', beta, ')')a_norm, _ = layernorm_forward(a, gamma, beta, {'mode': 'train'})print_mean_std(a_norm,axis=1)
We will now run the previous batch size experiment with layer normalization instead of batch normalization. Compared to the previous experiment, you should see a markedly smaller influence of batch size on the training history!
When is layer normalization likely to not work well, and why?
Using it in a very deep network
Having a very small dimension of features
Having a high regularization term
Answer:
1 - NO: There is no obvious relation between Layer Norm Formula and number of hidden layers in Neural Networks 2 - YES: Due to Layer Normalization calculates the mean and variance over the dimensions of the data, low-dimensional datas might lead to having inappropiate mean and variance calculations, those statistics metrics works better since their input size increases. 3 - NO: Even I am not quite sure, I think this is false because still there is no obvious relation between this to terms. Also, having high regularization terms can cause problems regardless of Layer Normalization, because this might forces network to too simple model