Skip to content

Instantly share code, notes, and snippets.

@amacgregor
Created February 20, 2017 00:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amacgregor/48343d13097f1b4963dd6b064f90204b to your computer and use it in GitHub Desktop.
Save amacgregor/48343d13097f1b4963dd6b064f90204b to your computer and use it in GitHub Desktop.
from random import choice
from numpy import array, dot, random
# Declare activation function
def stepFunction(value):
if value <= 0:
return 0
else:
return 1
# Define Training Data
training_data = [
(array([0,0,1]), 0),
(array([0,1,1]), 0),
(array([1,0,1]), 1),
(array([1,1,1]), 1)
]
# Define Test Data
test_data = [
(array([0,0,0])),
(array([0,1,0])),
(array([1,1,1])),
(array([0,0,1])),
(array([1,0,0]))
]
w = random.rand(3)
errors = []
eta = 0.2
n = 1000
print("Training Perceptron for {} iterations").format(n)
print("Starting weights: {}").format(w)
for i in xrange(n):
x, expected = choice(training_data)
result = dot(w, x)
error = expected - stepFunction(result)
errors.append(error)
w += eta * error * x
print ".",
print ""
print "Training completed"
print("Weights after training: {}").format(w)
print("Running trained Network against Test Data")
for x in test_data:
result = dot(x, w)
print("{}: {} -> {}".format(x[:3], result, stepFunction(result)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment