Preceptron

`class Perceptron(ptype='or', learning_rate=0.01, iterations=100)`

Parameters

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()

``````
``````

Starting 100 rounds of training. Initial weights: [ 0.8798246   0.8749459   0.03409441]

Training round 1/100: New weights - [ 0.87193595  0.86705725  0.02620576] | Error - 78.89%
Training round 2/100: New weights - [ 0.88214922  0.85684398  0.01599249] | Error - 102.13%
Training round 3/100: New weights - [ 0.87173625  0.86725696  0.00557951] | Error - 104.13%
Training round 4/100: New weights - [ 0.86440211  0.85992282  0.01291365] | Error - 73.34%
Training round 5/100: New weights - [ 0.85422818  0.87009675  0.00273972] | Error - 101.74%
Training round 6/100: New weights - [ 0.86441426  0.85991066 -0.00744636] | Error - 101.86%
Training round 7/100: New weights - [ 0.87429476  0.85003016 -0.01732686] | Error - 98.81%
Training round 8/100: New weights - [ 0.86422539  0.86009954 -0.02739624] | Error - 100.69%
Training round 9/100: New weights - [ 0.8572561   0.85313025 -0.03436553] | Error - 69.69%
Training round 10/100: New weights - [ 0.85049589  0.84637005 -0.04112574] | Error - 67.6%
Training round 11/100: New weights - [ 0.86004337  0.83682256 -0.05067322] | Error - 95.47%
Training round 12/100: New weights - [ 0.85358145  0.83036064 -0.05713515] | Error - 64.62%
Training round 13/100: New weights - [ 0.84392059  0.84002149 -0.066796  ] | Error - 96.61%
Training round 14/100: New weights - [ 0.83454956  0.84939252 -0.07616703] | Error - 93.71%
Training round 15/100: New weights - [ 0.82847181  0.84331477 -0.08224479] | Error - 60.78%
Training round 16/100: New weights - [ 0.8209315   0.83577446 -0.07470447] | Error - 75.4%
Training round 17/100: New weights - [ 0.81361739  0.82846035 -0.06739037] | Error - 73.14%
Training round 18/100: New weights - [ 0.82309192  0.81898583 -0.07686489] | Error - 94.75%
Training round 19/100: New weights - [ 0.81590249  0.8117964  -0.06967547] | Error - 71.89%
Training round 20/100: New weights - [ 0.81032226  0.80621617 -0.0752557 ] | Error - 55.8%
Training round 21/100: New weights - [ 0.81952864  0.79700979 -0.08446208] | Error - 92.06%
Training round 22/100: New weights - [ 0.82845883  0.7880796  -0.09339227] | Error - 89.3%
Training round 23/100: New weights - [ 0.81898896  0.79754946 -0.10286214] | Error - 94.7%
Training round 24/100: New weights - [ 0.8138522   0.7924127  -0.10799891] | Error - 51.37%
Training round 25/100: New weights - [ 0.80670956  0.78527006 -0.10085627] | Error - 71.43%
Training round 26/100: New weights - [ 0.80179833  0.78035883 -0.1057675 ] | Error - 49.11%
Training round 27/100: New weights - [ 0.79264161  0.78951555 -0.11492422] | Error - 91.57%
Training round 28/100: New weights - [ 0.8014611   0.78069605 -0.12374372] | Error - 88.19%
Training round 29/100: New weights - [ 0.79687697  0.77611192 -0.12832785] | Error - 45.84%
Training round 30/100: New weights - [ 0.7879526   0.78503629 -0.13725223] | Error - 89.24%
Training round 31/100: New weights - [ 0.78085019  0.77793388 -0.13014981] | Error - 71.02%
Training round 32/100: New weights - [ 0.77656384  0.77364754 -0.13443616] | Error - 42.86%
Training round 33/100: New weights - [ 0.76787904  0.78233234 -0.14312096] | Error - 86.85%
Training round 34/100: New weights - [ 0.76380814  0.77826143 -0.14719186] | Error - 40.71%
Training round 35/100: New weights - [ 0.75691552  0.77136882 -0.14029925] | Error - 68.93%
Training round 36/100: New weights - [ 0.75022969  0.76468298 -0.13361341] | Error - 66.86%
Training round 37/100: New weights - [ 0.75903809  0.75587459 -0.14242181] | Error - 88.08%
Training round 38/100: New weights - [ 0.75531318  0.75214968 -0.14614672] | Error - 37.25%
Training round 39/100: New weights - [ 0.75170002  0.74853652 -0.14975988] | Error - 36.13%
Training round 40/100: New weights - [ 0.74819525  0.74503175 -0.15326465] | Error - 35.05%
Training round 41/100: New weights - [ 0.73969626  0.75353074 -0.16176364] | Error - 84.99%
Training round 42/100: New weights - [ 0.73145224  0.76177476 -0.17000765] | Error - 82.44%
Training round 43/100: New weights - [ 0.74005539  0.75317161 -0.1786108 ] | Error - 86.03%
Training round 44/100: New weights - [ 0.73197266  0.76125434 -0.18669353] | Error - 80.83%
Training round 45/100: New weights - [ 0.72413241  0.76909459 -0.19453378] | Error - 78.4%
Training round 46/100: New weights - [ 0.71652737  0.77669963 -0.20213882] | Error - 76.05%
Training round 47/100: New weights - [ 0.70915048  0.78407651 -0.20951571] | Error - 73.77%
Training round 48/100: New weights - [ 0.70212306  0.77704909 -0.20248828] | Error - 70.27%
Training round 49/100: New weights - [ 0.69935622  0.77428225 -0.20525512] | Error - 27.67%
Training round 50/100: New weights - [ 0.69667238  0.77159842 -0.20793896] | Error - 26.84%
Training round 51/100: New weights - [ 0.69406907  0.7689951  -0.21054227] | Error - 26.03%
Training round 52/100: New weights - [ 0.69154385  0.76646988 -0.21306749] | Error - 25.25%
Training round 53/100: New weights - [ 0.68483303  0.75975907 -0.20635668] | Error - 67.11%
Training round 54/100: New weights - [ 0.69351873  0.75107337 -0.21504237] | Error - 86.86%
Training round 55/100: New weights - [ 0.68692238  0.74447703 -0.20844603] | Error - 65.96%
Training round 56/100: New weights - [ 0.67958239  0.75181702 -0.21578602] | Error - 73.4%
Training round 57/100: New weights - [ 0.6724626   0.75893681 -0.22290582] | Error - 71.2%
Training round 58/100: New weights - [ 0.6655564   0.76584301 -0.22981202] | Error - 69.06%
Training round 59/100: New weights - [ 0.65885738  0.77254203 -0.23651103] | Error - 66.99%
Training round 60/100: New weights - [ 0.65235934  0.77904007 -0.24300907] | Error - 64.98%
Training round 61/100: New weights - [ 0.66119606  0.77020335 -0.25184579] | Error - 88.37%
Training round 62/100: New weights - [ 0.66976767  0.76163174 -0.2604174 ] | Error - 85.72%
Training round 63/100: New weights - [ 0.66329049  0.76810892 -0.26689459] | Error - 64.77%
Training round 64/100: New weights - [ 0.65700762  0.77439179 -0.27317746] | Error - 62.83%
Training round 65/100: New weights - [ 0.64996185  0.76734603 -0.26613169] | Error - 70.46%
Training round 66/100: New weights - [ 0.64312745  0.76051163 -0.2592973 ] | Error - 68.34%
Training round 67/100: New weights - [ 0.64168403  0.75906821 -0.26074071] | Error - 14.43%
Training round 68/100: New weights - [ 0.64028392  0.7576681  -0.26214083] | Error - 14.0%
Training round 69/100: New weights - [ 0.63407917  0.76387285 -0.26834558] | Error - 62.05%
Training round 70/100: New weights - [ 0.6327831   0.76257678 -0.26964164] | Error - 12.96%
Training round 71/100: New weights - [ 0.62613309  0.75592677 -0.26299163] | Error - 66.5%
Training round 72/100: New weights - [ 0.62494241  0.75473609 -0.26418231] | Error - 11.91%
Training round 73/100: New weights - [ 0.63359852  0.74607997 -0.27283842] | Error - 86.56%
Training round 74/100: New weights - [ 0.64199495  0.73768354 -0.28123485] | Error - 83.96%
Training round 75/100: New weights - [ 0.63538582  0.73107441 -0.27462572] | Error - 66.09%
Training round 76/100: New weights - [ 0.64359645  0.72286378 -0.28283635] | Error - 82.11%
Training round 77/100: New weights - [ 0.63710348  0.71637081 -0.27634338] | Error - 64.93%
Training round 78/100: New weights - [ 0.6308053   0.71007264 -0.27004521] | Error - 62.98%
Training round 79/100: New weights - [ 0.62429843  0.71657951 -0.27655208] | Error - 65.07%
Training round 80/100: New weights - [ 0.63245572  0.70842222 -0.28470937] | Error - 81.57%
Training round 81/100: New weights - [ 0.63189403  0.70786054 -0.28527106] | Error - 5.62%
Training round 82/100: New weights - [ 0.63980099  0.69995358 -0.29317801] | Error - 79.07%
Training round 83/100: New weights - [ 0.64747073  0.69228384 -0.30084776] | Error - 76.7%
Training round 84/100: New weights - [ 0.65491039  0.68484418 -0.30828741] | Error - 74.4%
Training round 85/100: New weights - [ 0.6482926   0.69146197 -0.3149052 ] | Error - 66.18%
Training round 86/100: New weights - [ 0.65557524  0.68417933 -0.32218784] | Error - 72.83%
Training round 87/100: New weights - [ 0.65539957  0.68400366 -0.32236351] | Error - 1.76%
Training round 88/100: New weights - [ 0.64890925  0.69049398 -0.32885383] | Error - 64.9%
Training round 89/100: New weights - [ 0.65603656  0.68336668 -0.33598114] | Error - 71.27%
Training round 90/100: New weights - [ 0.66295005  0.67645319 -0.34289463] | Error - 69.13%
Training round 91/100: New weights - [ 0.65612707  0.66963021 -0.33607165] | Error - 68.23%
Training round 92/100: New weights - [ 0.65623021  0.66973335 -0.33596851] | Error - 1.03%
Training round 93/100: New weights - [ 0.64972493  0.67623863 -0.34247379] | Error - 65.05%
Training round 94/100: New weights - [ 0.64989003  0.67640374 -0.34230869] | Error - 1.65%
Training round 95/100: New weights - [ 0.65673208  0.66956169 -0.34915074] | Error - 68.42%
Training round 96/100: New weights - [ 0.64997764  0.66280724 -0.34239629] | Error - 67.54%
Training round 97/100: New weights - [ 0.65668197  0.65610291 -0.34910063] | Error - 67.04%
Training round 98/100: New weights - [ 0.65704513  0.65646607 -0.34873747] | Error - 3.63%
Training round 99/100: New weights - [ 0.66355196  0.64995923 -0.3552443 ] | Error - 65.07%
Training round 100/100: New weights - [ 0.65686441  0.64327168 -0.34855675] | Error - 66.88%

Final weights are - [ 0.65686441  0.64327168]
The bias node weight is: -0.34855674962222094

``````
``````

In [57]:

perceptron.predict((1,-1))

``````
``````

Actual prediction (using sigmoid function): 1.0

Out[57]:

1.0

``````
``````

In [ ]:

``````