-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNeuralNetwork.lua
More file actions
500 lines (264 loc) · 11.1 KB
/
NeuralNetwork.lua
File metadata and controls
500 lines (264 loc) · 11.1 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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
--[[
--https://gist.github.com/cassiozen/de0dff87eb7ed599b5d0
NerualNetwork.create( numInputs, numOutputs, numHiddenLayers, neuronsPerLayer, learningRate)
Return value - a neural network object.
Parameters:
numInputs - the number of input neurons
numOutputs - the number of output neurons
numHiddenLayers - the number of hidden layers (Reccomended: 1)
neuronsPerLayer - the number of neurons in the hidden layers (not input/output layers) (Reccomended: 4)
learningRate - the rate at which the neural network learns (Percentage eg: 0-1, smaller the value the longer it takes to teach, higher the value the less it takes to teach but it might not learn the sequence corectly) (Reccomended: 3)
NerualNetwork:forewardPropagate(...)
Return value - a table containg the results
number of arguments == number of inputs for the NN object
inputs whatever impulse and returns the learned results
NeuralNetwork:backwardPropagate( inputs, desiredOutputs)
inputs - a table that has the inputs
desiredOutputs - a table that has the desired outputs for this training set
NeuralNetwork:save()
return value - returns a unique string that can be used to load the NN again in its current state (remembers training). This string could then be written to a file.
NeuralNetwork.load( data)
data - the unique string returned by NeuralNetwork:save()
return value - a Neural Network object
--EXAMPLE--
math.randomseed(os.time())
dofile("luaneural.lua")
network = NeuralNetwork.create(2,1,1,4,0.3)
print("Training the neural network:")
attempts = 10000 -- number of times to do backpropagation
for i = 1,attempts do
network:backwardPropagate({0,0},{0})
network:backwardPropagate({1,0},{1})
network:backwardPropagate({0,1},{1})
network:backwardPropagate({1,1},{0})
end
print("Results:")
print("0 0 | "..network:forewardPropagate(0,0)[1])
print("1 0 | "..network:forewardPropagate(1,0)[1])
print("0 1 | "..network:forewardPropagate(0,1)[1])
print("1 1 | "..network:forewardPropagate(1,1)[1])
--EXAMPLE SAVE--
--If needed, you can save the trained neural network to a file to use it later--
print("Saving Neural Network...")
NN = io.open("neuralNet.txt","w")
NN:write(network:save())
NN:flush()
NN:close()
print("Loading Neural Network...")
NN = io.open("neuralNet.txt","r")
network = NeuralNetwork.load(NN:read())
print("Results:")
print("0 0 | "..network:forewardPropagate(0,0)[1])
print("1 0 | "..network:forewardPropagate(1,0)[1])
print("0 1 | "..network:forewardPropagate(0,1)[1])
print("1 1 | "..network:forewardPropagate(1,1)[1])
--]]
ACTIVATION_RESPONSE = 1
NeuralNetwork = {
transfer = function( x) return 1 / (1 + math.exp(-x / ACTIVATION_RESPONSE)) end --This is the Transfer function (in this case a sigmoid)
}
function NeuralNetwork.create( _numInputs, _numOutputs, _numHiddenLayers, _neuronsPerLayer, _learningRate)
_numInputs = _numInputs or 1
_numOutputs = _numOutputs or 1
_numHiddenLayers = _numHiddenLayers or math.ceil(_numInputs/2)
_neuronsPerLayer = _neuronsPerLayer or math.ceil(_numInputs*.66666+_numOutputs)
_learningRate = _learningRate or .5
--order goes network[layer][neuron][wieght]
local network = setmetatable({
learningRate = _learningRate
},{ __index = NeuralNetwork});
network[1] = {} --Input Layer
for i = 1,_numInputs do
network[1][i] = {}
end
for i = 2,_numHiddenLayers+2 do --plus 2 represents the output layer (also need to skip input layer)
network[i] = {}
local neuronsInLayer = _neuronsPerLayer
if i == _numHiddenLayers+2 then
neuronsInLayer = _numOutputs
end
for j = 1,neuronsInLayer do
network[i][j] = {bias = math.random()*2-1}
local numNeuronInputs = table.getn(network[i-1])
for k = 1,numNeuronInputs do
network[i][j][k] = math.random()*2-1 --return random number between -1 and 1
end
end
end
return network
end
function NeuralNetwork:forewardPropagate(...)
if table.getn(arg) ~= table.getn(self[1]) and type(arg[1]) ~= "table" then
error("Neural Network received "..table.getn(arg).." input[s] (expected "..table.getn(self[1]).." input[s])",2)
elseif type(arg[1]) == "table" and table.getn(arg[1]) ~= table.getn(self[1]) then
error("Neural Network received "..table.getn(arg[1]).." input[s] (expected "..table.getn(self[1]).." input[s])",2)
end
local outputs = {}
for i = 1,table.getn(self) do
for j = 1,table.getn(self[i]) do
if i == 1 then
if type(arg[1]) == "table" then
self[i][j].result = arg[1][j]
else
self[i][j].result = arg[j]
end
else
self[i][j].result = self[i][j].bias
for k = 1,table.getn(self[i][j]) do
self[i][j].result = self[i][j].result + (self[i][j][k]*self[i-1][k].result)
end
self[i][j].result = NeuralNetwork.transfer(self[i][j].result)
if i == table.getn(self) then
table.insert(outputs,self[i][j].result)
end
end
end
end
return outputs
end
function NeuralNetwork:backwardPropagate(inputs,desiredOutputs)
if table.getn(inputs) ~= table.getn(self[1]) then
error("Neural Network received "..table.getn(inputs).." input[s] (expected "..table.getn(self[1]).." input[s])",2)
elseif table.getn(desiredOutputs) ~= table.getn(self[table.getn(self)]) then
error("Neural Network received "..table.getn(desiredOutputs).." desired output[s] (expected "..table.getn(self[table.getn(self)]).." desired output[s])",2)
end
self:forewardPropagate(inputs) --update the internal inputs and outputs
for i = table.getn(self),2,-1 do --iterate backwards (nothing to calculate for input layer)
local tempResults = {}
for j = 1,table.getn(self[i]) do
if i == table.getn(self) then --special calculations for output layer
self[i][j].delta = (desiredOutputs[j] - self[i][j].result) * self[i][j].result * (1 - self[i][j].result)
else
local weightDelta = 0
for k = 1,table.getn(self[i+1]) do
weightDelta = weightDelta + self[i+1][k][j]*self[i+1][k].delta
end
self[i][j].delta = self[i][j].result * (1 - self[i][j].result) * weightDelta
end
end
end
for i = 2,table.getn(self) do
for j = 1,table.getn(self[i]) do
self[i][j].bias = self[i][j].delta * self.learningRate
for k = 1,table.getn(self[i][j]) do
self[i][j][k] = self[i][j][k] + self[i][j].delta * self.learningRate * self[i-1][k].result
end
end
end
end
function NeuralNetwork:save()
--[[
File specs:
|INFO| - should be FF BP NN
|I| - number of inputs
|O| - number of outputs
|HL| - number of hidden layers
|NHL| - number of neurons per hidden layer
|LR| - learning rate
|BW| - bias and weight values
]]--
local data = "|INFO|FF BP NN|I|"..tostring(table.getn(self[1])).."|O|"..tostring(table.getn(self[table.getn(self)])).."|HL|"..tostring(table.getn(self)-2).."|NHL|"..tostring(table.getn(self[2])).."|LR|"..tostring(self.learningRate).."|BW|"
for i = 2,table.getn(self) do -- nothing to save for input layer
for j = 1,table.getn(self[i]) do
local neuronData = tostring(self[i][j].bias).."{"
for k = 1,table.getn(self[i][j]) do
neuronData = neuronData..tostring(self[i][j][k])
neuronData = neuronData..","
end
data = data..neuronData.."}"
end
end
data = data.."|END|"
return data
end
function NeuralNetwork.load( data)
local dataPos = string.find(data,"|")+1
local currentChunk = string.sub( data, dataPos, string.find(data,"|",dataPos)-1)
local dataPos = string.find(data,"|",dataPos)+1
local _inputs, _outputs, _hiddenLayers, _neuronsPerLayer, _learningRate
local biasWeights = {}
local errorExit = false
while currentChunk ~= "END" and not errorExit do
if currentChuck == "INFO" then
currentChunk = string.sub( data, dataPos, string.find(data,"|",dataPos)-1)
dataPos = string.find(data,"|",dataPos)+1
if currentChunk ~= "FF BP NN" then
errorExit = true
end
elseif currentChunk == "I" then
currentChunk = string.sub( data, dataPos, string.find(data,"|",dataPos)-1)
dataPos = string.find(data,"|",dataPos)+1
_inputs = tonumber(currentChunk)
elseif currentChunk == "O" then
currentChunk = string.sub( data, dataPos, string.find(data,"|",dataPos)-1)
dataPos = string.find(data,"|",dataPos)+1
_outputs = tonumber(currentChunk)
elseif currentChunk == "HL" then
currentChunk = string.sub( data, dataPos, string.find(data,"|",dataPos)-1)
dataPos = string.find(data,"|",dataPos)+1
_hiddenLayers = tonumber(currentChunk)
elseif currentChunk == "NHL" then
currentChunk = string.sub( data, dataPos, string.find(data,"|",dataPos)-1)
dataPos = string.find(data,"|",dataPos)+1
_neuronsPerLayer = tonumber(currentChunk)
elseif currentChunk == "LR" then
currentChunk = string.sub( data, dataPos, string.find(data,"|",dataPos)-1)
dataPos = string.find(data,"|",dataPos)+1
_learningRate = tonumber(currentChunk)
elseif currentChunk == "BW" then
currentChunk = string.sub( data, dataPos, string.find(data,"|",dataPos)-1)
dataPos = string.find(data,"|",dataPos)+1
local subPos = 1
local subChunk
for i = 1,_hiddenLayers+1 do
biasWeights[i] = {}
local neuronsInLayer = _neuronsPerLayer
if i == _hiddenLayers+1 then
neuronsInLayer = _outputs
end
for j = 1,neuronsInLayer do
biasWeights[i][j] = {}
biasWeights[i][j].bias = tonumber(string.sub(currentChunk,subPos,string.find(currentChunk,"{",subPos)-1))
subPos = string.find(currentChunk,"{",subPos)+1
subChunk = string.sub( currentChunk, subPos, string.find(currentChunk,",",subPos)-1)
local maxPos = string.find(currentChunk,"}",subPos)
while subPos < maxPos do
table.insert(biasWeights[i][j],tonumber(subChunk))
subPos = string.find(currentChunk,",",subPos)+1
if string.find(currentChunk,",",subPos) ~= nil then
subChunk = string.sub( currentChunk, subPos, string.find(currentChunk,",",subPos)-1)
end
end
subPos = maxPos+1
end
end
end
currentChunk = string.sub( data, dataPos, string.find(data,"|",dataPos)-1)
dataPos = string.find(data,"|",dataPos)+1
end
if errorExit then
error("Failed to load Neural Network:"..currentChunk,2)
end
local network = setmetatable({
learningRate = _learningRate
},{ __index = NeuralNetwork});
network[1] = {} --Input Layer
for i = 1,_inputs do
network[1][i] = {}
end
for i = 2,_hiddenLayers+2 do --plus 2 represents the output layer (also need to skip input layer)
network[i] = {}
local neuronsInLayer = _neuronsPerLayer
if i == _hiddenLayers+2 then
neuronsInLayer = _outputs
end
for j = 1,neuronsInLayer do
network[i][j] = {bias = biasWeights[i-1][j].bias}
local numNeuronInputs = table.getn(network[i-1])
for k = 1,numNeuronInputs do
network[i][j][k] = biasWeights[i-1][j][k]
end
end
end
return network
end