MachineX: Artificial Neural Networks (Part 2)
This article shows us more about artificial neural networks and forward and backpropagation.
Join the DZone community and get the full member experience.Join For Free
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:
- Pick the network architecture (initialize with random weights)
- Do a forward pass (forward propagation)
- Calculate the total error (we need to minimize this error)
- Backpropagate the error and update weights (backpropagation)
- 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:
- 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)?
- Next, how much does the result of change with respect to its sum (or how much is a change in sum)?
- 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.
Published at DZone with permission of Shubham Goyal. See the original article here.
Opinions expressed by DZone contributors are their own.