Neural Networks

Neural networks are my favourite AI application which is just as well for you as they’re the most important development in computers since the internet!

I’m going to explain neural networks to you in the same manner I do all my training — by hands-on doing it! For now just have a quick look at this code and then below it we’ll start keying it in bit by bit and explaining what it’s doing step-by-step.

import numpy as np

# sigmoid function
def nonlinear(x,deriv=False):
    if(deriv==True):
        return x*(1-x)
    return 1/(1+np.exp(-x))
    
# input dataset
X = np.array([  [0,0,1],
                [0,1,1],
                [1,0,1],
                [1,1,1] ])
    
# output dataset            
y = np.array([[0,0,1,1]]).T

# seed random numbers to make calculation
# deterministic (just a good practice)
np.random.seed(1)

# initialize weights randomly with mean 0
synapse0 = 2*np.random.random((3,1)) - 1

for iter in xrange(10000):

    # forward propagation
    layer0 = X
    layer1 = nonlinear(np.dot(layer0,synapse0))

    # how much did we miss?
    layer1_error = y - layer1

    # multiply how much we missed by the 
    # slope of the sigmoid at the values in l1
    layer1_delta = layer1_error * nonlinear(l1,True)

    # update weights
    synapse0 += np.dot(layer0.T,layer1_delta)

print("Output After Training:")
print(layer1)

Key in this code (in bits and pieces as I’m doing below) into a new Jupyter Notebook and execute it (shift+enter) and read up my notes as you go along.

I pressed shift+enter to get the notebook to execute the cell and move the cursor into the next cell ready for me to type the next bit of code. There was no error so numpy is installed properly on my machine. If you got an error go back to this page and read through and install numpy.