Simple Neural Network in Python
Step 01:
import numpy as np
import matplotlib.pyplot as plt # to plot error during training
Step 02: input
inputs=np.array([[0,1,0],
[0,1,1], [1,1,0],[1,0,1]])
Step 03: output
outputs=np.array([[0],[0],[1],[1]])
Step 04: create NeuralNetwork class
class NeuralNetwork:
Step 05: intialize variables in class
def __init__(self, inputs, outputs):
self.inputs = inputs
self.outputs = outputs
Step 06: initialize weights as .50 for simplicity
self.weights = np.array([[.50], [.50], [.50]])
self.error_history = []
self.epoch_list = []
Step 07: activation function ==> S(x) = 1/1+e^(-x)
def sigmoid(self, x, deriv=False):
if deriv == True:
return x * (1 — x)
return 1 / (1 + np.exp(-x))
Step 08: data will flow through the neural network.
def feed_forward(self):
self.hidden = self.sigmoid(np.dot(self.inputs, self.weights))
Step 09: going backwards through the network to update weights
def backpropagation(self):
self.error = self.outputs — self.hidden
delta = self.error * self.sigmoid(self.hidden, deriv=True)
self.weights += np.dot(self.inputs.T, delta)
Step 10: train the neural net for 25,000 iterations
def train(self, epochs=25000):
for epoch in range(epochs):
Step 11: flow forward and produce an output
self.feed_forward()
Step 12: go back though the network to make corrections based on the output
self.backpropagation()
Step 13: keep track of the error history over each epoch
self.error_history.append(np.average(np.abs(self.error)))
self.epoch_list.append(epoch)
Step 14: function to predict output on new and unseen input data
def predict(self, new_input):
prediction = self.sigmoid(np.dot(new_input, self.weights))
return prediction
Step 15: create neural network
NN = NeuralNetwork(inputs, outputs)
Step 16: train neural network
NN.train()
Step 17: create two new examples to predict
example = np.array([[1, 1, 0]])
example_2 = np.array([[0, 1, 1]])
Step 18: print the predictions for both examples
print(NN.predict(example), ‘ — Correct: ‘, example[0][0])
print(NN.predict(example_2), ‘ — Correct: ‘, example_2[0][0])
Step 19: plot the error over the entire training duration
plt.figure(figsize=(15,5))
plt.plot(NN.epoch_list, NN.error_history)
plt.xlabel(‘Epoch’)
plt.ylabel(‘Error’)
plt.show()
No comments:
Post a Comment