Skip to content

Instantly share code, notes, and snippets.

@tyrion
Created October 30, 2014 16:36
Show Gist options
  • Save tyrion/eb833e93314856a5d98a to your computer and use it in GitHub Desktop.
Save tyrion/eb833e93314856a5d98a to your computer and use it in GitHub Desktop.
Perceptron
from collections import namedtuple
Input = namedtuple('Input', ['index', 'value'])
def identity(x):
return x
def step(x):
return 1 if x >= 0 else -1
class OutputNeuron:
def __init__(self, weights, fn=identity):
self.order = len(weights)
self.weights = weights
self.inputs = [0] * self.order
self.fn = fn
def activate(self, input):
self.inputs[input.index] = input.value
return self.activate_next()
def activate_many(self, inputs):
for i, value in inputs:
self.inputs[i] = value
return self.activate_next()
def activate_all(self, inputs):
self.inputs = list(inputs)
return self.activate_next()
def activate_next(self):
return self.compute()
def compute(self):
return self.fn(sum(map(lambda x,y: x*y, self.weights, self.inputs)))
def train(self, table, h):
print('Train')
error = False
for inputs, output in table.items():
y = self.activate_all(inputs)
print(self.weights, inputs, y, output)
if y != output:
error = True
for i in range(self.order):
self.weights[i] += h * inputs[i] * output
if error:
self.train(table, h)
def __repr__(self):
return 'Neuron I:{} O:{}'.format(self.inputs, self.compute())
class Neuron(OutputNeuron):
def __init__(self, weights, synapsis, fn=identity):
super().__init__(weights, fn)
self.output = synapsis
def activate_next(self):
input = Input(self.output.index, self.compute())
return self.output.neuron.activate(input)
if __name__ == '__main__':
n = OutputNeuron([0, 0, 0], step)
print(n)
table = {
(1, -1, -1): -1,
(1, -1, 1): -1,
(1, 1, -1): -1,
(1, 1, 1): 1
}
n.train(table, 0.5)
print(n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment