datahacker.rs@gmail.com

#010A Gradient Descent for Neural Networks

#010A Gradient Descent for Neural Networks

In this post we will see how to implement gradient descent for one hidden layer Neural Network as presented in the picture below. One hidden layer Neural Network

Parameters for one hidden layer Neural Network are $$\textbf{W}^{}$$, $$b^{}$$, $$\textbf{W}^{}$$ and $$b^{}$$. Number of unitis in each layer are:

• input of a Neural Network is feature vector ,so the length of “zero” layer $$a^{}$$ is the size of an input feature vector $$n_{x} = n^{}$$
• number of hidden units in a hidden layer is $$n^{}$$
• number of units in output layer is $$n^{}$$, so far we had one unit in an output layer so $$n^{}$$

As we have defined a number of units in hidden layers we can now tell what are dimension of the following matrices:

• $$\textbf{W}^{}$$ is $$(n^{} , n^{} )$$ matrix
• $$b^{}$$ is $$(n^{} , 1 )$$ matrix or a column vector
• $$\textbf{W}^{}$$ is $$(n^{} , n^{} )$$ matrix
• $$b^{}$$ is $$(n^{} , 1 )$$ , so far $$b^{}$$ is a scalar

Notation:

•  Superscript $$[l]$$ denotes a quantity associated with the $$l^{th}$$ layer.
• Example: $$a^{[L]}$$ is the $$L^{th}$$ layer activation. $$\textbf{W}^{[L]}$$ and $$b^{[L]}$$ are the $$L^{th}$$ layer parameters.
• Superscript $$(i)$$ denotes a quantity associated with the $$i^{th}$$ example.
• Example: $$x^{(i)}$$ is the $$i^{th}$$ training example.
• Lowerscript $$i$$ denotes the $$i^{th}$$ entry of a vector.
• Example: $$a^{[l]}_i$$ denotes the $$i^{th}$$ entry of the $$i^{th}$$ layer’s activations.

Equations for one example $$x^{(i)}$$ :

$$z^{ (i)} = \textbf{W}^{} x^{(i)} + b^{}$$
$$a^{ (i)} = \tanh(z^{ (i)})$$
$$z^{ (i)} = \textbf{W}^{} a^{ (i)} + b^{}$$
$$\hat{y}^{(i)} = a^{ (i)} = \sigma(z^{  (i)})$$

$$\hat{y^{(i)}} = \begin{cases} 1 & \mbox{if } a^{(i)} > 0.5 \\ 0 & \mbox{otherwise } \end{cases}$$

Assuming that we are doing a binary classification, and assuming that we have $$m$$ training examples, the cost function $$J$$ is:

$$J (\textbf{W}^{}, b^{}, \textbf{W}^{}, b^{} ) = \frac{1}{m}\sum_{i=1}^{m} \mathcal{L}(\hat{y}^{(i)},y^{(i)}) = \frac{1}{m}\sum_{i=1}^{m} \mathcal{L}(a^{(i)},y^{(i)})$$

$$J = – \frac{1}{m} \sum\limits_{i = 0}^{m} \large{(} \small y^{(i)}\log\left(a^{ (i)}\right) + (1-y^{(i)})\log\left(1- a^{ (i)}\right) \large{)} \small$$

To train parameters of our algorithm we need to perform gradient descent. When training neural network, it is important to initialize the parameters randomly rather then to all zeros. More about that you can see in this post. So after initializing the paramethers we get into gradient descent which looks like this:

$$repeat \enspace until \enspace convergence:$$

$$calculate \enspace predictions \enspace ( \hat{y}, i\enspace = \enspace 1 .. m )$$

$$\textbf{dW}^{} = \frac{\partial J}{\partial \textbf{W}^{}}$$

$$db^{} = \frac{\partial J}{\partial db^{}}$$

$$\textbf{dW}^{} = \frac{\partial J}{\partial \textbf{W}^{}}$$

$$db^{} = \frac{\partial J}{\partial db^{}}$$

$$\textbf{W}^{} = \textbf{W}^{} – \alpha \textbf{dW}^{}$$

$$b^{} = b^{} – \alpha db^{}$$

$$\textbf{W}^{} = \textbf{W}^{} – \alpha \textbf{dW}^{}$$

$$b^{} = b^{} – \alpha db^{}$$

So we need equations to calculate these derivatives.

Forward propagation equations (remember that if we are doing a binary classification then the activation function in the output layer is a  $$sigmoid$$ function):

 Non – Vectorized (for a single training example) Vectorized $$z^{ (i)} = \textbf{W}^{} x^{(i)} + b^{}$$ $$a^{ (i)} = \tanh(z^{ (i)})$$ $$z^{ (i)} = \textbf{W}^{} a^{ (i)} + b^{}$$ $$\hat{y}^{(i)} = a^{ (i)} = \sigma(z^{  (i)})$$ $$\textbf{Z}^{} = \textbf{W}^{}X + b^{}$$ $$\textbf{A}^{} = \tanh(\textbf{Z}^{})$$ $$\textbf{Z}^{}=\textbf{W}^{}A^{} + b^{}$$ $$\textbf{A}^{} = \sigma(\textbf{Z}^{})=\sigma (\textbf{Z}^{})$$

Now we will show equations in the backpropagation step:

 Non – Vectorized $$dz^{} = a^{} – y$$ $$\textbf{dW}^{} = dz^{} a^{T}$$ $$db^{} = dz^{}$$  $$dz^{} = \textbf{W}^{T}dz^{} \ast g’^{}(z^{})$$ $$\textbf{dW}^{} = dz^{} x^{T}$$ $$db^{} = dz^{}$$ Vectorized $$\textbf{dZ}^{} = \textbf{A}^{} – \textbf{Y}$$ $$\textbf{dW}^{} = \textbf{dZ}^{} \textbf{A}^{T}$$ $$db^{} = \frac{1}{m} np.sum(\textbf{dZ}^{}, axis = 1, keepdims=True )$$  $$\textbf{dZ}^{} = \textbf{W}^{T}\textbf{dZ}^{} \ast g’^{}(\textbf{Z}^{})$$ $$\textbf{dW}^{} = \frac{1}{m} \textbf{dZ}^{}\textbf{X}^{T}$$ $$db^{} = \frac{1}{m}np.sum(\textbf{dZ}^{}, axis=1, keepdims = True )$$

Sign $$*$$ stands for element  wise multiplication.

We will now the relation between a computation graph and these equations.

When we talked about Logistic Regression we had forward pass where we computed values of $$z$$, $$a$$ and at the end the loss $$\mathcal L$$. Then we had backward pass for calculating the derivatives, so we computed $$da$$, $$dz$$, and then $$dw$$ , $$db$$. Let’s see now the graph for Logistic Regression. Computation graph for Logistic Regression

We have defined a loss function the actual loss when the ground truth label is $$y$$, and our output is $$a$$:

$$\mathcal L(a = \hat{y},y) =- \enspace y loga-(1-y) \enspace log(1-a) \enspace$$

And corresponding derivatives are:

$$da = -\frac{y}{a} + \frac{1-y}{1-a}$$

$$dz = da g'(z) \enspace if \enspace g(z) = \sigma(z) = a \enspace \Rightarrow \enspace$$

$$dz =(-\frac{y}{a} + \frac{1-y}{1-a} )a(1-a) = a-y$$

$$dw = dz \enspace x$$

$$db = dz$$

Backpropagation graph for one hidden layer Neural Network

Backprpagation grapf is a graph that describes which calculations do we need to make when we want to calculate various derivatives and do the parameters update. In the following graph we can see that it is similar to the Logistic Regression grapf except that we do those calculations twice. Computation graph for 2 – layer Neural Network

Backpropagation algorithm is a technique used in training neural networks to update the weights and biases by calculating the gradient so that the accuracy of the neural network can be improved iteratively.

Firstly, we calculate $$da^{}$$ , $$dz^{}$$ and these calculations allows us to calculate $$\textbf{dW}^{}$$ and $$db^{}$$. Then, as we go deeper in the backpropagation step, we calculate $$da^{}$$ , $$dz^{}$$ which allows us to calculate  $$\textbf{dW}^{}$$ and $$db^{}$$. If you are interested in how these derivatives are calculated check these notes.

Click here to see how we can implement these ideas in Python.

In the next post we will learn how to train a shallow neural network with Gradient Descent.

More resources on the topic: 