MachineX: Artificial Neural Networks (Part 2)

If you are reading this article, you have probably already read Part 1. If not, you can find it here!

We got a basic understanding of neural networks, so let’s dive even deeper.

Once you got the dataset and problem identified, you can follow the below steps:

  1. Pick the network architecture (initialize with random weights)
  2. Do a forward pass (forward propagation)
  3. Calculate the total error (we need to minimize this error)
  4. Backpropagate the error and update weights (backpropagation)
  5. Repeat the process (2-4) for no of epochs/until error is minimum

Step 1: Pick the Network Architecture

Let’s take a toy dataset (XOR) and pick the architecture with 2 inputs, 2 outputs, and 1 hidden of 3 neurons.

Step 2: Forward Propagation

This is an easy process. We need to feed forward the inputs through each layer within the network, and the outputs from the previous layer become the inputs to the next layer (first, we need to feed our data as the inputs).

First, we provide the inputs(example) from our dataset:

dataset (XOR table) X y
1 1 0 --> X1=1 and X2=1 1 0 1 H1 = Sigmoid(X1*w1+X2*w2) = 0.5(assume with random 0 1 1 weights)
0 0 0 similarly H2, H3 and O1, O2

Step 3: Calculate the Total Error

Assume random weights and Activation (A1,2…) we get the errors for each neuron.

sum = inputs*weights and A = activation (sum) here Sigmoid (sum)

Out cost function according to Andrew Ng is:

Note: we take partial derivative w.r.t result (by using the Chain rule in calculus)

Step 4: Backpropagation

Don’t worry, it’s easy! Or I will make it easy.

The main goal of backpropagation is to update each of the weights within the network so they cause the predicted output to be nearer the target output, thereby minimizing the error for every output neuron and the network as a whole.

So far, we have the total error, which needs to be minimized.

If you know how gradient descent work, the rest is pretty easy. If you don’t know, here is an article that talks about gradient descent.

We need to calculate the below:

  1. How much does the total error change with respect to the result (or how much is a change in results. We already did in the picture above)?
  2. Next, how much does the result of change with respect to its sum (or how much is a change in sum)?
  3. Finally, how much is the sum of change with respect to weights (or how much is a change in weights)?

And that’s it.

Step 5: Repeat Steps 2-4 for Number of Epochs Until Error Is Minimized

We repeat the process of forwarding the weights (FP) and change weights (BP) for the number of epochs or when we reach the minimum error.

Once the training process is completed, we are able to do the prediction by feed-forwarding the input to the trained network.

In the next part, I will build a neural network from scratch.

Stay tuned!

This article was originally published here