forked from KevinCoble/AIToolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecurrentNeuralNetwork.swift
1114 lines (995 loc) · 42.3 KB
/
RecurrentNeuralNetwork.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
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// RecurrentNeuralNetwork.swift
// AIToolbox
//
// Created by Kevin Coble on 5/5/16.
// Copyright © 2016 Kevin Coble. All rights reserved.
//
import Foundation
#if os(Linux)
#else
import Accelerate
#endif
final class RecurrentNeuralNode {
// Activation function
let activation : NeuralActivationFunction
let numWeights : Int // This includes weights from inputs and from feedback
let numInputs : Int
let numFeedback : Int
var W : [Double] // Weights for inputs from previous layer
var U : [Double] // Weights for recurrent input data from this layer
var h : Double // Last result calculated
var outputHistory : [Double] // History of output for the sequence
var 𝟃E𝟃h : Double // Gradient in error for this time step and future time steps with respect to output of this node
var 𝟃E𝟃z : Double // Gradient of error with respect to weighted sum
var 𝟃E𝟃W : [Double] // Accumulated weight W change gradient
var 𝟃E𝟃U : [Double] // Accumulated weight U change gradient
var weightUpdateMethod = NeuralWeightUpdateMethod.normal
var weightUpdateParameter : Double? // Decay rate for rms prop weight updates
var WweightUpdateData : [Double] = [] // Array of running average for rmsprop
var UweightUpdateData : [Double] = [] // Array of running average for rmsprop
/// Create the 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
numWeights = self.numInputs + self.numFeedback
W = []
U = []
h = 0.0
outputHistory = []
𝟃E𝟃h = 0.0
𝟃E𝟃z = 0.0
𝟃E𝟃W = []
𝟃E𝟃U = []
}
// Initialize the weights
func initWeights(_ startWeights: [Double]!)
{
if let startWeights = startWeights {
if (startWeights.count == 1) {
W = [Double](repeating: startWeights[0], count: numInputs)
U = [Double](repeating: startWeights[0], count: numFeedback)
}
else if (startWeights.count == numInputs+numFeedback) {
// Full weight array, just split into the two weight arrays
W = Array(startWeights[0..<numInputs])
U = Array(startWeights[numInputs..<numInputs+numFeedback])
}
else {
W = []
var index = 0 // First number (if more than 1) goes into the bias weight, then repeat the initial
for _ in 0..<numInputs-1 {
if (index >= startWeights.count-1) { index = 0 } // Wrap if necessary
W.append(startWeights[index])
index += 1
}
W.append(startWeights[startWeights.count-1]) // Add the bias term
index = 0
U = []
for _ in 0..<numFeedback {
if (index >= startWeights.count-1) { index = 1 } // Wrap if necessary
U.append(startWeights[index])
index += 1
}
}
}
else {
W = []
for _ in 0..<numInputs-1 {
W.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
}
W.append(Gaussian.gaussianRandom(0.0, standardDeviation:1.0)) // Bias weight - Initialize to a random number to break initial symmetry of the network
U = []
for _ in 0..<numFeedback {
U.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) {
WweightUpdateData = [Double](repeating: 0.0, count: numInputs)
UweightUpdateData = [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 weighted sum: z = W⋅x + U⋅h(t-1)
var z = 0.0
var sum = 0.0
vDSP_dotprD(W, 1, x, 1, &z, vDSP_Length(numInputs))
vDSP_dotprD(U, 1, hPrev, 1, &sum, vDSP_Length(numFeedback))
z += sum
// Use the activation function function for the nonlinearity: h = act(z)
switch (activation) {
case .none:
h = z
break
case .hyperbolicTangent:
h = tanh(z)
break
case .sigmoidWithCrossEntropy:
fallthrough
case .sigmoid:
h = 1.0 / (1.0 + exp(-z))
break
case .rectifiedLinear:
h = z
if (z < 0) { h = 0.0 }
break
case .softSign:
h = z / (1.0 + abs(z))
break
case .softMax:
h = exp(z)
break
}
return h
}
// Get the partial derivitive of the error with respect to the weighted sum
func getFinalNode𝟃E𝟃zs(_ 𝟃E𝟃h: Double)
{
// Calculate 𝟃E/𝟃z. 𝟃E/𝟃z = 𝟃E/𝟃h ⋅ 𝟃h/𝟃z = 𝟃E/𝟃h ⋅ derivitive of nonlinearity
// derivitive of the non-linearity: tanh' -> 1 - result^2, sigmoid -> result - result^2, rectlinear -> 0 if result<0 else 1
switch (activation) {
case .none:
𝟃E𝟃z = 𝟃E𝟃h
break
case .hyperbolicTangent:
𝟃E𝟃z = 𝟃E𝟃h * (1 - h * h)
break
case .sigmoid:
𝟃E𝟃z = 𝟃E𝟃h * (h - h * h)
break
case .sigmoidWithCrossEntropy:
𝟃E𝟃z = 𝟃E𝟃h
break
case .rectifiedLinear:
𝟃E𝟃z = h <= 0.0 ? 0.0 : 𝟃E𝟃h
break
case .softSign:
// Reconstitute z from h
var z : Double
if (h < 0) { // Negative z
z = h / (1.0 + h)
𝟃E𝟃z = -𝟃E𝟃h / ((1.0 + z) * (1.0 + z))
}
else { // Positive z
z = h / (1.0 - h)
𝟃E𝟃z = 𝟃E𝟃h / ((1.0 + z) * (1.0 + z))
}
break
case .softMax:
𝟃E𝟃z = 𝟃E𝟃h
break
}
}
func reset𝟃E𝟃hs()
{
𝟃E𝟃h = 0.0
}
func addTo𝟃E𝟃hs(_ addition: Double)
{
𝟃E𝟃h += addition
}
func getWeightTimes𝟃E𝟃zs(_ weightIndex: Int) ->Double
{
return W[weightIndex] * 𝟃E𝟃z
}
func getFeedbackWeightTimes𝟃E𝟃zs(_ weightIndex: Int) ->Double
{
return U[weightIndex] * 𝟃E𝟃z
}
func get𝟃E𝟃z()
{
// 𝟃E𝟃h contains 𝟃E/𝟃h for the current time step plus all future time steps.
// Calculate 𝟃E𝟃z. 𝟃E/𝟃z = 𝟃E/𝟃h ⋅ 𝟃h/𝟃z = 𝟃E/𝟃h ⋅ derivitive of non-linearity
// derivitive of the non-linearity: tanh' -> 1 - result^2, sigmoid -> result - result^2, rectlinear -> 0 if result<0 else 1
switch (activation) {
case .none:
break
case .hyperbolicTangent:
𝟃E𝟃z = 𝟃E𝟃h * (1 - h * h)
break
case .sigmoidWithCrossEntropy:
fallthrough
case .sigmoid:
𝟃E𝟃z = 𝟃E𝟃h * (h - h * h)
break
case .rectifiedLinear:
𝟃E𝟃z = h < 0.0 ? 0.0 : 𝟃E𝟃h
break
case .softSign:
// Reconstitute z from h
var z : Double
if (h < 0) { // Negative z
z = h / (1.0 + h)
𝟃E𝟃z = -𝟃E𝟃h / ((1.0 + z) * (1.0 + z))
}
else { // Positive z
z = h / (1.0 - h)
𝟃E𝟃z = 𝟃E𝟃h / ((1.0 + z) * (1.0 + z))
}
break
case .softMax:
// Should not get here - SoftMax is only valid on output layer
break
}
}
func clearWeightChanges()
{
𝟃E𝟃W = [Double](repeating: 0.0, count: numInputs)
𝟃E𝟃U = [Double](repeating: 0.0, count: numFeedback)
}
func appendWeightChanges(_ x: [Double], hPrev: [Double]) -> Double
{
// Update each weight accumulation
// z = W⋅x + U⋅hPrev, therefore
// 𝟃E/𝟃W = 𝟃E/𝟃z ⋅ 𝟃z/𝟃W = 𝟃E/𝟃z ⋅ x
// 𝟃E/𝟃U = 𝟃E/𝟃z ⋅ 𝟃z/𝟃U = 𝟃E/𝟃z ⋅ hPrev
// 𝟃E/𝟃W += 𝟃E/𝟃z ⋅ 𝟃z/𝟃W = 𝟃E/𝟃z ⋅ x
vDSP_vsmaD(x, 1, &𝟃E𝟃z, 𝟃E𝟃W, 1, &𝟃E𝟃W, 1, vDSP_Length(numInputs))
// 𝟃E/𝟃U += 𝟃E/𝟃z ⋅ 𝟃z/𝟃U = 𝟃E/𝟃z ⋅ hPrev
vDSP_vsmaD(hPrev, 1, &𝟃E𝟃z, 𝟃E𝟃U, 1, &𝟃E𝟃U, 1, vDSP_Length(numFeedback))
return h // return output for next layer
}
func updateWeightsFromAccumulations(_ averageTrainingRate: Double)
{
// Update the weights from the accumulations
switch weightUpdateMethod {
case .normal:
// W -= 𝟃E/𝟃W * averageTrainingRate, U -= 𝟃E/𝟃U * averageTrainingRate
var η = -averageTrainingRate // Needed for unsafe pointer conversion - negate for multiply-and-add vector operation
vDSP_vsmaD(𝟃E𝟃W, 1, &η, W, 1, &W, 1, vDSP_Length(numInputs))
vDSP_vsmaD(𝟃E𝟃U, 1, &η, U, 1, &U, 1, vDSP_Length(numFeedback))
case .rmsProp:
// Update the rmsProp cache for W --> rmsprop_cache = decay_rate * rmsprop_cache + (1 - decay_rate) * gradient²
var gradSquared = [Double](repeating: 0.0, count: numInputs)
vDSP_vsqD(𝟃E𝟃W, 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(WweightUpdateData, 1, &decay, gradSquared, 1, &WweightUpdateData, 1, vDSP_Length(numInputs))
// Update the weights --> weight += learning_rate * gradient / (sqrt(rmsprop_cache) + 1e-5)
for i in 0..<numInputs { gradSquared[i] = sqrt(WweightUpdateData[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𝟃W, 1, gradSquared, 1, W, 1, &W, 1, vDSP_Length(numInputs))
// Update the rmsProp cache for U --> rmsprop_cache = decay_rate * rmsprop_cache + (1 - decay_rate) * gradient²
gradSquared = [Double](repeating: 0.0, count: numFeedback)
vDSP_vsqD(𝟃E𝟃U, 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(UweightUpdateData, 1, &decay, gradSquared, 1, &UweightUpdateData, 1, vDSP_Length(numFeedback))
// Update the weights --> weight += learning_rate * gradient / (sqrt(rmsprop_cache) + 1e-5)
for i in 0..<numFeedback { gradSquared[i] = sqrt(UweightUpdateData[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𝟃U, 1, gradSquared, 1, U, 1, &U, 1, vDSP_Length(numFeedback))
}
}
func decayWeights(_ decayFactor : Double)
{
var λ = decayFactor // Needed for unsafe pointer conversion
vDSP_vsmulD(W, 1, &λ, &W, 1, vDSP_Length(numInputs-1))
vDSP_vsmulD(U, 1, &λ, &U, 1, vDSP_Length(numFeedback))
}
func resetSequence()
{
h = 0.0
outputHistory = [0.0] // first 'previous' value is zero
𝟃E𝟃z = 0.0 // Backward propogation previous 𝟃E𝟃z (𝟃E𝟃z from next time step in sequence) is zero
}
func storeRecurrentValues()
{
outputHistory.append(h)
}
func getLastRecurrentValue()
{
h = outputHistory.removeLast()
}
func getPreviousOutputValue() -> Double
{
let hPrev = outputHistory.last
if (hPrev == nil) { return 0.0 }
return hPrev!
}
func gradientCheck(x: [Double], ε: Double, Δ: Double, network: NeuralNetwork) -> Bool
{
var result = true
// Iterate through each W parameter
for index in 0..<W.count {
let oldValue = W[index]
// Get the network loss with a small addition to the parameter
W[index] += ε
_ = network.feedForward(x)
var plusLoss : [Double]
do {
plusLoss = try network.getResultLoss()
}
catch {
return false
}
// Get the network loss with a small subtraction from the parameter
W[index] = oldValue - ε
_ = network.feedForward(x)
var minusLoss : [Double]
do {
minusLoss = try network.getResultLoss()
}
catch {
return false
}
W[index] = oldValue
// Iterate over the results
for resultIndex in 0..<plusLoss.count {
// Get the numerical gradient estimate 𝟃E/𝟃W
let gradient = (plusLoss[resultIndex] - minusLoss[resultIndex]) / (2.0 * ε)
// Compare with the analytical gradient
let difference = abs(gradient - 𝟃E𝟃W[index])
// print("difference = \(difference)")
if (difference > Δ) {
result = false
}
}
}
// Iterate through each U parameter
for index in 0..<U.count {
let oldValue = U[index]
// Get the network loss with a small addition to the parameter
U[index] += ε
_ = network.feedForward(x)
var plusLoss : [Double]
do {
plusLoss = try network.getResultLoss()
}
catch {
return false
}
// Get the network loss with a small subtraction from the parameter
U[index] = oldValue - ε
_ = network.feedForward(x)
var minusLoss : [Double]
do {
minusLoss = try network.getResultLoss()
}
catch {
return false
}
U[index] = oldValue
// Iterate over the results
for resultIndex in 0..<plusLoss.count {
// Get the numerical gradient estimate 𝟃E/𝟃U
let gradient = (plusLoss[resultIndex] - minusLoss[resultIndex]) / (2.0 * ε)
// Compare with the analytical gradient
let difference = abs(gradient - 𝟃E𝟃U[index])
// print("difference = \(difference)")
if (difference > Δ) {
result = false
}
}
}
return result
}
}
/// Class for a recurrent network layer with individual nodes (slower, but easier to get into details)
final class RecurrentNeuralLayerWithNodes: NeuralLayer {
// Nodes
var nodes : [RecurrentNeuralNode]
var bpttSequenceIndex: Int
/// 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(RecurrentNeuralNode(numInputs: numInputs, numFeedbacks: layerDefinition.numNodes, activationFunction: layerDefinition.activation))
}
bpttSequenceIndex = 0
}
// 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.W
weights += node.U
}
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 {
nodes[nNodeIndex].reset𝟃E𝟃hs()
// Add each portion from the nodes in the next forward layer to get 𝟃Enow/𝟃h
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/𝟃z from 𝟃E/𝟃h
for node in nodes {
node.get𝟃E𝟃z()
}
}
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()
{
// Have each node reset
for node in nodes {
node.resetSequence()
}
}
func storeRecurrentValues()
{
for node in nodes {
node.storeRecurrentValues()
}
}
func retrieveRecurrentValues(_ sequenceIndex: Int)
{
bpttSequenceIndex = sequenceIndex
// 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
{
var result = true
for node in nodes {
if (!node.gradientCheck(x: x, ε: ε, Δ: Δ, network: network)) { result = false }
}
return result
}
}
/// Class for a recurrent network layer without individual nodes (faster, but some things hidden in the matrix math)
final class RecurrentNeuralLayer: NeuralLayer {
var activation : NeuralActivationFunction
var numInputs = 0
var numNodes : Int
var W : [Double] = [] // Weights for inputs from previous layer
var U : [Double] = [] // Weights for recurrent input data from this layer
var h : [Double] // Last result calculated
var outputHistory : [[Double]] // History of output for the sequence
var 𝟃E𝟃z : [Double] // Gradient in error with respect to weighted sum
var 𝟃E𝟃W : [Double] = [] // Accumulated weight W change gradient
var 𝟃E𝟃U : [Double] = [] // Accumulated weight U change gradient
var bpttSequenceIndex : Int
var weightUpdateMethod = NeuralWeightUpdateMethod.normal
var weightUpdateParameter : Double? // Decay rate for rms prop weight updates
var WweightUpdateData : [Double] = [] // Array of running average for rmsprop
var UweightUpdateData : [Double] = [] // Array of running average for rmsprop
/// 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?))
{
activation = layerDefinition.activation
self.numInputs = numInputs
self.numNodes = layerDefinition.numNodes
h = [Double](repeating: 0.0, count: numNodes)
outputHistory = []
𝟃E𝟃z = [Double](repeating: 0.0, count: numNodes)
bpttSequenceIndex = 0
}
// Initialize the weights
func initWeights(_ startWeights: [Double]!)
{
let numWeights = (numInputs + 1) * numNodes // Add bias offset
let numRcurrentWeights = numNodes * numNodes // Add bias offset
W = []
U = []
if let startWeights = startWeights {
if (startWeights.count >= numNodes * (numInputs + 1 + numNodes)) {
// If there are enough weights for all nodes, split the weights and initialize
W = Array(startWeights[0..<(numNodes * (numInputs + 1))])
U = Array(startWeights[(numNodes * (numInputs + 1))..<(numNodes * (numInputs + 1 + numNodes))])
}
else {
// If there are not enough weights for all nodes, initialize each weight set with the set given
var index = 0
for _ in 0..<((numInputs + 1) * numNodes) {
W.append(startWeights[index])
index += 1
if (index >= startWeights.count) { index = 0 }
}
for _ in 0..<(numNodes * numNodes) {
U.append(startWeights[index])
index += 1
if (index >= startWeights.count) { index = 0 }
}
}
}
else {
// No specified weights - just initialize normally
// Allocate the weight array using 'Xavier' initialization
var weightDiviser: Double
if (activation == .rectifiedLinear) {
weightDiviser = 1 / sqrt(Double(numInputs) * 0.5)
}
else {
weightDiviser = 1 / sqrt(Double(numInputs))
}
for _ in 0..<numWeights {
W.append(Gaussian.gaussianRandom(0.0, standardDeviation : 1.0) * weightDiviser)
}
if (activation == .rectifiedLinear) {
weightDiviser = 1 / sqrt(Double(numNodes) * 0.5)
}
else {
weightDiviser = 1 / sqrt(Double(numNodes))
}
for _ in 0..<numRcurrentWeights {
U.append(Gaussian.gaussianRandom(0.0, standardDeviation : 1.0) * weightDiviser)
}
}
// If rmsprop update, allocate the momentum storage array
if (weightUpdateMethod == .rmsProp) {
WweightUpdateData = [Double](repeating: 0.0, count: numWeights)
UweightUpdateData = [Double](repeating: 0.0, count: numRcurrentWeights)
}
}
func getWeights() -> [Double]
{
var weights = W
weights += U
return weights
}
func setNeuralWeightUpdateMethod(_ method: NeuralWeightUpdateMethod, _ parameter: Double?)
{
weightUpdateMethod = method
weightUpdateParameter = parameter
}
func getLastOutput() -> [Double]
{
return h
}
func getNodeCount() -> Int
{
return numNodes
}
func getWeightsPerNode()-> Int
{
return numInputs + numNodes + 1
}
func getActivation()-> NeuralActivationFunction
{
return activation
}
func feedForward(_ x: [Double]) -> [Double]
{
// Gather the previous outputs for the feedback
var hPrev : [Double] = []
if let temp = outputHistory.last {
hPrev = temp
}
else {
hPrev = [Double](repeating: 0.0, count: numNodes)
}
var z = [Double](repeating: 0.0, count: numNodes)
var uz = [Double](repeating: 0.0, count: numNodes)
// Assume input array already has bias constant 1.0 appended
// Fully-connected nodes means all nodes get the same input array
vDSP_mmulD(W, 1, x, 1, &z, 1, vDSP_Length(numNodes), 1, vDSP_Length(numInputs+1))
vDSP_mmulD(U, 1, hPrev, 1, &uz, 1, vDSP_Length(numNodes), 1, vDSP_Length(numNodes))
vDSP_vaddD(z, 1, uz, 1, &z, 1, vDSP_Length(numNodes))
// Run through the non-linearity
var sum = 0.0
for node in 0..<numNodes {
switch (activation) {
case .none:
h[node] = z[node]
break
case .hyperbolicTangent:
h[node] = tanh(z[node])
break
case .sigmoidWithCrossEntropy:
h[node] = 1.0 / (1.0 + exp(-z[node]))
sum += h[node]
break
case .sigmoid:
h[node] = 1.0 / (1.0 + exp(-z[node]))
break
case .rectifiedLinear:
h[node] = z[node]
if (z[node] < 0) { h[node] = 0.0 }
break
case .softSign:
h[node] = z[node] / (1.0 + abs(z[node]))
break
case .softMax:
h[node] = exp(z[node])
break
}
}
if (activation == .softMax) {
var scale = 1.0 / sum // Do division once for efficiency
vDSP_vsmulD(h, 1, &scale, &h, 1, vDSP_Length(numNodes))
}
return h
}
func getFinalLayer𝟃E𝟃zs(_ 𝟃E𝟃h: [Double])
{
// Calculate 𝟃E/𝟃z from 𝟃E/𝟃h
switch (activation) {
case .none:
𝟃E𝟃z = 𝟃E𝟃h
break
case .hyperbolicTangent:
vDSP_vsqD(h, 1, &𝟃E𝟃z, 1, vDSP_Length(numNodes)) // h²
let ones = [Double](repeating: 1.0, count: numNodes)
vDSP_vsubD(𝟃E𝟃z, 1, ones, 1, &𝟃E𝟃z, 1, vDSP_Length(numNodes)) // 1 - h²
vDSP_vmulD(𝟃E𝟃z, 1, 𝟃E𝟃h, 1, &𝟃E𝟃z, 1, vDSP_Length(numNodes)) // 𝟃E𝟃h * (1 - h²)
break
case .sigmoidWithCrossEntropy:
fallthrough
case .sigmoid:
vDSP_vsqD(h, 1, &𝟃E𝟃z, 1, vDSP_Length(numNodes)) // h²
vDSP_vsubD(𝟃E𝟃z, 1, h, 1, &𝟃E𝟃z, 1, vDSP_Length(numNodes)) // h - h²
vDSP_vmulD(𝟃E𝟃z, 1, 𝟃E𝟃h, 1, &𝟃E𝟃z, 1, vDSP_Length(numNodes)) // 𝟃E𝟃h * (h - h²)
break
case .rectifiedLinear:
for i in 0..<numNodes {
𝟃E𝟃z[i] = h[i] <= 0.0 ? 0.0 : 𝟃E𝟃h[i]
}
break
case .softSign:
for i in 0..<numNodes {
// Reconstitute z from h
var z : Double
//!! - this might be able to be sped up with vector operations
if (h[i] < 0) { // Negative z
z = h[i] / (1.0 + h[i])
𝟃E𝟃z[i] = -𝟃E𝟃h[i] / ((1.0 + z) * (1.0 + z))
}
else { // Positive z
z = h[i] / (1.0 - h[i])
𝟃E𝟃z[i] = 𝟃E𝟃h[i] / ((1.0 + z) * (1.0 + z))
}
}
break
case .softMax:
// This should be done outside of the layer
break
}
}
func getLayer𝟃E𝟃zs(_ nextLayer: NeuralLayer)
{
// Get 𝟃E/𝟃h from the next layer
var 𝟃E𝟃h = [Double](repeating: 0.0, count: numNodes)
for node in 0..<numNodes {
𝟃E𝟃h[node] = nextLayer.get𝟃E𝟃hForNodeInPreviousLayer(node)
}
// Calculate 𝟃E/𝟃z from 𝟃E/𝟃h
getFinalLayer𝟃E𝟃zs(𝟃E𝟃h)
}
func get𝟃E𝟃hForNodeInPreviousLayer(_ inputIndex: Int) ->Double
{
var sum = 0.0
var offset = inputIndex
for node in 0..<numNodes {
sum += 𝟃E𝟃z[node] * W[offset]
offset += numInputs+1
}
return sum
}
func clearWeightChanges()
{
𝟃E𝟃W = [Double](repeating: 0.0, count: W.count)
𝟃E𝟃U = [Double](repeating: 0.0, count: U.count)
}
func appendWeightChanges(_ x: [Double]) -> [Double]
{
// Gather the previous outputs for the feedback
var hPrev : [Double] = []
if let temp = outputHistory.last {
hPrev = temp
}
else {
hPrev = [Double](repeating: 0.0, count: numNodes)
}
// Assume input array already has bias constant 1.0 appended
// Update each weight accumulation
var weightWChange = [Double](repeating: 0.0, count: W.count)
vDSP_mmulD(𝟃E𝟃z, 1, x, 1, &weightWChange, 1, vDSP_Length(numNodes), vDSP_Length(numInputs+1), 1)
vDSP_vaddD(weightWChange, 1, 𝟃E𝟃W, 1, &𝟃E𝟃W, 1, vDSP_Length(W.count))
var weightUChange = [Double](repeating: 0.0, count: U.count)
vDSP_mmulD(𝟃E𝟃z, 1, hPrev, 1, &weightUChange, 1, vDSP_Length(numNodes), vDSP_Length(numNodes), 1)
vDSP_vaddD(weightUChange, 1, 𝟃E𝟃U, 1, &𝟃E𝟃U, 1, vDSP_Length(U.count))
return h
}
func updateWeightsFromAccumulations(_ averageTrainingRate: Double, weightDecay: Double)
{
// Decay weights if indicated
if (weightDecay < 1) { decayWeights(weightDecay) }
// Update the weights from the accumulations
switch weightUpdateMethod {
case .normal:
// W -= 𝟃E/𝟃W * averageTrainingRate, U -= 𝟃E/𝟃U * averageTrainingRate
var η = -averageTrainingRate // Needed for unsafe pointer conversion - negate for multiply-and-add vector operation
vDSP_vsmaD(𝟃E𝟃W, 1, &η, W, 1, &W, 1, vDSP_Length(W.count))
vDSP_vsmaD(𝟃E𝟃U, 1, &η, U, 1, &U, 1, vDSP_Length(U.count))
case .rmsProp:
// Update the rmsProp cache for W --> rmsprop_cache = decay_rate * rmsprop_cache + (1 - decay_rate) * gradient²
let numWeights = W.count
var gradSquared = [Double](repeating: 0.0, count: numWeights)
vDSP_vsqD(𝟃E𝟃W, 1, &gradSquared, 1, vDSP_Length(numWeights)) // Get the gradient squared
var decay = 1.0 - weightUpdateParameter!
vDSP_vsmulD(gradSquared, 1, &decay, &gradSquared, 1, vDSP_Length(numWeights)) // (1 - decay_rate) * gradient²
decay = weightUpdateParameter!
vDSP_vsmaD(WweightUpdateData, 1, &decay, gradSquared, 1, &WweightUpdateData, 1, vDSP_Length(numWeights))
// Update the weights --> weight += learning_rate * gradient / (sqrt(rmsprop_cache) + 1e-5)
for i in 0..<numWeights { gradSquared[i] = sqrt(WweightUpdateData[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(numWeights)) // (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(numWeights))
vDSP_vmaD(𝟃E𝟃W, 1, gradSquared, 1, W, 1, &W, 1, vDSP_Length(numWeights))
// Update the rmsProp cache for U --> rmsprop_cache = decay_rate * rmsprop_cache + (1 - decay_rate) * gradient²
let numFeedback = U.count
gradSquared = [Double](repeating: 0.0, count: numFeedback)
vDSP_vsqD(𝟃E𝟃U, 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(UweightUpdateData, 1, &decay, gradSquared, 1, &UweightUpdateData, 1, vDSP_Length(numFeedback))
// Update the weights --> weight += learning_rate * gradient / (sqrt(rmsprop_cache) + 1e-5)
for i in 0..<numFeedback { gradSquared[i] = sqrt(UweightUpdateData[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𝟃U, 1, gradSquared, 1, U, 1, &U, 1, vDSP_Length(numFeedback))
}
}
func decayWeights(_ decayFactor : Double)
{
var decay = decayFactor
vDSP_vsmulD(W, 1, &decay, &W, 1, vDSP_Length(W.count))
vDSP_vsmulD(U, 1, &decay, &U, 1, vDSP_Length(U.count))
}
func getSingleNodeClassifyValue() -> Double
{
if (activation == .hyperbolicTangent || activation == .rectifiedLinear) { return 0.0 }