class Perceptron(ptype='or', learning_rate=0.01, iterations=100)
ptype [optional]: string
The type of the perceptron - 'or', 'and' or 'xor'.
Default = 'or'
learning_rate [optional]: float
The perceptron learning rate
Default = 0.01
iterations [optional]: int
The number of learning iterations the perception will go through. Each iteration uses a random input from the perceptron 'truth table' based on the perceptron type (ptype).
Default = 100
In [58]:
import numpy as np
import math
class Perceptron():
def __init__(self, input_dimensions=2, ptype='or', learning_rate=0.01, iterations=100):
self.ptype = ptype
self.input_dimensions = input_dimensions
if ptype == 'or':
self.training = np.array([[(-1,-1), -1],
[(1,-1), 1],
[(-1,1), 1],
[(1,1), 1],])
elif ptype == 'and':
self.training = np.array([[(-1,-1), -1],
[(1,-1), -1],
[(-1,1), -1],
[(1,1), 1],])
elif ptype == 'xor':
self.training = np.array([[(-1,-1), -1],
[(1,-1), 1],
[(-1,1), 1],
[(1,1), -1],])
self.learning_rate = learning_rate
self.iterations = iterations
self.weights = np.random.uniform(0, 1, size=input_dimensions+1)
print ('=> \"{}\" perceptron is ready!'.format(self.ptype))
def train(self):
print ('Starting {} rounds of training. Initial weights: {}\n'.format(self.iterations, self.weights))
for i in range(self.iterations):
point = self.training[np.random.randint(0,self.training.shape[0])]
input_values = point[0]
expected_result = point[1]
current_result = input_values[0]*self.weights[0] + input_values[1]*self.weights[1] + 1*self.weights[self.input_dimensions]
error = expected_result - current_result
error_percent = abs(error*100)
for w in range(len(self.weights)-1):
self.weights[w] += error * input_values[w] * self.learning_rate
self.weights[self.input_dimensions] += error * 1 * self.learning_rate
print ('Training round {}/{}: New weights - {} | Error - {}%'.format(i+1, self.iterations, self.weights, round(error_percent,2)))
print ('\nFinal weights are - {}\nThe bias node weight is: {}'.format(self.weights[:self.input_dimensions], self.weights[self.input_dimensions]))
def predict(self, values=(1,1)):
# raw_prediction = values[0]*self.weights[0] + values[1]*self.weights[1] + 1*self.weights[2]
# actual_prediction = int(round(raw_prediction))
current_result = np.sign( values[0]*self.weights[0] + values[1]*self.weights[1] + 1*self.weights[self.input_dimensions] )
# print ('Perceptron type: "{}"'.format(self.ptype))
# print ('Raw prediction value:', raw_prediction)
print ('Actual prediction (using sigmoid function):', current_result)
return current_result
In [59]:
perceptron = Perceptron(ptype='and', iterations=100)
In [60]:
perceptron.train()
In [57]:
perceptron.predict((1,-1))
Out[57]:
In [ ]: