From 3c939a264e9403b4ecfd7249864424d26362bdc4 Mon Sep 17 00:00:00 2001 From: George Lacey Date: Wed, 8 Feb 2017 23:17:26 +0000 Subject: [PATCH 01/14] Initial commit --- Input.py | 11 +++++++++++ Main.py | 0 Perceptron.py | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 Input.py create mode 100644 Main.py create mode 100644 Perceptron.py diff --git a/Input.py b/Input.py new file mode 100644 index 0000000..ae7c255 --- /dev/null +++ b/Input.py @@ -0,0 +1,11 @@ +class Input(object): + + def __init__(self, p_value, p_weight): + self.value = p_value + self.weight = p_weight + + def print(self): + print("value: %f\nweight: %f") % (self.value, self.weight) + + def setweight(self, p_weight): + self.weight = p_weight \ No newline at end of file diff --git a/Main.py b/Main.py new file mode 100644 index 0000000..e69de29 diff --git a/Perceptron.py b/Perceptron.py new file mode 100644 index 0000000..820a11f --- /dev/null +++ b/Perceptron.py @@ -0,0 +1,18 @@ +from Input import Input + + +class Perceptron(object): + + def __init__(self): + self.inputs = {} + + def print(self): + for key, value in self.inputs: + print("%s - )") % key + value.print() + + def addinput(self, p_name, p_value, p_weight): + self.inputs[p_name] = Input(p_value, p_weight) + + def setweight(self, p_input, p_weight): + self.inputs[p_input].weight = p_weight From 8fc14e1e0064768e15a91fea86c0dfd1835f826e Mon Sep 17 00:00:00 2001 From: George Lacey Date: Thu, 9 Feb 2017 19:04:52 +0000 Subject: [PATCH 02/14] Ignore IDE files --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bc8a670 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/* \ No newline at end of file From 4edf73d6741c053283c3b4ca9624d42a1039dcdf Mon Sep 17 00:00:00 2001 From: George Lacey Date: Thu, 9 Feb 2017 19:05:32 +0000 Subject: [PATCH 03/14] Fix print method, generate output --- Input.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Input.py b/Input.py index ae7c255..1f1c49c 100644 --- a/Input.py +++ b/Input.py @@ -5,7 +5,10 @@ class Input(object): self.weight = p_weight def print(self): - print("value: %f\nweight: %f") % (self.value, self.weight) + print("value: %f\tweight: %f" % (self.value, self.weight)) - def setweight(self, p_weight): - self.weight = p_weight \ No newline at end of file + def set_weight(self, p_weight): + self.weight = p_weight + + def output(self): + return self.value * self.weight From 9e8d0d290677cd33c8fd8077d2cc07cf554318f5 Mon Sep 17 00:00:00 2001 From: George Lacey Date: Thu, 9 Feb 2017 19:06:01 +0000 Subject: [PATCH 04/14] Add activation method, fix printing --- Perceptron.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/Perceptron.py b/Perceptron.py index 820a11f..327d17e 100644 --- a/Perceptron.py +++ b/Perceptron.py @@ -1,18 +1,32 @@ from Input import Input - class Perceptron(object): def __init__(self): self.inputs = {} def print(self): - for key, value in self.inputs: - print("%s - )") % key - value.print() + for key in self.inputs: + print(key + ":") + self.inputs[key].print() - def addinput(self, p_name, p_value, p_weight): + def add_input(self, p_name, p_value, p_weight): self.inputs[p_name] = Input(p_value, p_weight) - def setweight(self, p_input, p_weight): + def set_weight(self, p_input, p_weight): self.inputs[p_input].weight = p_weight + + def _sum_inputs(self): + total = 0 + for key, value in self.inputs.items(): + total += value.output() + return total + + def activation(self): + input = self._sum_inputs() + print("Total = %f" % input) + + if input > 0: + return True + else: + return False From 878c2c857a3bc49b3325685f7874bd1d3f5be9a8 Mon Sep 17 00:00:00 2001 From: George Lacey <490796@hull.ac.uk> Date: Fri, 10 Feb 2017 12:11:46 +0000 Subject: [PATCH 05/14] Create Data class --- Data.py | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 Data.py diff --git a/Data.py b/Data.py new file mode 100644 index 0000000..29ff058 --- /dev/null +++ b/Data.py @@ -0,0 +1,6 @@ +class Data(object): + + def __init__(self, p_value1, p_value2, p_target): + self.value1 = p_value1 + self.value2 = p_value2 + self.target = p_target From 64286d6aa65ed2071fcf529a13a795b601937b46 Mon Sep 17 00:00:00 2001 From: George Lacey <490796@hull.ac.uk> Date: Fri, 10 Feb 2017 12:12:16 +0000 Subject: [PATCH 06/14] Create Dataset class --- Dataset.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Dataset.py diff --git a/Dataset.py b/Dataset.py new file mode 100644 index 0000000..864ee5b --- /dev/null +++ b/Dataset.py @@ -0,0 +1,16 @@ +from Data import Data + + +class Dataset(object): + + def __init__(self, p_input, p_target): + self.inputs = {} + + for index, item in enumerate(p_input): + input1 = float(p_input[index].split(',')[0].strip()) + input2 = float(p_input[index].split(',')[1].strip()) + if p_target[index] == "1": + output = 1 + else: + output = -1 + self.inputs[index] = Data(input1, input2, output) From d91911691afddba8a3aba0e2766259135446a8cf Mon Sep 17 00:00:00 2001 From: George Lacey <490796@hull.ac.uk> Date: Fri, 10 Feb 2017 12:12:44 +0000 Subject: [PATCH 07/14] Implement guess method --- Perceptron.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Perceptron.py b/Perceptron.py index 327d17e..f499cc0 100644 --- a/Perceptron.py +++ b/Perceptron.py @@ -27,6 +27,11 @@ class Perceptron(object): print("Total = %f" % input) if input > 0: - return True + return 1 else: - return False + return -1 + + def guess(self, p_desired): + prediction = float(self.activation()) + return p_desired - prediction + From e4accc5927f73fa34444ed0f4ff9d838b15bf724 Mon Sep 17 00:00:00 2001 From: George Lacey Date: Fri, 10 Feb 2017 12:16:53 +0000 Subject: [PATCH 08/14] Add Main --- Main.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Main.py b/Main.py index e69de29..7bcc4db 100644 --- a/Main.py +++ b/Main.py @@ -0,0 +1,16 @@ +from Perceptron import Perceptron +from random import Random +from Dataset import Dataset + +rand = Random() +data = Dataset(open("input.txt").read().split('\n'), open("target.txt").read().split('\n')) + + +for key in data.inputs: + i = data.inputs + print("v1: ", i[key].value1, "v2: ", i[key].value2, "target: ", i[key].target) + p = Perceptron() + p.add_input("1", i[key].value1, rand.uniform(-1, 1)) + p.add_input("2", i[key].value2, rand.uniform(-1, 1)) + p.add_input("bias", 1, rand.uniform(-1, 1)) + print("%f" % p.guess(i[key].target)) From a950e3e99c6000ad4b26db2ea0845c00886c0a86 Mon Sep 17 00:00:00 2001 From: George Lacey Date: Fri, 10 Feb 2017 12:12:16 +0000 Subject: [PATCH 09/14] Create Dataset class --- Dataset.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Dataset.py diff --git a/Dataset.py b/Dataset.py new file mode 100644 index 0000000..864ee5b --- /dev/null +++ b/Dataset.py @@ -0,0 +1,16 @@ +from Data import Data + + +class Dataset(object): + + def __init__(self, p_input, p_target): + self.inputs = {} + + for index, item in enumerate(p_input): + input1 = float(p_input[index].split(',')[0].strip()) + input2 = float(p_input[index].split(',')[1].strip()) + if p_target[index] == "1": + output = 1 + else: + output = -1 + self.inputs[index] = Data(input1, input2, output) From d1b8f731d418af87c1d204b7fa6237d6191ddfd2 Mon Sep 17 00:00:00 2001 From: George Lacey Date: Fri, 10 Feb 2017 12:12:44 +0000 Subject: [PATCH 10/14] Implement guess method --- Perceptron.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Perceptron.py b/Perceptron.py index 327d17e..f499cc0 100644 --- a/Perceptron.py +++ b/Perceptron.py @@ -27,6 +27,11 @@ class Perceptron(object): print("Total = %f" % input) if input > 0: - return True + return 1 else: - return False + return -1 + + def guess(self, p_desired): + prediction = float(self.activation()) + return p_desired - prediction + From de0be12ccbce5f313f47bd5b1056f4cba01f8255 Mon Sep 17 00:00:00 2001 From: George Lacey Date: Fri, 10 Feb 2017 12:16:53 +0000 Subject: [PATCH 11/14] Implement basic functionality --- Main.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Main.py b/Main.py index e69de29..7bcc4db 100644 --- a/Main.py +++ b/Main.py @@ -0,0 +1,16 @@ +from Perceptron import Perceptron +from random import Random +from Dataset import Dataset + +rand = Random() +data = Dataset(open("input.txt").read().split('\n'), open("target.txt").read().split('\n')) + + +for key in data.inputs: + i = data.inputs + print("v1: ", i[key].value1, "v2: ", i[key].value2, "target: ", i[key].target) + p = Perceptron() + p.add_input("1", i[key].value1, rand.uniform(-1, 1)) + p.add_input("2", i[key].value2, rand.uniform(-1, 1)) + p.add_input("bias", 1, rand.uniform(-1, 1)) + print("%f" % p.guess(i[key].target)) From ba0f5036057eda287480a7d744978308d39709dd Mon Sep 17 00:00:00 2001 From: George Lacey Date: Fri, 10 Feb 2017 12:12:16 +0000 Subject: [PATCH 12/14] Create Dataset class --- Dataset.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Dataset.py diff --git a/Dataset.py b/Dataset.py new file mode 100644 index 0000000..864ee5b --- /dev/null +++ b/Dataset.py @@ -0,0 +1,16 @@ +from Data import Data + + +class Dataset(object): + + def __init__(self, p_input, p_target): + self.inputs = {} + + for index, item in enumerate(p_input): + input1 = float(p_input[index].split(',')[0].strip()) + input2 = float(p_input[index].split(',')[1].strip()) + if p_target[index] == "1": + output = 1 + else: + output = -1 + self.inputs[index] = Data(input1, input2, output) From 833560968ebcddb10bb36f4b0c39369d8098346f Mon Sep 17 00:00:00 2001 From: George Lacey Date: Fri, 10 Feb 2017 12:12:44 +0000 Subject: [PATCH 13/14] Implement guess method --- Perceptron.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Perceptron.py b/Perceptron.py index 327d17e..f499cc0 100644 --- a/Perceptron.py +++ b/Perceptron.py @@ -27,6 +27,11 @@ class Perceptron(object): print("Total = %f" % input) if input > 0: - return True + return 1 else: - return False + return -1 + + def guess(self, p_desired): + prediction = float(self.activation()) + return p_desired - prediction + From e75627010d0e39366866707dff4adfe86639b8a6 Mon Sep 17 00:00:00 2001 From: George Lacey Date: Fri, 10 Feb 2017 12:16:53 +0000 Subject: [PATCH 14/14] Implement basic functionality --- Main.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Main.py b/Main.py index e69de29..7bcc4db 100644 --- a/Main.py +++ b/Main.py @@ -0,0 +1,16 @@ +from Perceptron import Perceptron +from random import Random +from Dataset import Dataset + +rand = Random() +data = Dataset(open("input.txt").read().split('\n'), open("target.txt").read().split('\n')) + + +for key in data.inputs: + i = data.inputs + print("v1: ", i[key].value1, "v2: ", i[key].value2, "target: ", i[key].target) + p = Perceptron() + p.add_input("1", i[key].value1, rand.uniform(-1, 1)) + p.add_input("2", i[key].value2, rand.uniform(-1, 1)) + p.add_input("bias", 1, rand.uniform(-1, 1)) + print("%f" % p.guess(i[key].target))