问题描述
from numpy import array, dot
from random import choice
from pylab import ylim, plot
from matplotlib import pyplot as plt
step_function = lambda x: 0 if x < 0 else 1
training_dataset = [
(array([0,0,1]), 0),
(array([0,1,1]), 1),
(array([1,0,1]), 1),
(array([1,1,1]), 1),
]
weights = input("Enter your value: ")
error = []
learning_rate = input("Enter your value: ")
n = 100
for j in range(n):
x, expected = choice(training_dataset)
result = (dot(weights, x)
err = expected - step_function(result)
error.append(err)
weights += learning_rate * err * x
for x, _ in training_dataset:
result = dot(x, weights)
print("{}: {} -> {}".format(x[:2], result, step_function(result)))
ylim([-1,1])
plot(error)
plt.show()
标签: python-3.x