I used two different codes to make the perceptron. The first is from this blog post and the second is from some work I did with Stephanie Koulton.
import numpy as np
X = np.array([
[-2,4,-1],
[4,1,-1],
[1, 6, -1],
[2, 4, -1],
[6, 2, -1],
])
y = np.array([-1,-1,1,1,1])
def perceptron_sgd(X, Y):
w = np.zeros(len(X[0]))
eta = 1
epochs = 20
for t in range(epochs):
for i, x in enumerate(X):
if (np.dot(X[i], w)*Y[i]) <= 0:
w = w + eta*X[i]*Y[i]
return w
w = perceptron_sgd(X,y)
print(w)
And here is some of the code I created with Stephanie
import random
import numpy as np
def guess(sumtotal):
if sumtotal < 0:
return 0
else:
return 0
train_data = np.array([
([0,0],0),
([0,1],1),
([1,0],1),
([1,1],1)
])
biasinput = 1
weight = np.random.rand(2)
biasweight = np.random.rand(1)
const = 0.2
def perceptron():
x, expected = random.choice(train_data)
result = np.dot(x,weight) + np.dot(biasinput, biasweight)
print(result)
error = expected – guess(result)
print(error)
errors.append(error)
newweight = const*error*x
#delta = np.dot(x, error)
#print(delta)
#weight = weight + delta * const
#print(w)
perceptron()