From f9d4748e9cfc56b612a8704400c7feb8eaacc2e5 Mon Sep 17 00:00:00 2001 From: George Lacey Date: Fri, 10 Feb 2017 13:51:00 +0000 Subject: [PATCH] Add input method and implement learning constant --- Perceptron.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Perceptron.py b/Perceptron.py index f499cc0..382867f 100644 --- a/Perceptron.py +++ b/Perceptron.py @@ -1,5 +1,6 @@ from Input import Input + class Perceptron(object): def __init__(self): @@ -10,6 +11,10 @@ class Perceptron(object): print(key + ":") self.inputs[key].print() + def input(self, p_input1, p_input2): + self.inputs[0] = p_input1 + self.inputs[1] = p_input2 + def add_input(self, p_name, p_value, p_weight): self.inputs[p_name] = Input(p_value, p_weight) @@ -24,14 +29,16 @@ class Perceptron(object): def activation(self): input = self._sum_inputs() - print("Total = %f" % input) + print("Sum of inputs = %f" % input) if input > 0: return 1 else: return -1 - def guess(self, p_desired): + def guess(self, p_desired, p_learning_constant): prediction = float(self.activation()) - return p_desired - prediction - + error = p_desired - prediction + for key in self.inputs: + self.inputs[key].weight += error * self.inputs[key].value * p_learning_constant + return error