Neural Networks

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

This is my personal notes taken for the course Machine learning by Standford. Feel free to check the assignments.
Also, if you want to read my other notes, feel free to check them at my blog.

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.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • 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
    θ
    s parameters. All
    θ
    s are gathered in
    Θ(l)
    , matrix of weights from layer
    l
    to
    l+1
    .
  • b0
    is called bias. It is always equal to 1.
  • aj(l)
    = the
    jth
    [activation node]{.underline} in layer l.

The input matrix is denoted as:

x=[b0x1x2x3]

The weight matrix is denoted as:

Θ(1)=[θ1,0(1)θ1,1(1)θ1,2(1)θ1,3(1)θ2,0(1)θ2,1(1)θ2,2(1)θ2,3(1)θ3,0(1)θ3,1(1)θ3,2(1)θ3,3(1)θ4,0(1)θ4,1(1)θ4,2(1)θ4,3(1)θ5,0(1)θ5,1(1)θ5,2(1)θ5,3(1)]

For example:

  • θ1,0(1)
    is the weight from
    b0
    to
    a1(2)
    .
  • θ2,2(1)
    is the weight from
    x2
    to
    a2(2)
    .
  • θ5,3(1)
    is the weight from
    x3
    to
    a5(2)
    .

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)=[a1(2)a2(2)a3(2)a4(2)a5(2)]

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.

From Layer 1 to Layer 2:

The values for each of the "activation" nodes is obtained as follows:

a1(2)=g(θ1,0(1)b0+θ1,1(1)x1+θ1,2(1)x2+θ1,3(1)x3)a2(2)=g(θ2,0(1)b0+θ2,1(1)x1+θ2,2(1)x2+θ2,3(1)x3)a3(2)=g(θ3,0(1)b0+θ3,1(1)x1+θ3,2(1)x2+θ3,3(1)x3)a4(2)=g(θ4,0(1)b0+θ4,1(1)x1+θ4,2(1)x2+θ4,3(1)x3)a5(2)=g(θ5,0(1)b0+θ5,1(1)x1+θ5,2(1)x2+θ5,3(1)x3

which is basically:

a(2)=g(Θ(1)x)a(2)=g(z(2))

From Layer 2 to Layer 3:

The hidden layer matrix is denoted as:

a(2)=[b0a1(2)a2(2)a3(2)a4(2)a5(2)]

Notice that we have to manually add the bias.

The weight matrix is denoted as:

Θ(2)=[θ1,0(2)θ1,1(2)θ1,2(2)θ1,3(2)θ1,4(2)θ1,5(2)θ2,0(2)θ2,1(2)θ2,2(2)θ2,3(2)θ2,4(2)θ2,5(2)]

For example:

  • θ1,0(2)
    is the weight from
    b0
    to
    a1(3)
    .
  • θ2,2(2)
    is the weight from
    a2(2)
    to
    a2(3)
    .
  • θ2,5(2)
    is the weight from
    a5(2)
    to
    a2(3)
    .

The output matrix is denoted as:

a(3)=[a1(3)a2(3)]

The values for each of the output nodes is obtained as follows:

a1(3)=g(θ0,1(2)b0+θ1,1(2)a1(2)+θ2,1(2)a2(2)+θ3,1(2)a3(2)+θ4,1(2)a4(2)+θ5,1(2)a5(2))a2(3)=g(θ0,1(2)b0+θ1,1(2)a1(2)+θ2,1(2)a2(2)+θ3,1(2)a3(2)+θ4,1(2)a4(2)+θ5,1(2)a5(2))

which is basically:

a(3)=g(Θ(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 Forward propagation.

III) Intuition

A simple example of applying neural networks is by predicting

x1ANDx2, which is the logical AND operator and is only true if both
x1
and
x2
are 1.

[x0x1x2][g(z(2))]hΘ(x)

Remember that

x0 is our bias variable and is always 1.

Let's set our first theta matrix as:

Θ(1)=[302020]

This will cause the output of our hypothesis to only be positive if both

x1 and
x2
are 1. In other words, we replace the value of
x1
and
x2
by those in the table and then we compare the output of
hΘ(x)
with the graph of the sigmoid function (above the table).

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

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

θs. Thus, the goal of the neural network is to actually find the right values of
θ
s
. And to find the right values of
θ
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).

C(Θ)=1mi=1mk=1K[yk(i)log((hΘ(x(i)))k)+(1yk(i))log(1(hΘ(x(i)))k)]+λ2ml=1L1i=1slj=1sl+1(Θj,i(l))2

Remark:

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.

Θ(l)Θ(l)αLΘ(l)

  • Θ(l)
    : weights of layer l.
  • α
    : The learning rate.
  • L
    : Loss function (
    ylog(y^)+(1y)log(1y^)
    )

But how do we compute

LΘ(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 :

Lθjk(l)

But to compute those, we have to introduce an intermediate quantity

δjl, which we call the error in the j
th
neuron in the l
th
layer.

There is 2

δl equations here:

  • For the last layer L.
  • For hidden layer l.

For the last layer L:

As we can in the picture below, the error in the last layer is cause by the

a(L).

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Notice here that

C0 is equivalent to
L
. (
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:

δ(L)=(a(L)y)

For every other layer l:

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:

δ(l)=((Θ(l+1))Tδ(l+1)) . a(z(l))

The g-prime derivative terms can also be written out as:

a(u)=a(u) . (1a(u))

The full back propagation equation for the inner nodes is then:

δ(l)=((Θ(l+1))Tδ(l+1)) . a(l) . (1a(l))

We can then compute our partial derivative terms by multiplying our activation values and our error values for each training example t.

LΘj,k(l)=1mt=1maj(t)(l)δk(t)(l+1)

This however ignores regularization, which we'll deal with later.

V) Bonus: Backpropagation

Derivative computation of

δ(L):

By applying chain rule,

δ(L)=La(L)a(L)z(L)

Let's compute

La(L).

We know that:

L(θ)=(yilog(ai))+(1yi)log(1ai))

where

ai=hθ(xi)=g(θxi)=11+eθxia=a(1a)

Thus,

(ylog(a)+(1y)log(1a))a=(y1a+(1y)111a)(Chain Rule)=(ya+(1y)1a)

Let's compute

a(L)z(L).

a(L)z(L) is just the derivative of the sigmoid function,
aL(1aL)

Thus,

δ(L)=La(L)a(L)z(L)=(ya+(1y)1a)aL(1aL)=aLy


Derivative computation of

δ(l)

By definition,

δjl=Lzjl=Lzl+1zl+1zjl=zl+1zjlδl+1

We know that,

zl+1=jΘjl+1ajl+bl+1=jΘjl+1g(zjl)+bl+1

Differentiating, we obtain

zl+1zjl=Θjl+1g(zjl)

Thus,

δjl=Θjl+1g(zjl)δl+1δl=(Θl+1)Tδl+1g(zl)


Computation of

LΘj,k(l):

LΘjkl=Lzjl+1zjl+1Θjkl

By definition,

δjl+1=Lzjl+1 and since
zjl+1=kΘjklakl+bjl
, we have
zjl+1Θjkl=akl
.

And so we have,

LΘjkl=aklδjl+1 (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.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →