---
tags: machine-learning
---
# Neural Networks
<div style="text-align:center">
<img src="https://raw.githubusercontent.com/valoxe/image-storage-1/master/blog-machine-learning/neural-network/1.png" height="100%" width="70%">
</div>
> This is my personal notes taken for the course [Machine learning](https://www.coursera.org/learn/machine-learning#syllabus) by Standford. Feel free to check the [assignments](https://github.com/3outeille/Coursera-Labs).
> Also, if you want to read my other notes, feel free to check them at my [blog](https://ferdinandmom.engineer/machine-learning/).
## I) Representation
Neural networks offer an alternate way to perform machine learning when we have complex hypotheses with many features. In other words, Neural Networks have the ability to learn and model non-linear and complex relationships between inputs and outputs.
<div style="text-align:center">
<img src="https://raw.githubusercontent.com/valoxe/image-storage-1/master/blog-machine-learning/neural-network/2.png" height="100%" width="70%">
</div>
<br>
- Layer 1 is called **input layer**.
- Layer 3 is called **output layer**.
- Everything between Layer 1 and Layer 3 are called **hidden layer** (Layer 2). There can be more than 1 hidden layer.
- Links from **Layer 1 to Layer 2** and **Layer 2 to Layer 3** are called **weights**. They are the $\theta$s parameters. All $\theta$s are gathered in $\Theta^{(l)}$, matrix of weights from layer $l$ to $l+1$.
- $b_0$ is called **bias**. It is always equal to 1.
- $a^{(l)}_j$ = the $j^{th}$ [activation node]{.underline} in layer l.
The input matrix is denoted as:
$$x =
\begin{bmatrix}
b_0 \\
x_1 \\
x_2 \\
x_3\\
\end{bmatrix}
$$
The weight matrix is denoted as:
$$
\Theta^{(1)} =
\begin{bmatrix}
\theta^{(1)}_{1,0} & \theta^{(1)}_{1,1} & \theta^{(1)}_{1,2} & \theta^{(1)}_{1,3} \\
\theta^{(1)}_{2,0} & \theta^{(1)}_{2,1} & \theta^{(1)}_{2,2} & \theta^{(1)}_{2,3} \\
\theta^{(1)}_{3,0} & \theta^{(1)}_{3,1} & \theta^{(1)}_{3,2} & \theta^{(1)}_{3,3} \\
\theta^{(1)}_{4,0} & \theta^{(1)}_{4,1} & \theta^{(1)}_{4,2} & \theta^{(1)}_{4,3} \\
\theta^{(1)}_{5,0} & \theta^{(1)}_{5,1} & \theta^{(1)}_{5,2} & \theta^{(1)}_{5,3}
\end{bmatrix}$$
For example:
- $\theta^{(1)}_{1,0}$ is the weight from $b_0$ to $a^{(2)}_{1}$.
- $\theta^{(1)}_{2,2}$ is the weight from $x_2$ to $a^{(2)}_{2}$.
- $\theta^{(1)}_{5,3}$ is the weight from $x_3$ to $a^{(2)}_{5}$.
The indexing could seem weird but it was done this way to prevent from transposing the weight matrix in the hypothesis function.
The activation nodes matrix from the hidden layer is denoted as:
$$a^{(2)} =
\begin{bmatrix}
a^{(2)}_1 \\
a^{(2)}_2 \\
a^{(2)}_3 \\
a^{(2)}_4 \\
a^{(2)}_5 \\
\end{bmatrix}$$
## II) Forward propagation
To go from one layer to another, we have to do a **Forward propagation**. It consists on applying an **activation function** (most of the time the sigmoid function) on a weighted sum. Let's denote **g** the activation function.
<ins>From Layer 1 to Layer 2:</ins>
The values for each of the "activation" nodes is obtained as follows:
\begin{align*}
a^{(2)}_1 &= g(\theta^{(1)}_{1,0}b_0 + \theta^{(1)}_{1,1}x_1 + \theta^{(1)}_{1,2}x_2 + \theta^{(1)}_{1,3}x_3)
\\
a^{(2)}_2 &= g(\theta^{(1)}_{2,0}b_0 + \theta^{(1)}_{2,1}x_1 + \theta^{(1)}_{2,2}x_2 + \theta^{(1)}_{2,3}x_3)
\\
a^{(2)}_3 &= g(\theta^{(1)}_{3,0}b_0 + \theta^{(1)}_{3,1}x_1 + \theta^{(1)}_{3,2}x_2 + \theta^{(1)}_{3,3}x_3)
\\
a^{(2)}_4 &= g(\theta^{(1)}_{4,0}b_0 + \theta^{(1)}_{4,1}x_1 + \theta^{(1)}_{4,2}x_2 + \theta^{(1)}_{4,3}x_3)
\\
a^{(2)}_5 &= g(\theta^{(1)}_{5,0}b_0 + \theta^{(1)}_{5,1}x_1 + \theta^{(1)}_{5,2}x_2 + \theta^{(1)}_{5,3}x_3
\end{align*}
which is basically:
$$a^{(2)}=g(\Theta^{(1)}x)
\\
a^{(2)}=g(z^{(2)})$$
<ins>From Layer 2 to Layer 3:</ins>
The hidden layer matrix is denoted as:
$$a^{(2)} =
\begin{bmatrix}
b_0 \\
a^{(2)}_1 \\
a^{(2)}_2 \\
a^{(2)}_3 \\
a^{(2)}_4 \\
a^{(2)}_5
\end{bmatrix}$$
Notice that we have to manually add the bias.
The weight matrix is denoted as:
$$\Theta^{(2)} =
\begin{bmatrix}
\theta^{(2)}_{1,0} & \theta^{(2)}_{1,1} & \theta^{(2)}_{1,2} & \theta^{(2)}_{1,3} & \theta^{(2)}_{1,4} & \theta^{(2)}_{1,5}
\\
\theta^{(2)}_{2,0} & \theta^{(2)}_{2,1} & \theta^{(2)}_{2,2} & \theta^{(2)}_{2,3} & \theta^{(2)}_{2,4} & \theta^{(2)}_{2,5}
\end{bmatrix}$$
For example:
- $\theta^{(2)}_{1,0}$ is the weight from $b_0$ to $a^{(3)}_{1}$.
- $\theta^{(2)}_{2,2}$ is the weight from $a^{(2)}_2$ to $a^{(3)}_{2}$.
- $\theta^{(2)}_{2,5}$ is the weight from $a^{(2)}_5$ to $a^{(3)}_{2}$.
The output matrix is denoted as:
$$a^{(3)} =
\begin{bmatrix}
a^{(3)}_{1} \\
a^{(3)}_{2}
\end{bmatrix}$$
The values for each of the output nodes is obtained as follows:
\begin{align*}
a^{(3)}_{1} &= g(\theta^{(2)}_{0,1}b_0 + \theta^{(2)}_{1,1}a^{(2)}_1 + \theta^{(2)}_{2,1}a^{(2)}_2 + \theta^{(2)}_{3,1}a^{(2)}_3 + \theta^{(2)}_{4,1}a^{(2)}_4 + \theta^{(2)}_{5,1}a^{(2)}_5)
\\
a^{(3)}_{2} &= g(\theta^{(2)}_{0,1}b_0 + \theta^{(2)}_{1,1}a^{(2)}_1 + \theta^{(2)}_{2,1}a^{(2)}_2 + \theta^{(2)}_{3,1}a^{(2)}_3 + \theta^{(2)}_{4,1}a^{(2)}_4 + \theta^{(2)}_{5,1}a^{(2)}_5)
\\
\end{align*}
which is basically:
$$a^{(3)}=g(\Theta^{(2)}a^{(2)})
\\
a^{(3)}=g(z^{(3)})$$
To go from one layer to another, we have to do a **weighted sum** (characterized by $z$) on which we will apply the **activation function**. That's the principle of the <ins>**Forward propagation**</ins>.
## III) Intuition
A simple example of applying neural networks is by predicting $x_1 \text{AND} x_2$, which is the logical **AND** operator and is only true if both $x_1$ and $x_2$ are 1.
\begin{align*}
\begin{bmatrix}x_0 \\ x_1 \\ x_2\end{bmatrix} \rightarrow\begin{bmatrix}g(z^{(2)})\end{bmatrix} \rightarrow h_\Theta(x)
\end{align*}
Remember that $x_0$ is our bias variable and is always 1.
Let's set our first theta matrix as:
$$\Theta^{(1)} =\begin{bmatrix}-30 & 20 & 20\end{bmatrix}$$
This will cause the output of our hypothesis to only be positive if both $x_1$ and $x_2$ are 1. In other words, we replace the value of $x_1$ and $x_2$ by those in the table and then we compare the output of $h_\Theta(x)$ with the graph of the sigmoid function (above the table).
<div style="text-align:center">
<img src="https://raw.githubusercontent.com/valoxe/image-storage-1/master/blog-machine-learning/neural-network/3.png" height="100%">
</div>
<br>
As we can see, the result is exactly the AND gate. This was possible to do so because we already had the right values of $\theta$s. Thus, the goal of the neural network is to actually find **the right values of $\theta$s**. And to find the right values of $\theta$s, we have to minimize a cost function. Here is the one used by Andrew Ng: the **Cross-Entropy cost function** (adapted for Neural Network).
\begin{gather*}\large C(\Theta) = - \frac{1}{m} \sum_{i=1}^m \sum_{k=1}^K \left[y^{(i)}_k \log ((h_\Theta (x^{(i)}))_k) + (1 - y^{(i)}_k)\log (1 - (h_\Theta(x^{(i)}))_k)\right] + \frac{\lambda}{2m}\sum_{l=1}^{L-1} \sum_{i=1}^{s_l} \sum_{j=1}^{s_{l+1}} ( \Theta_{j,i}^{(l)})^2\end{gather*}
<ins>**Remark:**</ins>
Most of the time, the cost function used in Neural Network are **non-convex** (it means that it has multiple local minima).
## IV) Backward propagation
In order to minimize the cost function, we have to apply the **gradient descent**.
$$\Theta^{(l)} \leftarrow \Theta^{(l)} - \alpha \frac{\mathrm{\partial \mathcal{L}} }{\mathrm{\partial}\Theta^{(l)}}$$
- $\Theta^{(l)}$ : weights of layer l.
- $\alpha$ : The learning rate.
- $\mathcal{L}$ : Loss function ($y * \log(\hat{y}) + (1 - y) * \log(1 -\hat{y})$)
But how do we compute $\frac{\mathrm{\partial \mathcal{L}} }{\mathrm{\partial}\Theta^{(l)}}$ ? By applying the principle of **Backpropagation**.
**Backpropagation** is about understanding how changing the weights and biases in a network changes the cost function. Ultimately, this means computing the partial derivatives :
$$\frac{\mathrm{\partial \mathcal{L}} }{\mathrm{\partial }\theta_{jk}^{(l)}}$$
But to compute those, we have to introduce an intermediate quantity $\delta_{j}^{l}$, which we call the error in the j$^{th}$ neuron in the l$^{th}$ layer.
There is 2 $\delta^{l}$ equations here:
- For the last layer **L**.
- For hidden layer **l**.
<ins>**For the last layer L:**</ins>
As we can in the picture below, the error in the last layer is cause by the $a^{(L)}$.
<div style="text-align:center">
<img src="https://raw.githubusercontent.com/valoxe/image-storage-1/master/blog-machine-learning/neural-network/4.png" height="100%" width="40%">
</div>
<br>
Notice here that $C_0$ is equivalent to $\mathcal{L}$. ($\mathcal{L}$ is the loss function defined on a data point. The cost function $C$ is the sum of loss functions)
If we differentiate it, we have:
$$\delta^{(L)} = (a^{(L)} - y)$$
<ins>**For every other layer l:**</ins>
To get the delta values of the layers before the last layer, we can use an equation that steps us back from right to left:
$$\delta^{(l)} = ((\Theta^{(l+1)})^T \delta^{(l+1)})\ .*\ a'(z^{(l)})$$
The g-prime derivative terms can also be written out as:
$$a'(u)=a(u)\ .*\ (1 - a(u))$$
The full back propagation equation for the inner nodes is then:
$$\delta^{(l)} = ((\Theta^{(l+1)})^T \delta^{(l+1)})\ .*\ a^{(l)}\ .*\ (1 - a^{(l)})$$
We can then compute our partial derivative terms by multiplying our activation values and our error values for each training example t.
$$\dfrac{\partial \mathcal{L}}{\partial \Theta_{j,k}^{(l)}} = \frac{1}{m}\sum_{t=1}^m a_j^{(t)(l)} {\delta}_k^{(t)(l+1)}$$
This however ignores regularization, which we'll deal with later.
## V) Bonus: Backpropagation
<ins>**Derivative computation of $\delta^{(L)}$:**</ins>
By applying chain rule,
$$\delta^{(L)} = \frac{\mathrm{\partial \mathcal{L}} }{\mathrm{\partial a^{(L)}}} \frac{\mathrm{\partial a^{(L)}} }{\mathrm{\partial z^{(L)}}}$$
<ins>Let's compute $\frac{\mathrm{\partial \mathcal{L}} }{\mathrm{\partial a^{(L)}}}$.</ins>
We know that:
$$\mathcal{L}(\theta)=-\Big(y_i * log\big(a_i)\big)+(1-y_i) * log\big(1-a_i\big)\Big)$$
where
\begin{align*}
&a_i = h_\theta(x_i) = g(\theta x_i) = \frac{1}{1+e^{-\theta x_i}}
\\
&a' = a * (1-a)
\end{align*}
Thus,
\begin{align}
\frac{\partial -\Big(y*log\big(a\big)+(1-y) * log\big(1-a\big)\Big)}{\partial a} &=-\Big(y*\frac{1}{a} + (1-y) * -1 * \frac{1}{1-a}\Big) \text{(Chain Rule)}\\
&=\Big(\frac{y}{a} + \frac{(1-y)}{1-a}\Big)
\end{align}
<ins>Let's compute $\frac{\mathrm{\partial a^{(L)}}}{\mathrm{\partial z^{(L)}}}$.</ins>
$\frac{\mathrm{\partial a^{(L)}}}{\mathrm{\partial z^{(L)}}}$ is just the derivative of the sigmoid function, $a^{L}(1 - a^{L})$
Thus,
$$
\delta^{(L)} = \frac{\mathrm{\partial \mathcal{L}} }{\mathrm{\partial a^{(L)}}} \frac{\mathrm{\partial a^{(L)}} }{\mathrm{\partial z^{(L)}}}
= \Big(\frac{y}{a} + \frac{(1-y)}{1-a}\Big) * a^{L} * (1 - a^{L})
= \boxed{a^{L} - y}$$
---
<ins>**Derivative computation of $\delta^{(l)}$**</ins>
By definition,
\begin{eqnarray*}
\delta^l_j & = & \frac{\partial \mathcal{L}}{\partial z^l_j}
\\
& = & \frac{\partial L}{\partial z^{l+1}} \frac{\partial z^{l+1}}{\partial z^l_j}
\\
& = & \frac{\partial z^{l+1}}{\partial z^l_j} \delta^{l+1}
\end{eqnarray*}
We know that,
\begin{eqnarray*}
z^{l+1} = \sum_j \Theta^{l+1}_{j} a^l_j +b^{l+1} = \sum_j \Theta^{l+1}_{j} g(z^l_j) +b^{l+1}
\end{eqnarray*}
Differentiating, we obtain
\begin{eqnarray*}
\frac{\partial z^{l+1}}{\partial z^l_j} = \Theta^{l + 1}_{j} g'(z^l_j)
\end{eqnarray*}
Thus,
\begin{eqnarray*}
\delta^l_j &= \Theta^{l+1}_{j} g'(z^l_j) \delta^{l+1} \\
\delta^l &= \boxed{(\Theta^{l+1})^{T} \delta^{l+1} g'(z^l)}
\end{eqnarray*}
---
<ins>**Computation of $\dfrac{\partial \mathcal{L}}{\partial \Theta_{j,k}^{(l)}}$**:</ins>
$$\frac{\partial L}{\partial \Theta_{jk}^l} = \frac{\partial \mathcal{L}}{\partial z_j^{l+1}} \frac{\partial z_j^{l+1}}{\partial \Theta_{jk}^l}$$
By definition, $\delta_j^{l+1} = \frac{\partial \mathcal{L}}{\partial z_j^{l+1}}$ and since $z^{l+1}_j = \sum_k \Theta^{l}_{jk} a^{l}_k+b^{l}_j$, we have $\frac{\partial z_j^{l+1}}{\partial \Theta_{jk}^l} = a_k^{l}$.
And so we have,
$$\frac{\partial \mathcal{L}}{\partial \Theta^l_{jk}} = a^{l}_k \delta^{l+1}_j\ \text{(Vectorized form)}$$
## VI) Random initialization of weights
For example, let's say you initialize all your weights to zero, then all of the hidden neurons (units) in your neural network will be doing the exact same calculations. The goal of **Random initialization** is to break the symmetry (**symmetry breaking**). You want different hidden units to compute different functions.
**Analogy:** Imagine that someone has dropped you and your friends from a helicopter to an unknown mountain at the top and you're trapped there. Everywhere is fogged. The only thing you know is that you should get down to the sea level somehow. Which direction should you take to get down to the lowest possible point?
If you couldn't find a way to the sea level and so, the helicopter would take you guys again and would drop you to the same mountain at the top position. You would have to take the same directions again because you're "initializing" yourself to the same **starting positions**.
However, each time the helicopter drops you somewhere **random** on the mountain, you guys would take different directions and steps. So, there would be a **better chance** for you to reach to the lowest possible point.
This is what is meant by **breaking the symmetry**. The initialization is **asymmetric** so that you can find different solutions to the same problem.
In this analogy, **where you land is the weights**. So, with different weights, there's a better chance of reaching to the lowest (or lower) point.
<div style="text-align:center">
<img src="https://raw.githubusercontent.com/valoxe/image-storage-1/master/blog-machine-learning/neural-network/5.png" height="100%">
</div>