Move separate perceptron code into methods
This commit is contained in:
parent
e657953465
commit
9cc2c3a4e8
79
Main.py
79
Main.py
|
@ -3,30 +3,69 @@ from random import Random
|
||||||
from Dataset import Dataset
|
from Dataset import Dataset
|
||||||
from Graph import *
|
from Graph import *
|
||||||
|
|
||||||
rand = Random()
|
|
||||||
data = Dataset(open("input.txt").read().split('\n'), open("target.txt").read().split('\n'))
|
|
||||||
learn_rate = 0.01
|
|
||||||
graph = Graph(640, 480, "Perceptron")
|
graph = Graph(640, 480, "Perceptron")
|
||||||
graph.drawLine(Point(-1000, -1000 + 20), Point(1000, 1000 + 20))
|
graph.drawLine(Point(-1000, -1000 + 20), Point(1000, 1000 + 20))
|
||||||
|
|
||||||
|
rand = Random()
|
||||||
|
data = Dataset(open("input.txt").read().split('\n'), open("target.txt").read().split('\n'))
|
||||||
|
learn_rate = 0.01
|
||||||
|
|
||||||
p = Perceptron()
|
p = Perceptron()
|
||||||
i = data.inputs
|
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))
|
|
||||||
|
|
||||||
# train
|
|
||||||
for ind in range(0, 500):
|
|
||||||
i = data.inputs
|
|
||||||
for key in data.inputs:
|
|
||||||
p.input(i[key].value1, i[key].value2)
|
|
||||||
print("error: %f" % p.guess(i[key].target, learn_rate))
|
|
||||||
|
|
||||||
for i in range(0, 1000):
|
def line_perceptron():
|
||||||
x = input("Arg1: ")
|
p.add_input("1", 1, rand.uniform(-1, 1))
|
||||||
y = input("Arg2: ")
|
p.add_input("2", 1, rand.uniform(-1, 1))
|
||||||
p.input(x, y)
|
|
||||||
if p.activation() == 1:
|
def answer(p_x, p_y):
|
||||||
print("TRUE")
|
if p_y > calc_y(p_x):
|
||||||
else:
|
return 1
|
||||||
print("FALSE")
|
else:
|
||||||
|
return -1
|
||||||
|
|
||||||
|
|
||||||
|
def calc_y(p_x):
|
||||||
|
return p_x + 2
|
||||||
|
|
||||||
|
for ind in range(0, 1000):
|
||||||
|
x = rand.randint(-640 / 2, 640 / 2)
|
||||||
|
y = rand.randint(-480 / 2, 480 / 2)
|
||||||
|
print(x, y)
|
||||||
|
p.input(x, y)
|
||||||
|
print("error: %f" % p.guess(answer(x, y), learn_rate))
|
||||||
|
|
||||||
|
for i in range(0, 1000):
|
||||||
|
x = int(input("x: "))
|
||||||
|
y = int(input("y: "))
|
||||||
|
p.input(x, y)
|
||||||
|
if p.activation() == 1:
|
||||||
|
graph.win.plot(x, y, "green")
|
||||||
|
print("TRUE")
|
||||||
|
else:
|
||||||
|
graph.win.plot(x, y, "red")
|
||||||
|
print("FALSE")
|
||||||
|
|
||||||
|
|
||||||
|
def and_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))
|
||||||
|
|
||||||
|
# train
|
||||||
|
for ind in range(0, 500):
|
||||||
|
i = data.inputs
|
||||||
|
for key in data.inputs:
|
||||||
|
p.input(i[key].value1, i[key].value2)
|
||||||
|
print("error: %f" % p.guess(i[key].target, learn_rate))
|
||||||
|
|
||||||
|
for i in range(0, 1000):
|
||||||
|
x = input("Arg1: ")
|
||||||
|
y = input("Arg2: ")
|
||||||
|
p.input(x, y)
|
||||||
|
if p.activation() == 1:
|
||||||
|
print("TRUE")
|
||||||
|
else:
|
||||||
|
print("FALSE")
|
||||||
|
|
||||||
|
line_perceptron()
|
Loading…
Reference in New Issue
Block a user