...

/

Python Code for a Basic Neural Network

Python Code for a Basic Neural Network

Test the code of a basic neural network with some random inputs and see if it's built properly.

We'll cover the following...

Neural network class

Let’s pause to check what the code for the neural network class we’re building up looks like. It should look something like this:

Press + to interact
# neural network class definition
class neuralNetwork:
# initialise the neural network
def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate):
# set number of nodes in each input, hidden, output layer
self.inodes = inputnodes
self.hnodes = hiddennodes
self.onodes = outputnodes
# link weight matrices, wih and who
# weights inside the arrays are w_i_j, where link is from node i to node j in the next layer
# w11 w21
# w12 w22 etc
self.wih = numpy.random.normal(0.0, pow(self.hnodes, -0.5), (self.hnodes, self.inodes))
self.who = numpy.random.normal(0.0, pow(self.onodes, -0.5), (self.onodes, self.hnodes))
# learning rate
self.lr = learningrate
# activation function is the sigmoid function
self.activation_function = lambda x: scipy.special.expit(x)
pass
# train the neural network
def train():
pass
# query the neural network
def query(self, inputs_list):
# convert inputs list to 2d array
inputs = numpy.array(inputs_list, ndmin=2).T
# calculate signals into hidden layer
hidden_inputs = numpy.dot(self.wih, inputs)
# calculate the signals emerging from hidden layer
hidden_outputs = self.activation_function(hidden_inputs)
# calculate signals into final output layer
final_inputs = numpy.dot(self.who, hidden_outputs)
# calculate the signals emerging from final output layer
final_outputs = self.activation_function(final_inputs)
return final_outputs

That is just the class. Aside from that, we should be importing the numpy and scipy.special modules right at the top of the code in the cell.

Press + to interact
import numpy
# scipy.special for the sigmoid function expit()
import scipy.special

Note that the query() function only needs the input_list. It doesn’t need any other input.

That’s good progress, and now we’ll look at the missing piece, the train() function. ...