-
Notifications
You must be signed in to change notification settings - Fork 2
/
neuralnet.py
394 lines (314 loc) · 9.81 KB
/
neuralnet.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# Neural Network python module
# Written by William Ganucheau 2014
import sys, random, math
# Utility function to create an empty (zero) array of length n
def zeroArray(n):
return [0 for i in range(0, n)]
# A neural network class capable of saving/loading from a file,
# Training based on user-provided input-output data,
# and evaluating based on user input
class NeuralNetwork:
# Create a neural network. If weights aren't provided, they are
# initialized to random values
def __init__(self, neurons, weights=None, biases=None):
self.numLayers = len(neurons)
self.neurons = neurons
self.numNeurons = sum(neurons)
self.numInput = neurons[0];
self.numOutput = neurons[len(neurons)-1]
self.weights = weights
self.biases = biases
self.errorGradients = zeroArray(self.numNeurons)
self.outputs = zeroArray(self.numNeurons)
self.inputs = []
for layer in range(0, self.numLayers-1):
for neuron in range(0, self.neurons[layer+1]):
self.inputs.append(zeroArray(self.neurons[layer]))
# Default random values
if weights == None:
self.__initWeights()
self.tempWeights = weights;
if biases == None:
self.__initBiases()
# Initialize random weights for the neural network
def __initWeights(self):
i = 0
self.weights = []
# Initialize the weights for every non-input neuron
for layer in range(1, self.numLayers):
numWeights = self.neurons[layer-1]
for neuron in range(0, self.neurons[layer]):
self.weights.append(
[random.uniform(-0.5, 0.5) for j in range(0, numWeights)]
)
# Initialize random biases for the neural network
def __initBiases(self):
numBiased = self.numNeurons-self.numInput
self.biases = [random.uniform(-0.5, 0.5) for j in range(0, numBiased)]
# Save the neural network to a file
def save(self, path):
data = ''
# First line is # of layers
data += str(self.numLayers) + '\n'
# Second line is # of neurons in each layer
for c in range(0, len(self.neurons)):
data += str(self.neurons[c]) + ' '
data += '\n'
# Third line is biases for all the neurons
for b in range(0, len(self.biases)):
data += str(self.biases[b]) + ' '
data += '\n'
# Following lines are the weights of each neuron
i = 0
for l in range(1, self.numLayers):
for n in range(0, self.neurons[l]):
for w in range (0, len(self.weights[i])):
data += str(self.weights[i][w]) + ' '
data += '\n'
i += 1
f = open(path, 'w')
f.write(data)
f.flush()
f.close()
# Load a network from a file
@classmethod
def load(self, path):
f = open(path, 'r')
numLayers = int(f.readline())
charNeurons = f.readline().split()
charBiases = f.readline().split()
neurons = [int(charNeurons[i]) for i in range(0, len(charNeurons))]
biases = [float(charBiases[i]) for i in range(0, len(charBiases))]
weights = zeroArray(sum(neurons))
for neuron in range(0, sum(neurons)):
charWeights = f.readline().split()
weights[neuron] = (
[float(charWeights[i]) for i in range(0, len(charWeights))]
)
# Instantiate network
return self(neurons, weights, biases)
# Evaluate an input array with the neural network
def eval(self, input):
if len(input) != self.numInput:
sys.exit ('Error: Invalid input size.')
output = []
neuronIndex = 0;
for layer in range(1, self.numLayers):
output = zeroArray(self.neurons[layer])
for neuron in range (0, self.neurons[layer]):
neuronIndex = self.__getIndex(layer) + neuron
numWeights = len(self.weights[neuronIndex])
for weight in range (0, numWeights):
val = self.weights[neuronIndex][weight] * input[weight]
output[neuron] += val
self.inputs[neuronIndex][weight] = input[weight]
output[neuron] += self.biases[neuronIndex]
output[neuron] = self.__sigmoid(output[neuron])
self.outputs[neuronIndex] = output[neuron]
neuronIndex += 1
input = output
return output
# Sigmoid function maps (-inf, inf) -> (0, 1)
def __sigmoid(self, val):
return 1.0 / (1.0 + math.exp(-1*val))
# Train the network on a single set of expected vs. actual output data
def train(self, expected, actual):
if len(expected) != len(actual):
sys.exit ('Provided output different size from network output.')
# Train the output layer
for neuron in range(0, self.numOutput):
error = expected[neuron] - actual[neuron]
neuronIndex = self.__getIndex(self.numLayers-1) + neuron
self.__trainNeuron (neuronIndex, error)
# Train the hidden layers
for layer in range(self.numLayers-2, 0, -1):
numNeurons = self.neurons[layer]
for neuron in range (0, numNeurons):
neuronIndex = neuron + self.__getIndex(layer)
error = 0
for nextNeuron in range (0, self.neurons[layer+1]):
nextNeuronIndex = nextNeuron + self.__getIndex(layer+1)
error += (
self.weights[nextNeuronIndex][neuron] *
self.errorGradients[nextNeuronIndex]
)
self.__trainNeuron(neuronIndex, error)
self.weights = self.tempWeights;
# Train a neuron
def __trainNeuron(self, index, error):
self.errorGradients[index] = self.outputs[index]
self.errorGradients[index] *= (1 - self.outputs[index]) * error
numWeights = len(self.weights[index])
for weight in range(0, numWeights):
self.tempWeights[index][weight] += (
self.inputs[index][weight] * self.errorGradients[index]
)
# Get the index of the first neuron in a layer
def __getIndex(self, layer):
index = 0
for l in range(0, layer-1):
index += self.neurons[l+1]
return index
# Train a neural network until the error is below the threshold
def simulate (self, inputSet, outputSet, maxError):
iterations = 0
attempts = 1
maxIterations = 100000
maxAttempts = 5
# Arbitrary, initial error just has to be > maxError
error = maxError + 1
while error > maxError:
# Prevent the network from stalling in local mins
if iterations == maxIterations:
if attempts == maxAttempts:
return False
iterations = 0
attempts += 1
# Generate new weights and biases
self.__initWeights()
self.__initBiases()
print('Network failed to converge. Trying again with new vals')
error = 0
# Start at a random index to prevent getting stalled
startIndex = random.randrange(0, len(inputSet))
# Train on each of the input/output data sets
for i in range (0, len(inputSet)):
index = (startIndex + i) % len(inputSet)
output = self.eval(inputSet[index])
# Sum-squared error
error += math.pow(self.__maxDiff(outputSet[index], output), 2)
# Train the neural network
self.train(outputSet[index], output)
iterations += 1
# Network converged
return True
# Find the maximum difference between two numeric arrays
def __maxDiff (self, alist, blist):
if len(alist) != len(blist):
sys.exit('Lists must be of same size!')
max = None
for i in range (0, len(alist)):
dif = alist[i] - blist[i]
if max == None or max < dif:
max = dif
return max
# Convert a list of values two a 2D list
# Each line is one element of the list
def fileToList(path):
f = open(path, 'r')
string = None
list = []
for line in f:
strArr = line.split()
valArr = [float(strArr[i]) for i in range(0, len(strArr))]
list.append(valArr)
return list
def _initParsers():
import argparse
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='command')
# Create a new neural network
parser_new = subparsers.add_parser(
'new',
help='Create a new neural net.'
)
parser_new.add_argument(
'filepath',
type=str,
nargs=1,
help='Filepath to which the net should be saved.'
)
parser_new.add_argument(
'neurons',
type=int,
nargs='+',
help='Number of neurons in each layer. (Must have at least 2 layers!)'
)
# Train a neural network
parser_train = subparsers.add_parser(
'train',
help='Train a neural net on a set of input/output data'
)
parser_train.add_argument(
'filepath',
type=str,
nargs=1,
help='Filepath to the neural network.'
)
parser_train.add_argument(
'input',
type=str,
nargs=1,
help='File path to the training input.')
parser_train.add_argument(
'output',
type=str,
nargs=1,
help='File path to the training output.'
)
parser_train.add_argument(
'-e', '--maxError',
type=float,
nargs=1,
help='The desired accuracy of the network'
)
# Evaluate input data on the network
parser_eval = subparsers.add_parser(
'eval',
help='Evaluate input/output data on the neural network'
)
parser_eval.add_argument(
'filepath',
type=str,
nargs=1,
help='Filepath to the neural network.'
)
group = parser_eval.add_mutually_exclusive_group(required=True)
group.add_argument(
'-i', '--input',
type=float,
nargs='+',
help='A single set of input values'
)
group.add_argument(
'-l', '--loadInput',
type=str,
nargs=1,
help='A file from which to load input data'
)
return parser
# Commandline usage
if __name__ == "__main__":
parser = _initParsers()
args = parser.parse_args()
command = args.command.split()[0]
# User wants to create a new neural net
if command == 'new':
numLayers = len(args.neurons)
if numLayers < 2:
sys.exit('Error: Must have at least 2 layers')
net = NeuralNetwork(args.neurons)
net.save(args.filepath[0])
print('Neural network created and saved to ' + args.filepath[0] + '.')
# User wants to train a neural net
elif command == 'train':
net = NeuralNetwork.load(args.filepath[0])
print('Neural network loaded. ' + str(net.numLayers) + ' layers.')
inputSet = fileToList(args.input[0])
outputSet = fileToList(args.output[0])
print('Beginning to train')
if args.maxError:
maxError = args.maxError[0]
else:
maxError = 0.01
net.simulate(inputSet, outputSet, maxError)
net.save(args.filepath[0])
# User wants to evaluate some input on the neural net
elif command == 'eval':
net = NeuralNetwork.load(args.filepath[0])
if args.input:
print(net.eval(args.input))
sys.exit()
input = fileToList(args.loadInput[0])
for i in input:
print(net.eval(i))