NEURAL NETWORKS

This notebook covers the neural network algorithms from chapter 18 of the book Artificial Intelligence: A Modern Approach, by Stuart Russel and Peter Norvig. The code in the notebook can be found in learning.py.

Execute the below cell to get started:


In [1]:
from learning import *

from notebook import psource, pseudocode

NEURAL NETWORK ALGORITHM

Overview

Although the Perceptron may seem like a good way to make classifications, it is a linear classifier (which, roughly, means it can only draw straight lines to divide spaces) and therefore it can be stumped by more complex problems. We can extend Perceptron to solve this issue, by employing multiple layers of its functionality. The construct we are left with is called a Neural Network, or a Multi-Layer Perceptron, and it is a non-linear classifier. It achieves that by combining the results of linear functions on each layer of the network.

Similar to the Perceptron, this network also has an input and output layer. However it can also have a number of hidden layers. These hidden layers are responsible for the non-linearity of the network. The layers are comprised of nodes. Each node in a layer (excluding the input one), holds some values, called weights, and takes as input the output values of the previous layer. The node then calculates the dot product of its inputs and its weights and then activates it with an activation function (sometimes a sigmoid). Its output is fed to the nodes of the next layer. Note that sometimes the output layer does not use an activation function, or uses a different one from the rest of the network. The process of passing the outputs down the layer is called feed-forward.

After the input values are fed-forward into the network, the resulting output can be used for classification. The problem at hand now is how to train the network (ie. adjust the weights in the nodes). To accomplish that we utilize the Backpropagation algorithm. In short, it does the opposite of what we were doing up to now. Instead of feeding the input forward, it will feed the error backwards. So, after we make a classification, we check whether it is correct or not, and how far off we were. We then take this error and propagate it backwards in the network, adjusting the weights of the nodes accordingly. We will run the algorithm on the given input/dataset for a fixed amount of time, or until we are satisfied with the results. The number of times we will iterate over the dataset is called epochs. In a later section we take a detailed look at how this algorithm works.

NOTE: Sometimes we add to the input of each layer another node, called bias. This is a constant value that will be fed to the next layer, usually set to 1. The bias generally helps us "shift" the computed function to the left or right.

Implementation

The NeuralNetLearner function takes as input a dataset to train upon, the learning rate (in (0, 1]), the number of epochs and finally the size of the hidden layers. This last argument is a list, with each element corresponding to one hidden layer.

After that we will create our neural network in the network function. This function will make the necessary connections between the input layer, hidden layer and output layer. With the network ready, we will use the BackPropagationLearner to train the weights of our network for the examples provided in the dataset.

The NeuralNetLearner returns the predict function which, in short, can receive an example and feed-forward it into our network to generate a prediction.

In more detail, the example values are first passed to the input layer and then they are passed through the rest of the layers. Each node calculates the dot product of its inputs and its weights, activates it and pushes it to the next layer. The final prediction is the node with the maximum value from the output layer.


In [ ]:
psource(NeuralNetLearner)

BACKPROPAGATION

Overview

In both the Perceptron and the Neural Network, we are using the Backpropagation algorithm to train our weights. Basically it achieves that by propagating the errors from our last layer into our first layer, this is why it is called Backpropagation. In order to use Backpropagation, we need a cost function. This function is responsible for indicating how good our neural network is for a given example. One common cost function is the Mean Squared Error (MSE). This cost function has the following format:

$$MSE=\frac{1}{2} \sum_{i=1}^{n}(y - \hat{y})^{2}$$

Where n is the number of training examples, $\hat{y}$ is our prediction and $y$ is the correct prediction for the example.

The algorithm combines the concept of partial derivatives and the chain rule to generate the gradient for each weight in the network based on the cost function.

For example, if we are using a Neural Network with three layers, the sigmoid function as our activation function and the MSE cost function, we want to find the gradient for the a given weight $w_{j}$, we can compute it like this:

$$\frac{\partial MSE(\hat{y}, y)}{\partial w_{j}} = \frac{\partial MSE(\hat{y}, y)}{\partial \hat{y}}\times\frac{\partial\hat{y}(in_{j})}{\partial in_{j}}\times\frac{\partial in_{j}}{\partial w_{j}}$$

Solving this equation, we have:

$$\frac{\partial MSE(\hat{y}, y)}{\partial w_{j}} = (\hat{y} - y)\times{\hat{y}}'(in_{j})\times a_{j}$$

Remember that $\hat{y}$ is the activation function applied to a neuron in our hidden layer, therefore $$\hat{y} = sigmoid(\sum_{i=1}^{num\_neurons}w_{i}\times a_{i})$$

Also $a$ is the input generated by feeding the input layer variables into the hidden layer.

We can use the same technique for the weights in the input layer as well. After we have the gradients for both weights, we use gradient descent to update the weights of the network.

Pseudocode


In [3]:
pseudocode('Back-Prop-Learning')


Out[3]:

AIMA3e

function BACK-PROP-LEARNING(examples, network) returns a neural network
inputs examples, a set of examples, each with input vector x and output vector y
    network, a multilayer network with L layers, weights wi,j, activation function g
local variables: Δ, a vector of errors, indexed by network node

repeat
   for each weight wi,j in network do
     wi,j ← a small random number
   for each example (x, y) in examples do
     /* Propagate the inputs forward to compute the outputs */
     for each node i in the input layer do
       aixi
     for l = 2 to L do
       for each node j in layer l do
         inj ← Σ_i_ wi,j ai
         ajg(inj)
     /* Propagate deltas backward from output layer to input layer */
     for each node j in the output layer do
       Δ[j] ← g′(inj) × (yiaj)
     for l = L − 1 to 1 do
       for each node i in layer l do
         Δ[i] ← g′(ini) Σ_j_ wi,j Δ[j]
     /* Update every weight in network using deltas */
     for each weight wi,j in network do
       wi,jwi,j &plus α × ai × Δ[j]
until some stopping criterion is satisfied
return network


Figure ?? The back-propagation algorithm for learning in multilayer networks.

Implementation

First, we feed-forward the examples in our neural network. After that, we calculate the gradient for each layer weights. Once that is complete, we update all the weights using gradient descent. After running these for a given number of epochs, the function returns the trained Neural Network.


In [ ]:
psource(BackPropagationLearner)

In [4]:
iris = DataSet(name="iris")
iris.classes_to_numbers()

nNL = NeuralNetLearner(iris)
print(nNL([5, 3, 1, 0.1]))


0

The output should be 0, which means the item should get classified in the first class, "setosa". Note that since the algorithm is non-deterministic (because of the random initial weights) the classification might be wrong. Usually though it should be correct.

To increase accuracy, you can (most of the time) add more layers and nodes. Unfortunately the more layers and nodes you have, the greater the computation cost.