forked from KevinCoble/AIToolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LSTMNeuralNetwork.swift
983 lines (880 loc) · 45 KB
/
LSTMNeuralNetwork.swift
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
//
// LSTMNeuralNetwork.swift
// AIToolbox
//
// Created by Kevin Coble on 5/20/16.
// Copyright © 2016 Kevin Coble. All rights reserved.
//
import Foundation
#if os(Linux)
#else
import Accelerate
#endif
final class LSTMNeuralNode {
// Activation function
let activation : NeuralActivationFunction
let numInputs : Int
let numFeedback : Int
// Weights
let numWeights : Int // This includes weights from inputs and from feedback for input, forget, cell, and output
var Wi : [Double]
var Ui : [Double]
var Wf : [Double]
var Uf : [Double]
var Wc : [Double]
var Uc : [Double]
var Wo : [Double]
var Uo : [Double]
var h : Double // Last result calculated
var outputHistory : [Double] // History of output for the sequence
var lastCellState : Double // Last cell state calculated
var cellStateHistory : [Double] // History of cell state for the sequence
var ho : Double // Last output gate calculated
var outputGateHistory : [Double] // History of output gate result for the sequence
var hc : Double
var memoryCellHistory : [Double] // History of cell activation result for the sequence
var hi : Double // Last input gate calculated
var inputGateHistory : [Double] // History of input gate result for the sequence
var hf : Double // Last forget gate calculated
var forgetGateHistory : [Double] // History of forget gate result for the sequence
var 𝟃E𝟃h : Double // Gradient in error with respect to output of this node for this time step plus future time steps
var 𝟃E𝟃zo : Double // Gradient in error with respect to weighted sum of the output gate
var 𝟃E𝟃zi : Double // Gradient in error with respect to weighted sum of the input gate
var 𝟃E𝟃zf : Double // Gradient in error with respect to weighted sum of the forget gate
var 𝟃E𝟃zc : Double // Gradient in error with respect to weighted sum of the memory cell
var 𝟃E𝟃cellState : Double // Gradient in error with respect to state of the memory cell
var 𝟃E𝟃Wi : [Double]
var 𝟃E𝟃Ui : [Double]
var 𝟃E𝟃Wf : [Double]
var 𝟃E𝟃Uf : [Double]
var 𝟃E𝟃Wc : [Double]
var 𝟃E𝟃Uc : [Double]
var 𝟃E𝟃Wo : [Double]
var 𝟃E𝟃Uo : [Double]
var weightUpdateMethod = NeuralWeightUpdateMethod.normal
var weightUpdateParameter : Double? // Decay rate for rms prop weight updates
var WiWeightUpdateData : [Double] = [] // Array of running average for rmsprop
var UiWeightUpdateData : [Double] = [] // Array of running average for rmsprop
var WfWeightUpdateData : [Double] = [] // Array of running average for rmsprop
var UfWeightUpdateData : [Double] = [] // Array of running average for rmsprop
var WcWeightUpdateData : [Double] = [] // Array of running average for rmsprop
var UcWeightUpdateData : [Double] = [] // Array of running average for rmsprop
var WoWeightUpdateData : [Double] = [] // Array of running average for rmsprop
var UoWeightUpdateData : [Double] = [] // Array of running average for rmsprop
/// Create the LSTM neural network node with a set activation function
init(numInputs : Int, numFeedbacks : Int, activationFunction: NeuralActivationFunction)
{
activation = activationFunction
self.numInputs = numInputs + 1 // Add one weight for the bias term
self.numFeedback = numFeedbacks
// Weights
numWeights = (self.numInputs + self.numFeedback) * 4 // input, forget, cell and output all have weights
Wi = []
Ui = []
Wf = []
Uf = []
Wc = []
Uc = []
Wo = []
Uo = []
h = 0.0
outputHistory = []
lastCellState = 0.0
cellStateHistory = []
ho = 0.0
outputGateHistory = []
hc = 0.0
memoryCellHistory = []
hi = 0.0
inputGateHistory = []
hf = 0.0
forgetGateHistory = []
𝟃E𝟃h = 0.0
𝟃E𝟃zo = 0.0
𝟃E𝟃zi = 0.0
𝟃E𝟃zf = 0.0
𝟃E𝟃zc = 0.0
𝟃E𝟃cellState = 0.0
𝟃E𝟃Wi = []
𝟃E𝟃Ui = []
𝟃E𝟃Wf = []
𝟃E𝟃Uf = []
𝟃E𝟃Wc = []
𝟃E𝟃Uc = []
𝟃E𝟃Wo = []
𝟃E𝟃Uo = []
}
// Initialize the weights
func initWeights(_ startWeights: [Double]!)
{
if let startWeights = startWeights {
if (startWeights.count == 1) {
Wi = [Double](repeating: startWeights[0], count: numInputs)
Ui = [Double](repeating: startWeights[0], count: numFeedback)
Wf = [Double](repeating: startWeights[0], count: numInputs)
Uf = [Double](repeating: startWeights[0], count: numFeedback)
Wc = [Double](repeating: startWeights[0], count: numInputs)
Uc = [Double](repeating: startWeights[0], count: numFeedback)
Wo = [Double](repeating: startWeights[0], count: numInputs)
Uo = [Double](repeating: startWeights[0], count: numFeedback)
}
else if (startWeights.count == (numInputs+numFeedback) * 4) {
// Full weight array, just split into the eight weight arrays
var index = 0
Wi = Array(startWeights[index..<index+numInputs])
index += numInputs
Ui = Array(startWeights[index..<index+numFeedback])
index += numFeedback
Wf = Array(startWeights[index..<index+numInputs])
index += numInputs
Uf = Array(startWeights[index..<index+numFeedback])
index += numFeedback
Wc = Array(startWeights[index..<index+numInputs])
index += numInputs
Uc = Array(startWeights[index..<index+numFeedback])
index += numFeedback
Wo = Array(startWeights[index..<index+numInputs])
index += numInputs
Uo = Array(startWeights[index..<index+numFeedback])
index += numFeedback
}
else {
// Get the weights and bias start indices
let numValues = startWeights.count
var inputStart : Int
var forgetStart : Int
var cellStart : Int
var outputStart : Int
var sectionLength : Int
if ((numValues % 4) == 0) {
// Evenly divisible by 4, pass each quarter
sectionLength = numValues / 4
inputStart = 0
forgetStart = sectionLength
cellStart = sectionLength * 2
outputStart = sectionLength * 3
}
else {
// Use the values for all sections
inputStart = 0
forgetStart = 0
cellStart = 0
outputStart = 0
sectionLength = numValues
}
Wi = []
var index = inputStart // Last number (if more than 1) goes into the bias weight, then repeat the initial
for _ in 0..<numInputs-1 {
if (index >= sectionLength-1) { index = inputStart } // Wrap if necessary
Wi.append(startWeights[index])
index += 1
}
Wi.append(startWeights[inputStart + sectionLength - 1]) // Add the bias term
Ui = []
for _ in 0..<numFeedback {
if (index >= sectionLength-1) { index = inputStart } // Wrap if necessary
Ui.append(startWeights[index])
index += 1
}
index = forgetStart
Wf = []
for _ in 0..<numInputs-1 {
if (index >= sectionLength-1) { index = forgetStart } // Wrap if necessary
Wi.append(startWeights[index])
index += 1
}
Wf.append(startWeights[forgetStart + sectionLength - 1]) // Add the bias term
Uf = []
for _ in 0..<numFeedback {
if (index >= sectionLength-1) { index = forgetStart } // Wrap if necessary
Uf.append(startWeights[index])
index += 1
}
index = cellStart
Wc = []
for _ in 0..<numInputs-1 {
if (index >= sectionLength-1) { index = cellStart } // Wrap if necessary
Wc.append(startWeights[index])
index += 1
}
Wc.append(startWeights[cellStart + sectionLength - 1]) // Add the bias term
Uc = []
for _ in 0..<numFeedback {
if (index >= sectionLength-1) { index = cellStart } // Wrap if necessary
Uc.append(startWeights[index])
index += 1
}
index = outputStart
Wo = []
for _ in 0..<numInputs-1 {
if (index >= sectionLength-1) { index = outputStart } // Wrap if necessary
Wo.append(startWeights[index])
index += 1
}
Wo.append(startWeights[outputStart + sectionLength - 1]) // Add the bias term
Uo = []
for _ in 0..<numFeedback {
if (index >= sectionLength-1) { index = outputStart } // Wrap if necessary
Uo.append(startWeights[index])
index += 1
}
}
}
else {
Wi = []
for _ in 0..<numInputs-1 {
Wi.append(Gaussian.gaussianRandom(0.0, standardDeviation: 1.0 / Double(numInputs-1))) // input weights - Initialize to a random number to break initial symmetry of the network, scaled to the inputs
}
Wi.append(Gaussian.gaussianRandom(-2.0, standardDeviation:1.0)) // Bias weight - Initialize to a negative number to have inputs learn to feed in
Ui = []
for _ in 0..<numFeedback {
Ui.append(Gaussian.gaussianRandom(0.0, standardDeviation: 1.0 / Double(numFeedback))) // feedback weights - Initialize to a random number to break initial symmetry of the network, scaled to the inputs
}
Wf = []
for _ in 0..<numInputs-1 {
Wf.append(Gaussian.gaussianRandom(0.0, standardDeviation: 1.0 / Double(numInputs-1))) // input weights - Initialize to a random number to break initial symmetry of the network, scaled to the inputs
}
Wf.append(Gaussian.gaussianRandom(2.0, standardDeviation:1.0)) // Bias weight - Initialize to a positive number to turn off forget (output close to 1) until it 'learns' to forget
Uf = []
for _ in 0..<numFeedback {
Uf.append(Gaussian.gaussianRandom(0.0, standardDeviation: 1.0 / Double(numFeedback))) // feedback weights - Initialize to a random number to break initial symmetry of the network, scaled to the inputs
}
Wc = []
for _ in 0..<numInputs-1 {
Wc.append(Gaussian.gaussianRandom(0.0, standardDeviation: 1.0 / Double(numInputs-1))) // input weights - Initialize to a random number to break initial symmetry of the network, scaled to the inputs
}
Wc.append(Gaussian.gaussianRandom(0.0, standardDeviation:1.0)) // Bias weight - Initialize to a random number to break initial symmetry of the network
Uc = []
for _ in 0..<numFeedback {
Uc.append(Gaussian.gaussianRandom(0.0, standardDeviation: 1.0 / Double(numFeedback))) // feedback weights - Initialize to a random number to break initial symmetry of the network, scaled to the inputs
}
Wo = []
for _ in 0..<numInputs-1 {
Wo.append(Gaussian.gaussianRandom(0.0, standardDeviation: 1.0 / Double(numInputs-1))) // input weights - Initialize to a random number to break initial symmetry of the network, scaled to the inputs
}
Wo.append(Gaussian.gaussianRandom(-2.0, standardDeviation:1.0)) // Bias weight - Initialize to a negative number to limit output until network learns when output is needed
Uo = []
for _ in 0..<numFeedback {
Uo.append(Gaussian.gaussianRandom(0.0, standardDeviation: 1.0 / Double(numFeedback))) // feedback weights - Initialize to a random number to break initial symmetry of the network, scaled to the inputs
}
}
// If rmsprop update, allocate the momentum storage array
if (weightUpdateMethod == .rmsProp) {
WiWeightUpdateData = [Double](repeating: 0.0, count: numInputs)
UiWeightUpdateData = [Double](repeating: 0.0, count: numFeedback)
WfWeightUpdateData = [Double](repeating: 0.0, count: numInputs)
UfWeightUpdateData = [Double](repeating: 0.0, count: numFeedback)
WcWeightUpdateData = [Double](repeating: 0.0, count: numInputs)
UcWeightUpdateData = [Double](repeating: 0.0, count: numFeedback)
WoWeightUpdateData = [Double](repeating: 0.0, count: numInputs)
UoWeightUpdateData = [Double](repeating: 0.0, count: numFeedback)
}
}
func setNeuralWeightUpdateMethod(_ method: NeuralWeightUpdateMethod, _ parameter: Double?)
{
weightUpdateMethod = method
weightUpdateParameter = parameter
}
func feedForward(_ x: [Double], hPrev: [Double]) -> Double
{
// Get the input gate value
var zi = 0.0
var sum = 0.0
vDSP_dotprD(Wi, 1, x, 1, &zi, vDSP_Length(numInputs))
vDSP_dotprD(Ui, 1, hPrev, 1, &sum, vDSP_Length(numFeedback))
zi += sum
hi = 1.0 / (1.0 + exp(-zi))
// Get the forget gate value
var zf = 0.0
vDSP_dotprD(Wf, 1, x, 1, &zf, vDSP_Length(numInputs))
vDSP_dotprD(Uf, 1, hPrev, 1, &sum, vDSP_Length(numFeedback))
zf += sum
hf = 1.0 / (1.0 + exp(-zf))
// Get the output gate value
var zo = 0.0
vDSP_dotprD(Wo, 1, x, 1, &zo, vDSP_Length(numInputs))
vDSP_dotprD(Uo, 1, hPrev, 1, &sum, vDSP_Length(numFeedback))
zo += sum
ho = 1.0 / (1.0 + exp(-zo))
// Get the memory cell z sumation
var zc = 0.0
vDSP_dotprD(Wc, 1, x, 1, &zc, vDSP_Length(numInputs))
vDSP_dotprD(Uc, 1, hPrev, 1, &sum, vDSP_Length(numFeedback))
zc += sum
// Use the activation function function for the nonlinearity
switch (activation) {
case .none:
hc = zc
break
case .hyperbolicTangent:
hc = tanh(zc)
break
case .sigmoidWithCrossEntropy:
fallthrough
case .sigmoid:
hc = 1.0 / (1.0 + exp(-zc))
break
case .rectifiedLinear:
hc = zc
if (zc < 0) { hc = 0.0 }
break
case .softSign:
hc = zc / (1.0 + abs(zc))
break
case .softMax:
hc = exp(zc)
break
}
// Combine the forget and input gates into the cell summation
lastCellState = lastCellState * hf + hc * hi
// Use the activation function function for the nonlinearity
let squashedCellState = getSquashedCellState()
// Multiply the cell value by the output gate value to get the final result
h = squashedCellState * ho
return h
}
func getSquashedCellState() -> Double
{
// Use the activation function function for the nonlinearity
var squashedCellState : Double
switch (activation) {
case .none:
squashedCellState = lastCellState
break
case .hyperbolicTangent:
squashedCellState = tanh(lastCellState)
break
case .sigmoidWithCrossEntropy:
fallthrough
case .sigmoid:
squashedCellState = 1.0 / (1.0 + exp(-lastCellState))
break
case .rectifiedLinear:
squashedCellState = lastCellState
if (lastCellState < 0) { squashedCellState = 0.0 }
break
case .softSign:
squashedCellState = lastCellState / (1.0 + abs(lastCellState))
break
case .softMax:
squashedCellState = exp(lastCellState)
break
}
return squashedCellState
}
// Get the partial derivitive of the error with respect to the weighted sum
func getFinalNode𝟃E𝟃zs(_ 𝟃E𝟃h: Double)
{
// Store 𝟃E/𝟃h, set initial future error contributions to zero, and have the hidden layer routine do the work
self.𝟃E𝟃h = 𝟃E𝟃h
get𝟃E𝟃zs()
}
func reset𝟃E𝟃hs()
{
𝟃E𝟃h = 0.0
}
func addTo𝟃E𝟃hs(_ addition: Double)
{
𝟃E𝟃h += addition
}
func getWeightTimes𝟃E𝟃zs(_ weightIndex: Int) ->Double
{
var sum = Wo[weightIndex] * 𝟃E𝟃zo
sum += Wf[weightIndex] * 𝟃E𝟃zf
sum += Wc[weightIndex] * 𝟃E𝟃zc
sum += Wi[weightIndex] * 𝟃E𝟃zi
return sum
}
func getFeedbackWeightTimes𝟃E𝟃zs(_ weightIndex: Int) ->Double
{
var sum = Uo[weightIndex] * 𝟃E𝟃zo
sum += Uf[weightIndex] * 𝟃E𝟃zf
sum += Uc[weightIndex] * 𝟃E𝟃zc
sum += Ui[weightIndex] * 𝟃E𝟃zi
return sum
}
func get𝟃E𝟃zs()
{
// 𝟃E𝟃h contains 𝟃E/𝟃h for the current time step plus all future time steps.
// h = ho * squashedCellState -->
// 𝟃E/𝟃zo = 𝟃E/𝟃h ⋅ 𝟃h/𝟃ho ⋅ 𝟃ho/𝟃zo = 𝟃E/𝟃h ⋅ squashedCellState ⋅ (ho - ho²)
// 𝟃E/𝟃cellState = 𝟃E/𝟃h ⋅ 𝟃h/𝟃squashedCellState ⋅ 𝟃squashedCellState/𝟃cellState
// = 𝟃E/𝟃h ⋅ ho ⋅ act'(cellState) + 𝟃E_future/𝟃cellState (from previous backpropogation step)
𝟃E𝟃zo = 𝟃E𝟃h * getSquashedCellState() * (ho - ho * ho)
𝟃E𝟃cellState = 𝟃E𝟃h * ho * getActPrime(getSquashedCellState()) + 𝟃E𝟃cellState
// cellState = prevCellState * hf + hc * hi -->
// 𝟃E/𝟃zf = 𝟃E𝟃cellState ⋅ 𝟃cellState/𝟃hf ⋅ 𝟃hf/𝟃zf = 𝟃E𝟃cellState ⋅ prevCellState ⋅ (hf - hf²)
// 𝟃E/𝟃zc = 𝟃E𝟃cellState ⋅ 𝟃cellState/𝟃hc ⋅ 𝟃hc/𝟃zc = 𝟃E𝟃cellState ⋅ hi ⋅ act'(zc)
// 𝟃E/𝟃zi = 𝟃E𝟃cellState ⋅ 𝟃cellState/𝟃hi ⋅ 𝟃hi/𝟃zi = 𝟃E𝟃cellState ⋅ hc ⋅ (hi - hi²)
𝟃E𝟃zf = 𝟃E𝟃cellState * getPreviousCellState() * (hf - hf * hf)
𝟃E𝟃zc = 𝟃E𝟃cellState * hi * getActPrime(hc)
𝟃E𝟃zi = 𝟃E𝟃cellState * hc * (hi - hi * hi)
}
func getActPrime(_ h: Double) -> Double
{
// derivitive of the non-linearity: tanh' -> 1 - result^2, sigmoid -> result - result^2, rectlinear -> 0 if result<0 else 1
var actPrime = 0.0
switch (activation) {
case .none:
break
case .hyperbolicTangent:
actPrime = (1 - h * h)
break
case .sigmoidWithCrossEntropy:
fallthrough
case .sigmoid:
actPrime = (h - h * h)
break
case .rectifiedLinear:
actPrime = h <= 0.0 ? 0.0 : 1.0
break
case .softSign:
// Reconstitute z from h
var z : Double
if (h < 0) { // Negative z
z = h / (1.0 + h)
actPrime = -1.0 / ((1.0 + z) * (1.0 + z))
}
else { // Positive z
z = h / (1.0 - h)
actPrime = 1.0 / ((1.0 + z) * (1.0 + z))
}
break
case .softMax:
// Should not get here - SoftMax is only valid on output layer
break
}
return actPrime
}
func getPreviousCellState() -> Double
{
let prevValue = cellStateHistory.last
if (prevValue == nil) { return 0.0 }
return prevValue!
}
func clearWeightChanges()
{
𝟃E𝟃Wi = [Double](repeating: 0.0, count: numInputs)
𝟃E𝟃Ui = [Double](repeating: 0.0, count: numFeedback)
𝟃E𝟃Wf = [Double](repeating: 0.0, count: numInputs)
𝟃E𝟃Uf = [Double](repeating: 0.0, count: numFeedback)
𝟃E𝟃Wc = [Double](repeating: 0.0, count: numInputs)
𝟃E𝟃Uc = [Double](repeating: 0.0, count: numFeedback)
𝟃E𝟃Wo = [Double](repeating: 0.0, count: numInputs)
𝟃E𝟃Uo = [Double](repeating: 0.0, count: numFeedback)
}
func appendWeightChanges(_ x: [Double], hPrev: [Double]) -> Double
{
// Update each weight accumulation
// With 𝟃E/𝟃zo, we can get 𝟃E/𝟃Wo. zo = Wo⋅x + Uo⋅h(t-1)). 𝟃zo/𝟃Wo = x --> 𝟃E/𝟃Wo = 𝟃E/𝟃zo ⋅ 𝟃zo/𝟃Wo = 𝟃E/𝟃zo ⋅ x
vDSP_vsmaD(x, 1, &𝟃E𝟃zo, 𝟃E𝟃Wo, 1, &𝟃E𝟃Wo, 1, vDSP_Length(numInputs))
// 𝟃E/𝟃Uo. zo = Wo⋅x + Uo⋅h(t-1). 𝟃zo/𝟃Uo = h(t-1) --> 𝟃E/𝟃Uo = 𝟃E/𝟃zo ⋅ 𝟃zo/𝟃Uo = 𝟃E/𝟃zo ⋅ h(t-1)
vDSP_vsmaD(hPrev, 1, &𝟃E𝟃zo, 𝟃E𝟃Uo, 1, &𝟃E𝟃Uo, 1, vDSP_Length(numFeedback))
// With 𝟃E/𝟃zi, we can get 𝟃E/𝟃Wi. zi = Wi⋅x + Ui⋅h(t-1). 𝟃zi/𝟃Wi = x --> 𝟃E/𝟃Wi = 𝟃E/𝟃zi ⋅ 𝟃zi/𝟃Wi = 𝟃E/𝟃zi ⋅ x
vDSP_vsmaD(x, 1, &𝟃E𝟃zi, 𝟃E𝟃Wi, 1, &𝟃E𝟃Wi, 1, vDSP_Length(numInputs))
// 𝟃E/𝟃Ui. i = Wi⋅x + Ui⋅h(t-1). 𝟃zi/𝟃Ui = h(t-1) --> 𝟃E/𝟃Ui = 𝟃E/𝟃zi ⋅ 𝟃zi/𝟃Ui = 𝟃E/𝟃zi ⋅ h(t-1)
vDSP_vsmaD(hPrev, 1, &𝟃E𝟃zi, 𝟃E𝟃Ui, 1, &𝟃E𝟃Ui, 1, vDSP_Length(numFeedback))
// With 𝟃E/𝟃zf, we can get 𝟃E/𝟃Wf. zf = Wf⋅x + Uf⋅h(t-1). 𝟃zf/𝟃Wf = x --> 𝟃E/𝟃Wf = 𝟃E/𝟃zf ⋅ 𝟃zf/𝟃Wf = 𝟃E/𝟃zf ⋅ x
vDSP_vsmaD(x, 1, &𝟃E𝟃zf, 𝟃E𝟃Wf, 1, &𝟃E𝟃Wf, 1, vDSP_Length(numInputs))
// 𝟃E/𝟃Uf. f = Wf⋅x + Uf⋅h(t-1). 𝟃zf/𝟃Uf = h(t-1) --> 𝟃E/𝟃Uf = 𝟃E/𝟃zf ⋅ 𝟃zf/𝟃Uf = 𝟃E/𝟃zf ⋅ h(t-1)
vDSP_vsmaD(hPrev, 1, &𝟃E𝟃zf, 𝟃E𝟃Uf, 1, &𝟃E𝟃Uf, 1, vDSP_Length(numFeedback))
// With 𝟃E/𝟃zc, we can get 𝟃E/𝟃Wc. za = Wc⋅x + Uc⋅h(t-1). 𝟃za/𝟃Wa = x --> 𝟃E/𝟃Wc = 𝟃E/𝟃zc ⋅ 𝟃zc/𝟃Wc = 𝟃E/𝟃zc ⋅ x
vDSP_vsmaD(x, 1, &𝟃E𝟃zc, 𝟃E𝟃Wc, 1, &𝟃E𝟃Wc, 1, vDSP_Length(numInputs))
// 𝟃E/𝟃Ua. f = Wc⋅x + Uc⋅h(t-1). 𝟃zc/𝟃Uc = h(t-1) --> 𝟃E/𝟃Uc = 𝟃E/𝟃zc ⋅ 𝟃zc/𝟃Uc = 𝟃E/𝟃zc ⋅ h(t-1)
vDSP_vsmaD(hPrev, 1, &𝟃E𝟃zc, 𝟃E𝟃Uc, 1, &𝟃E𝟃Uc, 1, vDSP_Length(numFeedback))
return h
}
func updateWeightsFromAccumulations(_ averageTrainingRate: Double)
{
// Update the weights from the accumulations
switch weightUpdateMethod {
case .normal:
// weights -= accumulation * averageTrainingRate
var η = -averageTrainingRate
vDSP_vsmaD(𝟃E𝟃Wi, 1, &η, Wi, 1, &Wi, 1, vDSP_Length(numInputs))
vDSP_vsmaD(𝟃E𝟃Ui, 1, &η, Ui, 1, &Ui, 1, vDSP_Length(numFeedback))
vDSP_vsmaD(𝟃E𝟃Wf, 1, &η, Wf, 1, &Wf, 1, vDSP_Length(numInputs))
vDSP_vsmaD(𝟃E𝟃Uf, 1, &η, Uf, 1, &Uf, 1, vDSP_Length(numFeedback))
vDSP_vsmaD(𝟃E𝟃Wc, 1, &η, Wc, 1, &Wc, 1, vDSP_Length(numInputs))
vDSP_vsmaD(𝟃E𝟃Uc, 1, &η, Uc, 1, &Uc, 1, vDSP_Length(numFeedback))
vDSP_vsmaD(𝟃E𝟃Wo, 1, &η, Wo, 1, &Wo, 1, vDSP_Length(numInputs))
vDSP_vsmaD(𝟃E𝟃Uo, 1, &η, Uo, 1, &Uo, 1, vDSP_Length(numFeedback))
case .rmsProp:
// Update the rmsProp cache for Wi --> rmsprop_cache = decay_rate * rmsprop_cache + (1 - decay_rate) * gradient²
var gradSquared = [Double](repeating: 0.0, count: numInputs)
vDSP_vsqD(𝟃E𝟃Wi, 1, &gradSquared, 1, vDSP_Length(numInputs)) // Get the gradient squared
var decay = 1.0 - weightUpdateParameter!
vDSP_vsmulD(gradSquared, 1, &decay, &gradSquared, 1, vDSP_Length(numInputs)) // (1 - decay_rate) * gradient²
decay = weightUpdateParameter!
vDSP_vsmaD(WiWeightUpdateData, 1, &decay, gradSquared, 1, &WiWeightUpdateData, 1, vDSP_Length(numInputs))
// Update the weights --> weight += learning_rate * gradient / (sqrt(rmsprop_cache) + 1e-5)
for i in 0..<numInputs { gradSquared[i] = sqrt(WiWeightUpdateData[i]) } // Re-use gradSquared for efficiency
var small = 1.0e-05 // Small offset to make sure we are not dividing by zero
vDSP_vsaddD(gradSquared, 1, &small, &gradSquared, 1, vDSP_Length(numInputs)) // (sqrt(rmsprop_cache) + 1e-5)
var η = -averageTrainingRate // Needed for unsafe pointer conversion - negate for multiply-and-add vector operation
vDSP_svdivD(&η, gradSquared, 1, &gradSquared, 1, vDSP_Length(numInputs))
vDSP_vmaD(𝟃E𝟃Wi, 1, gradSquared, 1, Wi, 1, &Wi, 1, vDSP_Length(numInputs))
// Update the rmsProp cache for Ui --> rmsprop_cache = decay_rate * rmsprop_cache + (1 - decay_rate) * gradient²
gradSquared = [Double](repeating: 0.0, count: numFeedback)
vDSP_vsqD(𝟃E𝟃Ui, 1, &gradSquared, 1, vDSP_Length(numFeedback)) // Get the gradient squared
decay = 1.0 - weightUpdateParameter!
vDSP_vsmulD(gradSquared, 1, &decay, &gradSquared, 1, vDSP_Length(numFeedback)) // (1 - decay_rate) * gradient²
decay = weightUpdateParameter!
vDSP_vsmaD(UiWeightUpdateData, 1, &decay, gradSquared, 1, &UiWeightUpdateData, 1, vDSP_Length(numFeedback))
// Update the weights --> weight += learning_rate * gradient / (sqrt(rmsprop_cache) + 1e-5)
for i in 0..<numFeedback { gradSquared[i] = sqrt(UiWeightUpdateData[i]) } // Re-use gradSquared for efficiency
small = 1.0e-05 // Small offset to make sure we are not dividing by zero
vDSP_vsaddD(gradSquared, 1, &small, &gradSquared, 1, vDSP_Length(numFeedback)) // (sqrt(rmsprop_cache) + 1e-5)
η = -averageTrainingRate // Needed for unsafe pointer conversion - negate for multiply-and-add vector operation
vDSP_svdivD(&η, gradSquared, 1, &gradSquared, 1, vDSP_Length(numFeedback))
vDSP_vmaD(𝟃E𝟃Ui, 1, gradSquared, 1, Ui, 1, &Ui, 1, vDSP_Length(numFeedback))
// Update the rmsProp cache for Wf --> rmsprop_cache = decay_rate * rmsprop_cache + (1 - decay_rate) * gradient²
gradSquared = [Double](repeating: 0.0, count: numInputs)
vDSP_vsqD(𝟃E𝟃Wf, 1, &gradSquared, 1, vDSP_Length(numInputs)) // Get the gradient squared
decay = 1.0 - weightUpdateParameter!
vDSP_vsmulD(gradSquared, 1, &decay, &gradSquared, 1, vDSP_Length(numInputs)) // (1 - decay_rate) * gradient²
decay = weightUpdateParameter!
vDSP_vsmaD(WfWeightUpdateData, 1, &decay, gradSquared, 1, &WfWeightUpdateData, 1, vDSP_Length(numInputs))
// Update the weights --> weight += learning_rate * gradient / (sqrt(rmsprop_cache) + 1e-5)
for i in 0..<numInputs { gradSquared[i] = sqrt(WfWeightUpdateData[i]) } // Re-use gradSquared for efficiency
small = 1.0e-05 // Small offset to make sure we are not dividing by zero
vDSP_vsaddD(gradSquared, 1, &small, &gradSquared, 1, vDSP_Length(numInputs)) // (sqrt(rmsprop_cache) + 1e-5)
η = -averageTrainingRate // Needed for unsafe pointer conversion - negate for multiply-and-add vector operation
vDSP_svdivD(&η, gradSquared, 1, &gradSquared, 1, vDSP_Length(numInputs))
vDSP_vmaD(𝟃E𝟃Wf, 1, gradSquared, 1, Wf, 1, &Wf, 1, vDSP_Length(numInputs))
// Update the rmsProp cache for Uf --> rmsprop_cache = decay_rate * rmsprop_cache + (1 - decay_rate) * gradient²
gradSquared = [Double](repeating: 0.0, count: numFeedback)
vDSP_vsqD(𝟃E𝟃Uf, 1, &gradSquared, 1, vDSP_Length(numFeedback)) // Get the gradient squared
decay = 1.0 - weightUpdateParameter!
vDSP_vsmulD(gradSquared, 1, &decay, &gradSquared, 1, vDSP_Length(numFeedback)) // (1 - decay_rate) * gradient²
decay = weightUpdateParameter!
vDSP_vsmaD(UfWeightUpdateData, 1, &decay, gradSquared, 1, &UfWeightUpdateData, 1, vDSP_Length(numFeedback))
// Update the weights --> weight += learning_rate * gradient / (sqrt(rmsprop_cache) + 1e-5)
for i in 0..<numFeedback { gradSquared[i] = sqrt(UfWeightUpdateData[i]) } // Re-use gradSquared for efficiency
small = 1.0e-05 // Small offset to make sure we are not dividing by zero
vDSP_vsaddD(gradSquared, 1, &small, &gradSquared, 1, vDSP_Length(numFeedback)) // (sqrt(rmsprop_cache) + 1e-5)
η = -averageTrainingRate // Needed for unsafe pointer conversion - negate for multiply-and-add vector operation
vDSP_svdivD(&η, gradSquared, 1, &gradSquared, 1, vDSP_Length(numFeedback))
vDSP_vmaD(𝟃E𝟃Uf, 1, gradSquared, 1, Uf, 1, &Uf, 1, vDSP_Length(numFeedback))
// Update the rmsProp cache for Wc --> rmsprop_cache = decay_rate * rmsprop_cache + (1 - decay_rate) * gradient²
gradSquared = [Double](repeating: 0.0, count: numInputs)
vDSP_vsqD(𝟃E𝟃Wc, 1, &gradSquared, 1, vDSP_Length(numInputs)) // Get the gradient squared
decay = 1.0 - weightUpdateParameter!
vDSP_vsmulD(gradSquared, 1, &decay, &gradSquared, 1, vDSP_Length(numInputs)) // (1 - decay_rate) * gradient²
decay = weightUpdateParameter!
vDSP_vsmaD(WcWeightUpdateData, 1, &decay, gradSquared, 1, &WcWeightUpdateData, 1, vDSP_Length(numInputs))
// Update the weights --> weight += learning_rate * gradient / (sqrt(rmsprop_cache) + 1e-5)
for i in 0..<numInputs { gradSquared[i] = sqrt(WcWeightUpdateData[i]) } // Re-use gradSquared for efficiency
small = 1.0e-05 // Small offset to make sure we are not dividing by zero
vDSP_vsaddD(gradSquared, 1, &small, &gradSquared, 1, vDSP_Length(numInputs)) // (sqrt(rmsprop_cache) + 1e-5)
η = -averageTrainingRate // Needed for unsafe pointer conversion - negate for multiply-and-add vector operation
vDSP_svdivD(&η, gradSquared, 1, &gradSquared, 1, vDSP_Length(numInputs))
vDSP_vmaD(𝟃E𝟃Wc, 1, gradSquared, 1, Wc, 1, &Wc, 1, vDSP_Length(numInputs))
// Update the rmsProp cache for Uc --> rmsprop_cache = decay_rate * rmsprop_cache + (1 - decay_rate) * gradient²
gradSquared = [Double](repeating: 0.0, count: numFeedback)
vDSP_vsqD(𝟃E𝟃Uc, 1, &gradSquared, 1, vDSP_Length(numFeedback)) // Get the gradient squared
decay = 1.0 - weightUpdateParameter!
vDSP_vsmulD(gradSquared, 1, &decay, &gradSquared, 1, vDSP_Length(numFeedback)) // (1 - decay_rate) * gradient²
decay = weightUpdateParameter!
vDSP_vsmaD(UcWeightUpdateData, 1, &decay, gradSquared, 1, &UcWeightUpdateData, 1, vDSP_Length(numFeedback))
// Update the weights --> weight += learning_rate * gradient / (sqrt(rmsprop_cache) + 1e-5)
for i in 0..<numFeedback { gradSquared[i] = sqrt(UcWeightUpdateData[i]) } // Re-use gradSquared for efficiency
small = 1.0e-05 // Small offset to make sure we are not dividing by zero
vDSP_vsaddD(gradSquared, 1, &small, &gradSquared, 1, vDSP_Length(numFeedback)) // (sqrt(rmsprop_cache) + 1e-5)
η = -averageTrainingRate // Needed for unsafe pointer conversion - negate for multiply-and-add vector operation
vDSP_svdivD(&η, gradSquared, 1, &gradSquared, 1, vDSP_Length(numFeedback))
vDSP_vmaD(𝟃E𝟃Uc, 1, gradSquared, 1, Uc, 1, &Uc, 1, vDSP_Length(numFeedback))
// Update the rmsProp cache for Wo --> rmsprop_cache = decay_rate * rmsprop_cache + (1 - decay_rate) * gradient²
gradSquared = [Double](repeating: 0.0, count: numInputs)
vDSP_vsqD(𝟃E𝟃Wo, 1, &gradSquared, 1, vDSP_Length(numInputs)) // Get the gradient squared
decay = 1.0 - weightUpdateParameter!
vDSP_vsmulD(gradSquared, 1, &decay, &gradSquared, 1, vDSP_Length(numInputs)) // (1 - decay_rate) * gradient²
decay = weightUpdateParameter!
vDSP_vsmaD(WoWeightUpdateData, 1, &decay, gradSquared, 1, &WoWeightUpdateData, 1, vDSP_Length(numInputs))
// Update the weights --> weight += learning_rate * gradient / (sqrt(rmsprop_cache) + 1e-5)
for i in 0..<numInputs { gradSquared[i] = sqrt(WoWeightUpdateData[i]) } // Re-use gradSquared for efficiency
small = 1.0e-05 // Small offset to make sure we are not dividing by zero
vDSP_vsaddD(gradSquared, 1, &small, &gradSquared, 1, vDSP_Length(numInputs)) // (sqrt(rmsprop_cache) + 1e-5)
η = -averageTrainingRate // Needed for unsafe pointer conversion - negate for multiply-and-add vector operation
vDSP_svdivD(&η, gradSquared, 1, &gradSquared, 1, vDSP_Length(numInputs))
vDSP_vmaD(𝟃E𝟃Wo, 1, gradSquared, 1, Wo, 1, &Wo, 1, vDSP_Length(numInputs))
// Update the rmsProp cache for Uo --> rmsprop_cache = decay_rate * rmsprop_cache + (1 - decay_rate) * gradient²
gradSquared = [Double](repeating: 0.0, count: numFeedback)
vDSP_vsqD(𝟃E𝟃Uo, 1, &gradSquared, 1, vDSP_Length(numFeedback)) // Get the gradient squared
decay = 1.0 - weightUpdateParameter!
vDSP_vsmulD(gradSquared, 1, &decay, &gradSquared, 1, vDSP_Length(numFeedback)) // (1 - decay_rate) * gradient²
decay = weightUpdateParameter!
vDSP_vsmaD(UoWeightUpdateData, 1, &decay, gradSquared, 1, &UoWeightUpdateData, 1, vDSP_Length(numFeedback))
// Update the weights --> weight += learning_rate * gradient / (sqrt(rmsprop_cache) + 1e-5)
for i in 0..<numFeedback { gradSquared[i] = sqrt(UoWeightUpdateData[i]) } // Re-use gradSquared for efficiency
small = 1.0e-05 // Small offset to make sure we are not dividing by zero
vDSP_vsaddD(gradSquared, 1, &small, &gradSquared, 1, vDSP_Length(numFeedback)) // (sqrt(rmsprop_cache) + 1e-5)
η = -averageTrainingRate // Needed for unsafe pointer conversion - negate for multiply-and-add vector operation
vDSP_svdivD(&η, gradSquared, 1, &gradSquared, 1, vDSP_Length(numFeedback))
vDSP_vmaD(𝟃E𝟃Uo, 1, gradSquared, 1, Uo, 1, &Uo, 1, vDSP_Length(numFeedback))
}
}
func decayWeights(_ decayFactor : Double)
{
var λ = decayFactor // Needed for unsafe pointer conversion
vDSP_vsmulD(Wi, 1, &λ, &Wi, 1, vDSP_Length(numInputs-1))
vDSP_vsmulD(Ui, 1, &λ, &Ui, 1, vDSP_Length(numFeedback))
vDSP_vsmulD(Wf, 1, &λ, &Wf, 1, vDSP_Length(numInputs-1))
vDSP_vsmulD(Uf, 1, &λ, &Uf, 1, vDSP_Length(numFeedback))
vDSP_vsmulD(Wc, 1, &λ, &Wc, 1, vDSP_Length(numInputs-1))
vDSP_vsmulD(Uc, 1, &λ, &Uc, 1, vDSP_Length(numFeedback))
vDSP_vsmulD(Wo, 1, &λ, &Wo, 1, vDSP_Length(numInputs-1))
vDSP_vsmulD(Uo, 1, &λ, &Uo, 1, vDSP_Length(numFeedback))
}
func resetSequence()
{
h = 0.0
lastCellState = 0.0
ho = 0.0
hc = 0.0
hi = 0.0
hf = 0.0
𝟃E𝟃zo = 0.0
𝟃E𝟃zi = 0.0
𝟃E𝟃zf = 0.0
𝟃E𝟃zc = 0.0
𝟃E𝟃cellState = 0.0
outputHistory = [0.0] // first 'previous' value is zero
cellStateHistory = [0.0] // first 'previous' value is zero
outputGateHistory = [0.0] // first 'previous' value is zero
memoryCellHistory = [0.0] // first 'previous' value is zero
inputGateHistory = [0.0] // first 'previous' value is zero
forgetGateHistory = [0.0] // first 'previous' value is zero
}
func storeRecurrentValues()
{
outputHistory.append(h)
cellStateHistory.append(lastCellState)
outputGateHistory.append(ho)
memoryCellHistory.append(hc)
inputGateHistory.append(hi)
forgetGateHistory.append(hf)
}
func getLastRecurrentValue()
{
h = outputHistory.removeLast()
lastCellState = cellStateHistory.removeLast()
ho = outputGateHistory.removeLast()
hc = memoryCellHistory.removeLast()
hi = inputGateHistory.removeLast()
hf = forgetGateHistory.removeLast()
}
func getPreviousOutputValue() -> Double
{
let prevValue = outputHistory.last
if (prevValue == nil) { return 0.0 }
return prevValue!
}
}
final class LSTMNeuralLayer: NeuralLayer {
// Nodes
var nodes : [LSTMNeuralNode]
var dataSet : DataSet? // Sequence data set (inputs and outputs)
/// Create the neural network layer based on a tuple (number of nodes, activation function)
init(numInputs : Int, layerDefinition: (layerType: NeuronLayerType, numNodes: Int, activation: NeuralActivationFunction, auxiliaryData: AnyObject?))
{
nodes = []
for _ in 0..<layerDefinition.numNodes {
nodes.append(LSTMNeuralNode(numInputs: numInputs, numFeedbacks: layerDefinition.numNodes, activationFunction: layerDefinition.activation))
}
}
// Initialize the weights
func initWeights(_ startWeights: [Double]!)
{
if let startWeights = startWeights {
if (startWeights.count >= nodes.count * nodes[0].numWeights) {
// If there are enough weights for all nodes, split the weights and initialize
var startIndex = 0
for node in nodes {
let subArray = Array(startWeights[startIndex...(startIndex+node.numWeights-1)])
node.initWeights(subArray)
startIndex += node.numWeights
}
}
else {
// If there are not enough weights for all nodes, initialize each node with the set given
for node in nodes {
node.initWeights(startWeights)
}
}
}
else {
// No specified weights - just initialize normally
for node in nodes {
node.initWeights(nil)
}
}
}
func getWeights() -> [Double]
{
var weights: [Double] = []
for node in nodes {
weights += node.Wi
weights += node.Ui
weights += node.Wf
weights += node.Uf
weights += node.Wc
weights += node.Uc
weights += node.Wo
weights += node.Uo
}
return weights
}
func setNeuralWeightUpdateMethod(_ method: NeuralWeightUpdateMethod, _ parameter: Double?)
{
for node in nodes {
node.setNeuralWeightUpdateMethod(method, parameter)
}
}
func getLastOutput() -> [Double]
{
var h: [Double] = []
for node in nodes {
h.append(node.h)
}
return h
}
func getNodeCount() -> Int
{
return nodes.count
}
func getWeightsPerNode()-> Int
{
return nodes[0].numWeights
}
func getActivation()-> NeuralActivationFunction
{
return nodes[0].activation
}
func feedForward(_ x: [Double]) -> [Double]
{
// Gather the previous outputs for the feedback
var hPrev : [Double] = []
for node in nodes {
hPrev.append(node.getPreviousOutputValue())
}
var outputs : [Double] = []
// Assume input array already has bias constant 1.0 appended
// Fully-connected nodes means all nodes get the same input array
if (nodes[0].activation == .softMax) {
var sum = 0.0
for node in nodes { // Sum each output
sum += node.feedForward(x, hPrev: hPrev)
}
let scale = 1.0 / sum // Do division once for efficiency
for node in nodes { // Get the outputs scaled by the sum to give the probability distribuition for the output
node.h *= scale
outputs.append(node.h)
}
}
else {
for node in nodes {
outputs.append(node.feedForward(x, hPrev: hPrev))
}
}
return outputs
}
func getFinalLayer𝟃E𝟃zs(_ 𝟃E𝟃h: [Double])
{
for nNodeIndex in 0..<nodes.count {
// Start with the portion from the squared error term
nodes[nNodeIndex].getFinalNode𝟃E𝟃zs(𝟃E𝟃h[nNodeIndex])
}
}
func getLayer𝟃E𝟃zs(_ nextLayer: NeuralLayer)
{
// Get 𝟃E/𝟃h
for nNodeIndex in 0..<nodes.count {
// Reset the 𝟃E/𝟃h total
nodes[nNodeIndex].reset𝟃E𝟃hs()
// Add each portion from the nodes in the next forward layer
nodes[nNodeIndex].addTo𝟃E𝟃hs(nextLayer.get𝟃E𝟃hForNodeInPreviousLayer(nNodeIndex))
// Add each portion from the nodes in this layer, using the feedback weights. This adds 𝟃Efuture/𝟃h
for node in nodes {
nodes[nNodeIndex].addTo𝟃E𝟃hs(node.getFeedbackWeightTimes𝟃E𝟃zs(nNodeIndex))
}
}
// Calculate 𝟃E/𝟃zs for this time step from 𝟃E/𝟃h
for node in nodes {
node.get𝟃E𝟃zs()
}
}
func get𝟃E𝟃hForNodeInPreviousLayer(_ inputIndex: Int) ->Double
{
var sum = 0.0
for node in nodes {
sum += node.getWeightTimes𝟃E𝟃zs(inputIndex)
}
return sum
}
func clearWeightChanges()
{
for node in nodes {
node.clearWeightChanges()
}
}
func appendWeightChanges(_ x: [Double]) -> [Double]
{
// Gather the previous outputs for the feedback
var hPrev : [Double] = []
for node in nodes {
hPrev.append(node.getPreviousOutputValue())
}
var outputs : [Double] = []
// Assume input array already has bias constant 1.0 appended
// Fully-connected nodes means all nodes get the same input array
for node in nodes {
outputs.append(node.appendWeightChanges(x, hPrev: hPrev))
}
return outputs
}
func updateWeightsFromAccumulations(_ averageTrainingRate: Double, weightDecay: Double)
{
// Have each node update it's weights from the accumulations
for node in nodes {
if (weightDecay < 1) { node.decayWeights(weightDecay) }
node.updateWeightsFromAccumulations(averageTrainingRate)
}
}
func decayWeights(_ decayFactor : Double)
{
for node in nodes {
node.decayWeights(decayFactor)
}
}
func getSingleNodeClassifyValue() -> Double
{
let activation = nodes[0].activation
if (activation == .hyperbolicTangent || activation == .rectifiedLinear) { return 0.0 }
return 0.5
}
func resetSequence()
{
for node in nodes {
node.resetSequence()
}
}
func storeRecurrentValues()
{
for node in nodes {
node.storeRecurrentValues()
}
}
func retrieveRecurrentValues(_ sequenceIndex: Int)
{
// Set the last recurrent value in the history array to the last output
for node in nodes {
node.getLastRecurrentValue()
}
}
func gradientCheck(x: [Double], ε: Double, Δ: Double, network: NeuralNetwork) -> Bool
{
//!!
return true
}
}