Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy
- # scipy.special for the sigmoid function expit()
- import scipy.special
- # neural network class definition
- class neuralNetwork:
- # initialize 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 = (2 * numpy.random.rand(self.hnodes, self.inodes) - 1)
- self.who = (2 * numpy.random.rand(self.onodes, self.hnodes) - 1)
- # learning rate
- self.lr = learningrate
- pass
- # 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
- # number of input, hidden and output nodes
- input_nodes = 3
- hidden_nodes = 3
- output_nodes = 3
- # learning rate is 0.3
- learning_rate = 0.3
- # create instance of neural network
- n = neuralNetwork(input_nodes, hidden_nodes, output_nodes, learning_rate)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement