-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_neural.py
More file actions
120 lines (103 loc) · 4.96 KB
/
test_neural.py
File metadata and controls
120 lines (103 loc) · 4.96 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
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
from unittest import TestCase
from neural import *
class Test(TestCase):
def test_high_logistic_function(self):
self.assertTrue(logistic(10) > 0.9)
def test_low_logistic_function(self):
self.assertTrue(logistic(-10) < 0.1)
def test_medium_logistic_function(self):
self.assertAlmostEqual(0.5, logistic(0))
# These tests use a net with 2 input units, no hidden units, and 1 output unit.
# It eventually learns OR and AND.
# In most of the tests, the weights are initialized to specific values to test formulae.
def test_predicts_2_1(self):
net = Network([2, 1])
output = net.layers[1][0] # The output unit
output.weights = [-0.1, 0.1, 0.2]
self.assertAlmostEqual(0.475, net.predict([0, 0])[0], delta=0.001)
self.assertAlmostEqual(0.525, net.predict([0, 1])[0], delta=0.001)
self.assertAlmostEqual(0.500, net.predict([1, 0])[0], delta=0.001)
self.assertAlmostEqual(0.550, net.predict([1, 1])[0], delta=0.001)
def test_updates_deltas_2_1(self):
net = Network([2, 1])
output = net.layers[1][0] # The output unit
output.weights = [-0.1, 0.1, 0.2]
net.predict([0, 1])
net.reset_deltas()
net.update_deltas([1])
self.assertAlmostEqual(-0.118, output.delta, delta=0.001)
def test_updates_weights_2_1(self):
net = Network([2, 1])
output = net.layers[1][0] # The output unit
output.weights = [-0.1, 0.1, 0.2]
net.train([0, 1], [1])
self.assertAlmostEqual(0.583, net.predict([0, 1])[0], delta=0.001)
def test_learns_or_2_1(self):
net = Network([2, 1])
inputs = [[0, 0], [0, 1], [1, 0], [1, 1]]
targets = [[0], [1], [1], [1]]
for _ in range(1000):
for i, t in zip(inputs, targets):
net.train(i, t)
for i, t in zip(inputs, targets):
self.assertAlmostEqual(t[0], net.predict(i)[0], delta=0.2)
def test_learns_and_2_1(self):
net = Network([2, 1])
inputs = [[0, 0], [0, 1], [1, 0], [1, 1]]
targets = [[0], [0], [0], [1]]
for _ in range(1000):
for i, t in zip(inputs, targets):
net.train(i, t)
for i, t in zip(inputs, targets):
self.assertAlmostEqual(t[0], net.predict(i)[0], delta=0.2)
# These tests use a net with 2 input units, 2 hidden units, and 1 output unit.
# It eventually learns XOR.
# In most of the tests, the weights are initialized to specific values to test formulae.
def test_predicts_2_2_1(self):
net = Network([2, 2, 1])
net.layers[2][0].weights = [-0.1, 0.1, 0.2] # Output unit
net.layers[1][0].weights = [-0.2, 0.3, 0.4] # First hidden unit
net.layers[1][1].weights = [-0.3, 0.5, -0.4] # Second hidden unit
self.assertAlmostEqual(0.508, net.predict([0, 0])[0], delta=0.001)
self.assertAlmostEqual(0.505, net.predict([0, 1])[0], delta=0.001)
self.assertAlmostEqual(0.516, net.predict([1, 0])[0], delta=0.001)
self.assertAlmostEqual(0.513, net.predict([1, 1])[0], delta=0.001)
def test_updates_deltas_2_2_1(self):
net = Network([2, 2, 1])
net.layers[2][0].weights = [-0.1, 0.1, 0.2] # Output unit
net.layers[1][0].weights = [-0.2, 0.3, 0.4] # First hidden unit
net.layers[1][1].weights = [-0.3, 0.5, -0.4] # Second hidden unit
net.predict([0, 1])
net.reset_deltas()
net.update_deltas([1])
self.assertAlmostEqual(-0.124, net.layers[2][0].delta, delta=0.001)
self.assertAlmostEqual(-0.003, net.layers[1][0].delta, delta=0.001)
self.assertAlmostEqual(-0.005, net.layers[1][1].delta, delta=0.001)
def test_updates_weights_2_2_1(self):
net = Network([2, 2, 1])
net.layers[2][0].weights = [-0.1, 0.1, 0.2] # Output unit
net.layers[1][0].weights = [-0.2, 0.3, 0.4] # First hidden unit
net.layers[1][1].weights = [-0.3, 0.5, -0.4] # Second hidden unit
net.train([0, 1], [1])
self.assertAlmostEqual(0.549, net.predict([0, 1])[0], delta=0.001)
def test_learns_xor_2_2_1(self):
# NOTE: This test will occasionally fail due to a local minimum
# If this happens, run it again.
net = Network([2, 2, 1])
inputs = [[0, 0], [0, 1], [1, 0], [1, 1]]
targets = [[0], [1], [1], [0]]
for _ in range(1000):
for i, t in zip(inputs, targets):
net.train(i, t)
for i, t in zip(inputs, targets):
self.assertAlmostEqual(t[0], net.predict(i)[0], delta=0.2)
# A network with 5 hidden units is more reliable.
def test_learns_xor_2_5_1(self):
net = Network([2, 5, 1])
inputs = [[0, 0], [0, 1], [1, 0], [1, 1]]
targets = [[0], [1], [1], [0]]
for _ in range(1000):
for i, t in zip(inputs, targets):
net.train(i, t)
for i, t in zip(inputs, targets):
self.assertAlmostEqual(t[0], net.predict(i)[0], delta=0.2)