forked from j2kun/neural-networks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
neuralnetwork-tests.py
113 lines (82 loc) · 3.23 KB
/
neuralnetwork-tests.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
from unittest import *
from neuralnetwork import *
def binaryNumbersTest():
network = Network()
inputNodes = [InputNode(i) for i in range(3)]
hiddenNodes = [Node() for i in range(3)]
outputNode = Node()
# weights are all randomized
for inputNode in inputNodes:
for node in hiddenNodes:
Edge(inputNode, node)
for node in hiddenNodes:
Edge(node, outputNode)
network.outputNode = outputNode
network.inputNodes.extend(inputNodes)
labeledExamples = [((0,0,0), 1),
((0,0,1), 0),
((0,1,0), 1),
((0,1,1), 0),
((1,0,0), 1),
((1,0,1), 0),
((1,1,0), 1),
((1,1,1), 0)]
network.train(labeledExamples, maxIterations=5000)
# test for consistency
for number, isEven in labeledExamples:
print "Error for %r is %0.4f. Output was:%0.4f" % (number, isEven - network.evaluate(number), network.evaluate(number))
def makeNetwork(numInputs, numHiddenLayers, numInEachLayer):
network = Network()
inputNodes = [InputNode(i) for i in range(numInputs)]
outputNode = Node()
network.outputNode = outputNode
network.inputNodes.extend(inputNodes)
layers = [[Node() for _ in range(numInEachLayer)] for _ in range(numHiddenLayers)]
# weights are all randomized
for inputNode in inputNodes:
for node in layers[0]:
Edge(inputNode, node)
for layer1, layer2 in [(layers[i], layers[i+1]) for i in range(numHiddenLayers-1)]:
for node1 in layer1:
for node2 in layer2:
Edge(node1, node2)
for node in layers[-1]:
Edge(node, outputNode)
return network
def sineTest(numLayers, numNodes):
import math
import random
f = lambda x: 0.5 * (1.0 + math.sin(x))
domain = lambda: [random.random()*math.pi*4 for _ in range(100)]
network = makeNetwork(1, numLayers, numNodes)
labeledExamples = [((x,), f(x)) for x in domain()]
network.train(labeledExamples, learningRate=0.25, maxIterations=100000)
errors = [abs(f(x) - network.evaluate((x,))) for x in domain()]
print "Avg error: %.4f" % (sum(errors) * 1.0 / len(errors))
with open('sine.txt', 'a') as theFile:
vals = tuple((x,network.evaluate((x,))) for x in domain())
line = "{%s},\n" % (",".join(["{%s}" % ",".join([str(n) for n in x]) for x in vals]),)
theFile.write(line)
def digitsTest():
import random
network = makeNetwork(256, 2, 15)
digits = []
with open('digits.dat', 'r') as dataFile:
for line in dataFile:
(exampleStr, classStr) = line.split(',')
digits.append(([int(x) for x in exampleStr.split()], float(classStr) / 9))
random.shuffle(digits)
trainingData, testData = digits[:-500], digits[-500:]
network.train(trainingData, learningRate=0.5, maxIterations=100000)
errors = [abs(testPt[-1] - round(network.evaluate(testPt[0]))) for testPt in testData]
print "Average error: %.4f" % (sum(errors)*1.0 / len(errors))
if __name__ == "__main__":
#binaryNumbersTest()
print "Sine"
with open('sine.txt','w') as theFile:
theFile.write("{")
sineTest(1, 20)
with open('sine.txt','a') as theFile:
theFile.write("}\n")
print "Digits"
digitsTest()