-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrain.py
More file actions
87 lines (64 loc) · 2.04 KB
/
Train.py
File metadata and controls
87 lines (64 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from MLP import *
from LogicOperators import *
import random
def generatorLogicFunction(operator):
bools = [True, False]
while True:
x1 = bools[random.randint(0, 1)]
x2 = bools[random.randint(0, 1)]
target = operator.result(x1, x2)
result = [x1, x2, target]
# Bool to [0,1]
for i, value in enumerate(result):
if value:
result[i] = 1
else:
result[i] = 0
yield result
def getPerformance(mlp, operator, testSize=50):
THRESHOLD = 0.5
loss = 0
cntCorrect = 0
for i in range(testSize):
data = next(generatorLogicFunction(operator))
[x1, x2, target] = data
y = mlp.forward_step([data[0], data[1]])
y = y[0] # extract value
loss += (y - target) ** 2
# MLP said "this is true"
if y > THRESHOLD:
# correct?
if target == 1:
cntCorrect += 1
# MLP said "this is false"
else:
# correct?
if target == 0:
cntCorrect += 1
accuracy = cntCorrect / testSize
avgLoss = loss / testSize
return accuracy, avgLoss
def main():
epsilon = 1 # Learning rate
NUM_EPOCHS = 1000
operators = [XOR()]
for operator in operators:
mlp = MLP([2, 5, 1])
performance = []
loss = []
for i in range(NUM_EPOCHS):
# Measure
avgPerformance, avgLoss = getPerformance(mlp, operator)
performance.append(avgPerformance)
loss.append(avgLoss)
# Train
data = next(generatorLogicFunction(operator))
[x1, x2, target] = data
mlp.backprop_step([x1, x2], target, epsilon)
print(f"Average Performance: {avgPerformance}; Average Loss: {avgLoss}")
return
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("KeyboardInterrupt received.")