The present session will introduce the Python classes and how to create your own Artificial Neural Network (ANN) class. The class will implement the `__init__`, `feedforward`, and `backpropagation` methods.
Each method will be described in detail to understand the ANN’s inner work. Then, the handwriting MNIST dataset will be used to train and test our ANN.
# Objectives
- Learn the basics of using Python Classes
- Create methods for: create, feedforward, and backpropagation
A class defines the structure, data and methods that an object will have. There is possible to have public and private variables to operate in the methods.
The next chunk of code defines the Neural Network's basic structure. We are going to implement and define the methods one at time to understand them in a better way.
Let’s begin with the initialization. We know we need to set the number of input, hidden and output layer nodes. That defines the shape and size of the neural network. Thus, we’ll let them be set when a new neural network object is created by using the class' parameters. That way we retain the choice to create new neural networks of different sizes with simple methods.
A good programmers, computer scientists and mathematicians, try to create more general code rather than specific code. It is a good habit, because it forces us to think about solving problems in a deeper and more general way. This means that our code can be used in more general scenarios.
At this point were are only creating an object, but the class can't do any useful yet. Also, this is a good technique to start coding somethig, by keeping it small at the begining (make commits), and then grow the methods.
So the next step is to create the network of nodes and links. The most important part of the network is the link weights. They’re used to calculate the signal being fed forward, the error as it’s propagated backwards, and it is the link weights themselves that are refined in an attempt to to improve the network.
For the basic NN, the weight matrix consist of:
- A matrix that links the input and hidden layers, $Wih$, of size hidden nodes by input nodes ($hn×in$)
- and another matrix for the links between the hidden and output layers, $Who$, of size $on×hn$ (output nodes by hidden nodes)
So the next step is to create the network of nodes and links. The most important part of the network is the link weights. They’re used to calculate the signal being fed forward, the error as it’s propagated backwards, and it is the link weights themselves that are refined in an attempt to to improve the network.