-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
147 lines (107 loc) · 4.1 KB
/
main.py
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from lib import *
def mnist():
from mnist_data import train_data_x, train_data_y, test_data_x, test_data_y
mnist = Model(input_shape=(28*28,))
mnist.add(Layer(512, 28*28))
mnist.add(Layer(10, 512))
def argmax(l):
return l.index(max(l))
def test():
TOTAL_RIGHT = 0
AVERAGE = 0
for i, t_data in enumerate(test_data_x):
prediction = mnist.forwardpropagate(t_data)
if test_data_y[i][argmax(prediction)] == 1:
TOTAL_RIGHT += 1
if AVERAGE == 0:
AVERAGE = mnist.get_error(t_data, test_data_y[i])
else:
AVERAGE = (AVERAGE * i + mnist.get_error(t_data, test_data_y[i])) / (i + 1)
#if i % 1000 == 0:
# print(f"{TOTAL_RIGHT}/{i}")
# print(f"Average Cost: {AVERAGE}")
print(f"{TOTAL_RIGHT}/{i} = {TOTAL_RIGHT / i}")
print(f"Average Cost: {AVERAGE}")
test()
for k in range(30):
print(f"Epoch {k}")
mnist.train_batch(train_data_x, train_data_y, learning_rate=0.5)
test()
print("-"*10)
def logic_xor():
xor = Model(input_shape=(2,))
xor.add(Layer(2, 2))
xor.add(Layer(1, 2))
x, y = [[0, 0], [0, 1], [1, 0], [1, 1]], [[0], [1], [1], [0]]
def test():
TOTAL_RIGHT = 0
AVERAGE = 0
for i, x_data in enumerate(x):
prediction = xor.forwardpropagate(x_data)
print(prediction, y[i][0])
if y[i][0] == round(prediction[0]):
TOTAL_RIGHT += 1
if AVERAGE == 0:
AVERAGE = xor.get_error(x_data, y[i])
else:
AVERAGE = (AVERAGE * i + xor.get_error(x_data, y[i])) / (i + 1)
print(f"{TOTAL_RIGHT}/{i + 1} = {TOTAL_RIGHT / (i + 1)}")
print(f"Average Cost: {AVERAGE}")
for epoch in range(100000 + 1):
if epoch % 1000 == 0:
print(f"Epoch: {epoch}")
for tx, ty in zip(x, y):
xor.train(tx, ty, learning_rate=0.5)
test()
def logic_and():
and_ = Model(input_shape=(2,))
and_.add(Layer(2, 2))
and_.add(Layer(1, 2))
x, y = [[0, 0], [0, 1], [1, 0], [1, 1]], [[0], [0], [0], [1]]
def test():
TOTAL_RIGHT = 0
AVERAGE = 0
for i, x_data in enumerate(x):
prediction = and_.forwardpropagate(x_data)
print(prediction, y[i][0])
if y[i][0] == round(prediction[0]):
TOTAL_RIGHT += 1
if AVERAGE == 0:
AVERAGE = and_.get_error(x_data, y[i])
else:
AVERAGE = (AVERAGE * i + and_.get_error(x_data, y[i])) / (i + 1)
print(f"{TOTAL_RIGHT}/{i + 1} = {TOTAL_RIGHT / (i + 1)}")
print(f"Average Cost: {AVERAGE}")
for epoch in range(10000 + 1):
if epoch % 1000 == 0:
print(f"Epoch: {epoch}")
for tx, ty in zip(x, y):
and_.train(tx, ty, learning_rate=0.5)
test()
def logic_or():
or_ = Model(input_shape=(2,))
or_.add(Layer(2, 2))
or_.add(Layer(1, 2))
x, y = [[0, 0], [0, 1], [1, 0], [1, 1]], [[0], [1], [1], [1]]
def test():
TOTAL_RIGHT = 0
AVERAGE = 0
for i, x_data in enumerate(x):
prediction = or_.forwardpropagate(x_data)
print(prediction, y[i][0])
if y[i][0] == round(prediction[0]):
TOTAL_RIGHT += 1
if AVERAGE == 0:
AVERAGE = or_.get_error(x_data, y[i])
else:
AVERAGE = (AVERAGE * i + or_.get_error(x_data, y[i])) / (i + 1)
print(f"{TOTAL_RIGHT}/{i + 1} = {TOTAL_RIGHT / (i + 1)}")
print(f"Average Cost: {AVERAGE}")
for epoch in range(10000 + 1):
if epoch % 1000 == 0:
print(f"Epoch: {epoch}")
for tx, ty in zip(x, y):
or_.train(tx, ty, learning_rate=0.5)
test()
if __name__ == "__main__":
logic_or()