In [3]:
import numpy as np
import matplotlib as plt
In [20]:
class Perceptron(object):
def __init__(self, n):
self.n = n
lc = 0.05
weights = np.random.rand(n) * 2 - 1
print(weights)
def activate(self, sum_):
if sum_ > 0:
return 1
else:
return 0
def feedforward(self, inputs):
sum_ = 0
for i in range(len(weights)):
sum_ += inputs[i]*weights[i]
return activate(sum_)
def train(self, inputs, desired):
guess = feedforward(inputs)
error = desired - guess
for i in range(len(weights)):
weights[i] = lc*error*inputs[i]
class Trainer(object):
def __init__(self, x, y, answer):
self.x = x
self.y = y
self.answer = answer
inputs = [x, y, 1]
answer = answer
def func(x):
return 2*x + 1
x = np.random.rand() * 10 - 5
y = np.random.rand() * 10 - 5
yline = func(x)
if y < yline:
answer = -1
else:
answer = 1
t = Trainer(x, y, answer)
ptron = Perceptron(3)
In [ ]:
In [ ]: