From 77380e3ef30a2039d54d6c50f912c7199b41311c Mon Sep 17 00:00:00 2001 From: George Lacey Date: Wed, 15 Feb 2017 12:36:42 +0000 Subject: [PATCH 1/6] Add drawCircle method --- Graph.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Graph.py b/Graph.py index 5a7b1cd..80ebc57 100644 --- a/Graph.py +++ b/Graph.py @@ -13,3 +13,10 @@ class Graph(object): def drawLine(self, p_point1, p_point2): Line(p_point1, p_point2).draw(self.win).move(self.x_offset, self.y_offset) + + def drawCircle(self, p_centre, p_radius, p_fill): + circle = Circle(p_centre, p_radius) + circle.setFill(p_fill) + circle.setOutline(p_fill) + circle.draw(self.win).move(self.x_offset, self.y_offset) + From ba994cceadafebfcf2daa1687573d494579463ce Mon Sep 17 00:00:00 2001 From: George Lacey Date: Wed, 15 Feb 2017 12:38:09 +0000 Subject: [PATCH 2/6] Plot predictions --- Main.py | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/Main.py b/Main.py index 4bce33d..46f6217 100644 --- a/Main.py +++ b/Main.py @@ -4,7 +4,7 @@ from Dataset import Dataset from Graph import * graph = Graph(640, 480, "Perceptron") -graph.drawLine(Point(-1000, -1000 + 20), Point(1000, 1000 + 20)) +graph.drawLine(Point(-1000, -1000 + 100), Point(1000, 1000 + 100)) rand = Random() data = Dataset(open("input.txt").read().split('\n'), open("target.txt").read().split('\n')) @@ -13,39 +13,51 @@ learn_rate = 0.01 p = Perceptron() i = data.inputs - def line_perceptron(): p.add_input("1", 1, rand.uniform(-1, 1)) p.add_input("2", 1, rand.uniform(-1, 1)) + p.add_input("bias", 1, rand.uniform(-1, 1)) def answer(p_x, p_y): - if p_y > calc_y(p_x): + if p_y > calc_y(p_x) return 1 else: return -1 - def calc_y(p_x): - return p_x + 2 + return p_x + 100 - for ind in range(0, 1000): - x = rand.randint(-640 / 2, 640 / 2) - y = rand.randint(-480 / 2, 480 / 2) + for ind in range(0, 100): + x = rand.randint(-1000, 1000) + y = rand.randint(-1000, 1000) print(x, y) p.input(x, y) - print("error: %f" % p.guess(answer(x, y), learn_rate)) + z = answer(x, y) + print("error: %f" % p.guess(z, learn_rate)) - for i in range(0, 1000): + '''for i in range(0, 1000): x = int(input("x: ")) + if x == 9001: + break y = int(input("y: ")) p.input(x, y) if p.activation() == 1: - graph.win.plot(x, y, "green") - print("TRUE") + graph.drawCircle(Point(x, y), 4, "green") else: - graph.win.plot(x, y, "red") - print("FALSE") + graph.drawCircle(Point(x, y), 4, "red")''' + for i in range(0, 1000000): + x = int(input()) + y = int(input()) + if i == 500: + y = 8 + p.input(x, y) + if p.activation() == 1: + graph.drawCircle(Point(x, y), 4, "green") + else: + graph.drawCircle(Point(x, y), 4, "red") + + input() def and_perceptron(): p.add_input("1", 1, rand.uniform(-1, 1)) @@ -68,4 +80,4 @@ def and_perceptron(): else: print("FALSE") -line_perceptron() \ No newline at end of file +line_perceptron() From b607814e0ffc2240e8392ad1f524674ca322abed Mon Sep 17 00:00:00 2001 From: George Lacey Date: Wed, 15 Feb 2017 12:39:43 +0000 Subject: [PATCH 3/6] Add missing colon --- Main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Main.py b/Main.py index 46f6217..68dabca 100644 --- a/Main.py +++ b/Main.py @@ -19,7 +19,7 @@ def line_perceptron(): p.add_input("bias", 1, rand.uniform(-1, 1)) def answer(p_x, p_y): - if p_y > calc_y(p_x) + if p_y > calc_y(p_x): return 1 else: return -1 From 80b2afed598ecf6dc1961765a44cd028c645f7d5 Mon Sep 17 00:00:00 2001 From: George Lacey Date: Wed, 15 Feb 2017 12:48:22 +0000 Subject: [PATCH 4/6] Generate random input --- Main.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Main.py b/Main.py index 68dabca..fe13f96 100644 --- a/Main.py +++ b/Main.py @@ -27,7 +27,7 @@ def line_perceptron(): def calc_y(p_x): return p_x + 100 - for ind in range(0, 100): + for ind in range(0, 1000): x = rand.randint(-1000, 1000) y = rand.randint(-1000, 1000) print(x, y) @@ -47,15 +47,15 @@ def line_perceptron(): graph.drawCircle(Point(x, y), 4, "red")''' for i in range(0, 1000000): - x = int(input()) - y = int(input()) - if i == 500: - y = 8 - p.input(x, y) - if p.activation() == 1: - graph.drawCircle(Point(x, y), 4, "green") - else: - graph.drawCircle(Point(x, y), 4, "red") + x = rand.randint(-320, 320) + y = rand.randint(-240, 240) + p.input(x, y) + if p.activation() == 1: + graph.drawCircle(Point(x, y), 4, "green") + else: + graph.drawCircle(Point(x, y), 4, "red") + if i % 1000 == 0: + graph.drawLine(Point(-1000, -1000 + 100), Point(1000, 1000 + 100)) input() From ad1ef4d7e730e73b43667588bf3fe04ee4ae2acb Mon Sep 17 00:00:00 2001 From: George Lacey Date: Wed, 15 Feb 2017 12:51:16 +0000 Subject: [PATCH 5/6] Tidy code --- Main.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Main.py b/Main.py index fe13f96..ccc12c1 100644 --- a/Main.py +++ b/Main.py @@ -3,17 +3,15 @@ from random import Random from Dataset import Dataset from Graph import * -graph = Graph(640, 480, "Perceptron") -graph.drawLine(Point(-1000, -1000 + 100), Point(1000, 1000 + 100)) - rand = Random() -data = Dataset(open("input.txt").read().split('\n'), open("target.txt").read().split('\n')) learn_rate = 0.01 p = Perceptron() -i = data.inputs def line_perceptron(): + graph = Graph(640, 480, "Perceptron") + graph.drawLine(Point(-1000, -1000 + 100), Point(1000, 1000 + 100)) + p.add_input("1", 1, rand.uniform(-1, 1)) p.add_input("2", 1, rand.uniform(-1, 1)) p.add_input("bias", 1, rand.uniform(-1, 1)) @@ -60,6 +58,9 @@ def line_perceptron(): input() def and_perceptron(): + data = Dataset(open("input.txt").read().split('\n'), open("target.txt").read().split('\n')) + i = data.inputs + p.add_input("1", 1, rand.uniform(-1, 1)) p.add_input("2", 1, rand.uniform(-1, 1)) p.add_input("bias", 1, rand.uniform(-1, 1)) From bc1327a8bacff40c1cd9ea97c8934a10a2bf94ca Mon Sep 17 00:00:00 2001 From: George Lacey Date: Wed, 22 Feb 2017 11:29:33 +0000 Subject: [PATCH 6/6] Add input and target data examples --- input.txt | 4 ++++ target.txt | 4 ++++ 2 files changed, 8 insertions(+) create mode 100644 input.txt create mode 100644 target.txt diff --git a/input.txt b/input.txt new file mode 100644 index 0000000..ee53213 --- /dev/null +++ b/input.txt @@ -0,0 +1,4 @@ +0, 0 +1, 0 +0, 1 +1, 1 \ No newline at end of file diff --git a/target.txt b/target.txt new file mode 100644 index 0000000..99470a5 --- /dev/null +++ b/target.txt @@ -0,0 +1,4 @@ +-1 +-1 +-1 +1 \ No newline at end of file