-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcNeuralNet.cls
2586 lines (2141 loc) · 78.9 KB
/
cNeuralNet.cls
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
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "cNeuralNet"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
'====================================================================================
'2023-04-25
'This is a general purpose feed-forward neutral network
'Structure of the network needs to be initialized manually with Init() and AddLayer()
'The two main methods to fit data are Fit() and FitCV()
'either ADAM or RMSProp can be used to speed up convergence
'networks can be printed to a worksheet using PrintNetwork(), and read by ReadNetwork()
'A few frequently used functions like Label2Dummy(), Dummy2Label() and SampleSoftmax()
'are also included
'2023-05-14 Add support for allowing no hidden layer
'======================================================================================
Private pOutputType As String
Private pn_input As Long, pn_output As Long, pn_unit As Long
Private pn_layer As Long, pn_hidden() As Long, pLayerType() As String
Private vWgts() As Variant, vBias As Variant
Private pWOut() As Double, pbOut() As Double
Private vdWgts() As Variant, vdBias As Variant
Private dWOut() As Double, dbOut() As Double
Private vWgts_tmp() As Variant, vBias_tmp As Variant
Private pWOut_tmp() As Double, pbOut_tmp() As Double
Private vdW2() As Variant, vdB2() As Variant
Private dWOut2() As Double, dbOut2() As Double
Private vdW1() As Variant, vdB1() As Variant
Private dWOut1() As Double, dbOut1() As Double
Private vh_out() As Variant, px_in() As Double, py_out() As Double
Private pn_row As Long, pn_hist As Long
Private pTrainProgress() As Double
Private pADAM_count As Long, pRMS_count As Long
Private visDrop() As Variant
Property Get OutputType() As String
OutputType = pOutputType
End Property
Property Get n_input() As Long
n_input = pn_input
End Property
Property Get n_output() As Long
n_output = pn_output
End Property
Property Get n_unit() As Long
n_unit = pn_unit
End Property
Property Get n_layer() As Long
n_layer = pn_layer
End Property
Property Get n_row() As Long
n_row = pn_row
End Property
Property Get n_hist() As Long
n_hist = pn_hist
End Property
Property Get WgtHidden()
WgtHidden = vWgts
End Property
Property Get BiasHidden()
BiasHidden = vBias
End Property
Property Get WOut()
WOut = pWOut
End Property
Property Get bOut()
bOut = pbOut
End Property
Property Get n_hidden()
n_hidden = pn_hidden
End Property
Property Get LayerType()
LayerType = pLayerType
End Property
Property Get Hist_x_in() As Double()
Hist_x_in = px_in
End Property
Property Get TrainProgress()
TrainProgress = pTrainProgress
End Property
'Clone properties from another network
Sub Clone(cNN As cNeuralNet)
pOutputType = cNN.OutputType
pn_input = cNN.n_input
pn_output = cNN.n_output
pn_unit = cNN.n_unit
pn_layer = cNN.n_layer
If pn_layer > 0 Then
pn_hidden = cNN.n_hidden
pLayerType = cNN.LayerType
vWgts = cNN.WgtHidden
vBias = cNN.BiasHidden
End If
pWOut = cNN.WOut
pbOut = cNN.bOut
End Sub
Sub RMSProp_Clear()
pRMS_count = 0
Erase dWOut2, dbOut2
If pn_layer > 0 Then
Erase vdW2, vdB2
End If
End Sub
Private Sub RMSProp_CalcRMS()
Dim i As Long, j As Long, k As Long, m As Long, n As Long, iterate As Long
Dim n_input As Long, n_output As Long
Dim dWgt() As Double, dBias() As Double
Dim dW2() As Double, db2() As Double
Dim tmp_x As Double
If pRMS_count = 0 Then
ReDim dWOut2(1 To pn_output, 1 To UBound(pWOut, 2))
ReDim dbOut2(1 To pn_output)
If pn_layer > 0 Then
ReDim vdW2(1 To pn_layer)
ReDim vdB2(1 To pn_layer)
For iterate = 1 To pn_layer
If iterate = 1 Then
n_input = pn_input
Else
n_input = pn_hidden(iterate - 1)
End If
n_output = pn_hidden(iterate)
ReDim dW2(1 To n_output, 1 To n_input)
ReDim db2(1 To n_output)
vdW2(iterate) = dW2
vdB2(iterate) = db2
Next iterate
End If
End If
For iterate = 1 To pn_layer
If pLayerType(iterate) <> "DROPOUT" Then
dWgt = vdWgts(iterate)
dBias = vdBias(iterate)
n_output = UBound(dBias, 1)
n_input = UBound(dWgt, 2)
If pRMS_count = 0 Then
ReDim dW2(1 To n_output, 1 To n_input)
ReDim db2(1 To n_output)
For j = 1 To n_input
For i = 1 To n_output
dW2(i, j) = dWgt(i, j) ^ 2
Next i
Next j
For i = 1 To n_output
db2(i) = dBias(i) ^ 2
Next i
Else
dW2 = vdW2(iterate)
db2 = vdB2(iterate)
For j = 1 To n_input
For i = 1 To n_output
dW2(i, j) = 0.9 * dW2(i, j) + 0.1 * dWgt(i, j) ^ 2
Next i
Next j
For i = 1 To n_output
db2(i) = 0.9 * db2(i) + 0.1 * dBias(i) ^ 2
Next i
End If
vdW2(iterate) = dW2
vdB2(iterate) = db2
End If
Next iterate
If pRMS_count = 0 Then
For j = 1 To UBound(dWOut, 2)
For i = 1 To pn_output
dWOut2(i, j) = dWOut(i, j) ^ 2
Next i
Next j
For i = 1 To pn_output
dbOut2(i) = dbOut(i) ^ 2
Next i
Else
For j = 1 To UBound(dWOut, 2)
For i = 1 To pn_output
dWOut2(i, j) = 0.9 * dWOut2(i, j) + 0.1 * dWOut(i, j) ^ 2
Next i
Next j
For i = 1 To pn_output
dbOut2(i) = 0.9 * dbOut2(i) + 0.1 * dbOut(i) ^ 2
Next i
End If
pRMS_count = pRMS_count + 1
End Sub
'Clear and reset memories used in ADAM
Sub ADAM_Clear()
pADAM_count = 0
If pn_layer > 0 Then
Erase vdW1, vdB1
Erase vdW2, vdB2
End If
Erase dWOut1, dbOut1
Erase dWOut2, dbOut2
End Sub
Private Sub ADAM_Init()
Dim i As Long, j As Long, k As Long, m As Long, n As Long, iterate As Long
Dim n_input As Long, n_output As Long
Dim dWgt() As Double, dBias() As Double
Dim dW2() As Double, db2() As Double
Dim dW1() As Double, db1() As Double
pADAM_count = 0
ReDim dWOut1(1 To pn_output, 1 To UBound(pWOut, 2))
ReDim dbOut1(1 To pn_output)
ReDim dWOut2(1 To pn_output, 1 To UBound(pWOut, 2))
ReDim dbOut2(1 To pn_output)
If pn_layer > 0 Then
ReDim vdW1(1 To pn_layer)
ReDim vdB1(1 To pn_layer)
ReDim vdW2(1 To pn_layer)
ReDim vdB2(1 To pn_layer)
For iterate = 1 To pn_layer
dW1 = vWgts(iterate)
n_output = UBound(dW1, 1)
n_input = UBound(dW1, 2)
ReDim dW1(1 To n_output, 1 To n_input)
ReDim db1(1 To n_output)
ReDim dW2(1 To n_output, 1 To n_input)
ReDim db2(1 To n_output)
vdW1(iterate) = dW1
vdB1(iterate) = db1
vdW2(iterate) = dW2
vdB2(iterate) = db2
Next iterate
End If
End Sub
Private Sub ADAM_CalcMoment(Optional isFirst As Boolean = False)
Dim i As Long, j As Long, k As Long, m As Long, n As Long, iterate As Long
Dim n_input As Long, n_output As Long
Dim dWgt() As Double, dBias() As Double
Dim dW2() As Double, db2() As Double
Dim dW1() As Double, db1() As Double
Dim tmp_x As Double
If pADAM_count = 0 Then Call ADAM_Init
pADAM_count = pADAM_count + 1
For iterate = 1 To pn_layer
dWgt = vdWgts(iterate)
dBias = vdBias(iterate)
n_output = UBound(dWgt, 1)
n_input = UBound(dWgt, 2)
If pLayerType(iterate) <> "DROPOUT" Then
dW1 = vdW1(iterate)
db1 = vdB1(iterate)
dW2 = vdW2(iterate)
db2 = vdB2(iterate)
For j = 1 To n_input
For i = 1 To n_output
dW1(i, j) = 0.9 * dW1(i, j) + 0.1 * dWgt(i, j)
dW2(i, j) = 0.999 * dW2(i, j) + 0.001 * dWgt(i, j) ^ 2
Next i
Next j
For i = 1 To n_output
db1(i) = 0.9 * db1(i) + 0.1 * dBias(i)
db2(i) = 0.999 * db2(i) + 0.001 * dBias(i) ^ 2
Next i
vdW1(iterate) = dW1
vdB1(iterate) = db1
vdW2(iterate) = dW2
vdB2(iterate) = db2
End If
Next iterate
n_input = UBound(dWOut, 2)
For j = 1 To n_input
For i = 1 To pn_output
dWOut1(i, j) = 0.9 * dWOut1(i, j) + 0.1 * dWOut(i, j)
dWOut2(i, j) = 0.999 * dWOut2(i, j) + 0.001 * dWOut(i, j) ^ 2
Next i
Next j
For i = 1 To pn_output
dbOut1(i) = 0.9 * dbOut1(i) + 0.1 * dbOut(i)
dbOut2(i) = 0.999 * dbOut2(i) + 0.001 * dbOut(i) ^ 2
Next i
End Sub
'Cache current weights
Sub CacheCurrentWgt()
If pn_layer > 0 Then
vWgts_tmp = vWgts
vBias_tmp = vBias
End If
pWOut_tmp = pWOut
pbOut_tmp = pbOut
End Sub
'Clear cache weights
Sub ClearCacheWgt()
If pn_layer > 0 Then
Erase vWgts_tmp, vBias_tmp
End If
Erase pWOut_tmp, pbOut_tmp
End Sub
'Restore weights to cached values
Sub RestoreWgt()
If pn_layer > 0 Then
vWgts = vWgts_tmp
vBias = vBias_tmp
End If
pWOut = pWOut_tmp
pbOut = pbOut_tmp
End Sub
'clear intermediate outputs from memory
Sub ClearHist()
pn_hist = 0
If pn_layer > 0 Then
Erase vh_out, visDrop
End If
Erase px_in
End Sub
'Reset gradients
Sub ResetWgtChg()
If pn_layer > 0 Then
Erase vdWgts, vdBias
End If
Erase dWOut, dbOut
End Sub
Sub InitWgtChg()
Dim i As Long, j As Long, k As Long, m As Long, n As Long
Dim iterate As Long, n_hidden As Long
Dim xW() As Double, xb() As Double
ReDim dWOut(1 To pn_output, 1 To UBound(pWOut, 2))
ReDim dbOut(1 To pn_output)
If pn_layer > 0 Then
ReDim vdWgts(1 To pn_layer)
ReDim vdBias(1 To pn_layer)
m = pn_input
For iterate = 1 To pn_layer
n_hidden = pn_hidden(iterate)
ReDim xW(1 To n_hidden, 1 To m)
ReDim xb(1 To n_hidden)
vdWgts(iterate) = xW
vdBias(iterate) = xb
m = n_hidden
Next iterate
End If
End Sub
'Initialize with one input layer and one output layer of specified activation function
Sub Init(n_input As Long, n_output As Long, Optional strOutputType As String = "SIGMOID")
Dim i As Long, j As Long
Dim tmp_x As Double
pn_input = n_input
pn_output = n_output
pn_layer = 0
pOutputType = UCase(Trim(strOutputType))
If pn_output = 1 And pOutputType = "SOFTMAX" Then
Debug.Print "cNeuralNet: Init: when output is binary, use SIGMOID instead of SOFTMAX"
End
End If
VBA.Randomize
tmp_x = Sqr(2 / pn_input)
ReDim pWOut(1 To pn_output, 1 To pn_input)
ReDim pbOut(1 To pn_output)
For i = 1 To pn_output
pbOut(i) = (-0.1 + Rnd() * 0.2) * tmp_x
For j = 1 To pn_input
pWOut(i, j) = (-0.5 + Rnd()) * tmp_x
Next j
Next i
pn_unit = pn_output * (pn_input + 1)
pn_hist = 0
pADAM_count = 0
pRMS_count = 0
End Sub
'Insert a hidden layer to current network, layer is inserted between the last hidden layer and the output layer
Sub AddLayer(n_hidden As Long, Optional strType As String = "SIGMOID", Optional DropOut As Double = 0)
Dim i As Long, j As Long, k As Long, m As Long, n As Long
Dim xW() As Double, xb() As Double
Dim tmp_x As Double
pn_layer = pn_layer + 1
If pn_layer = 1 Then
ReDim vWgts(1 To pn_layer)
ReDim vBias(1 To pn_layer)
ReDim pn_hidden(1 To pn_layer)
ReDim pLayerType(1 To pn_layer)
m = pn_input
Else
ReDim Preserve vWgts(1 To pn_layer)
ReDim Preserve vBias(1 To pn_layer)
ReDim Preserve pn_hidden(1 To pn_layer)
ReDim Preserve pLayerType(1 To pn_layer)
m = UBound(vBias(pn_layer - 1), 1)
End If
If UCase(Trim(strType)) = "DROPOUT" Then
'for dropout layer, use first element
'of vBias to save dropout rate
pn_hidden(pn_layer) = m
pLayerType(pn_layer) = UCase(Trim(strType))
ReDim xb(1 To m)
ReDim xW(1 To m, 1 To m)
For i = 1 To m
xW(i, i) = 1
Next i
xb(1) = DropOut
n = m
Else
tmp_x = Sqr(2 / m)
pn_hidden(pn_layer) = n_hidden
pLayerType(pn_layer) = UCase(Trim(strType))
ReDim xW(1 To n_hidden, 1 To m)
ReDim xb(1 To n_hidden)
For i = 1 To n_hidden
xb(i) = 0
For j = 1 To m
xW(i, j) = (-0.5 + Rnd()) * tmp_x
Next j
Next i
n = n_hidden
End If
vWgts(pn_layer) = xW
vBias(pn_layer) = xb
'Reconnect output layer to new hidden layer
'reset weights if sizes are not compatible
If n <> UBound(pWOut, 2) Then
tmp_x = Sqr(2 / n)
ReDim pWOut(1 To pn_output, 1 To n)
For i = 1 To pn_output
pbOut(i) = 0
For j = 1 To n
pWOut(i, j) = (-0.5 + Rnd()) * tmp_x
Next j
Next i
End If
pn_unit = pn_output * (n + 1)
For i = 1 To pn_layer
If pLayerType(i) <> "DROPOUT" Then
If i > 1 Then
pn_unit = pn_unit + pn_hidden(i) * (pn_hidden(i - 1) + 1)
Else
pn_unit = pn_unit + pn_hidden(i) * (pn_input + 1)
End If
End If
Next i
End Sub
'Forward pass with input x(1:pn_input, 1:n)
'Returns double array y() of dimension y(1:pn_output, 1:n), where n is same as length of x
'If storeOutput is set to True, intermediate activation and input x is stored
Function FwdPass(x, Optional storeOutput As Boolean = False)
Dim i As Long, j As Long, k As Long, m As Long, n As Long, n_len As Long, n_input As Long, n_hidden As Long
Dim iterate As Long
Dim xW() As Double, xb() As Double
Dim xin As Variant
Dim h() As Double, h_tmp() As Double
Dim v As Variant
Dim strType As String
Dim y() As Double
Dim n_hist_prv As Long
Dim isDrop() As Boolean, n_drop As Long, tmp_x As Double, tmp_y As Double
Dim boolean_tmp() As Boolean
n_len = UBound(x, 2)
n_input = UBound(x, 1)
If n_input <> pn_input Then
Debug.Print "cNeuralNet: FwdPass: input size incorrect. n_input=" & n_input & ", pn_input=" & pn_input
End
End If
n_hist_prv = pn_hist
If storeOutput Then pn_hist = n_hist_prv + n_len
'store output of each hidden layer
If storeOutput And n_hist_prv = 0 And pn_layer > 0 Then
ReDim vh_out(1 To pn_layer)
ReDim visDrop(1 To pn_layer)
End If
ReDim xin(1 To n_input, 1 To n_len)
For k = 1 To n_len
For i = 1 To n_input
xin(i, k) = x(i, k)
Next i
Next k
'store input to be used later in back propagation
If storeOutput Then
If n_hist_prv = 0 Then
ReDim px_in(1 To n_input, 1 To n_len)
For k = 1 To n_len
For i = 1 To n_input
px_in(i, k) = xin(i, k)
Next i
Next k
Else
ReDim Preserve px_in(1 To n_input, 1 To n_hist_prv + n_len)
For k = 1 To n_len
For i = 1 To n_input
px_in(i, n_hist_prv + k) = xin(i, k)
Next i
Next k
End If
End If
For iterate = 1 To pn_layer
xW = vWgts(iterate)
xb = vBias(iterate)
n_hidden = pn_hidden(iterate)
strType = pLayerType(iterate)
ReDim h(1 To n_hidden, 1 To n_len)
If strType = "DROPOUT" Then
'for dropout layer, only apply dropout
'during training, i.e. when storeOutput=True
'remember which instances are dropped for
'later use in backpropagation
If storeOutput And xb(1) > 0 Then
ReDim isDrop(1 To n_hidden, 1 To n_len)
tmp_x = 1 / (1 - xb(1))
For k = 1 To n_len
For i = 1 To n_hidden
If Rnd() >= xb(1) Then
h(i, k) = xin(i, k) * tmp_x
Else
isDrop(i, k) = True
End If
Next i
Next k
If n_hist_prv = 0 Then
visDrop(iterate) = isDrop
Else
boolean_tmp = visDrop(iterate)
ReDim Preserve boolean_tmp(1 To n_hidden, 1 To n_hist_prv + n_len)
For k = 1 To n_len
For i = 1 To n_hidden
boolean_tmp(i, n_hist_prv + k) = isDrop(i, k)
Next i
Next k
visDrop(iterate) = boolean_tmp
End If
Else
h = xin
End If
Else
v = WorksheetFunction.MMult(xW, xin)
If n_hidden = 1 Then
For k = 1 To n_len
h(1, k) = f_Activate(v(k) + xb(1), strType)
Next k
Else
For k = 1 To n_len
For i = 1 To n_hidden
h(i, k) = f_Activate(v(i, k) + xb(i), strType)
Next i
Next k
End If
' ReDim h(1 To n_hidden, 1 To n_len)
' For k = 1 To n_len
' For i = 1 To pn_hidden
' tmp_x = xb(i)
' For j = 1 To n_input
' tmp_x = tmp_x + xW(i, j) * x(j, k)
' Next j
' h(i, k) = f_Activate(tmp_x, strType)
' Next i
' Next k
End If
'store output for gradient calculation
If storeOutput And n_hist_prv = 0 Then
vh_out(iterate) = h
ElseIf storeOutput And n_hist_prv > 0 Then
h_tmp = vh_out(iterate)
ReDim Preserve h_tmp(1 To n_hidden, 1 To n_hist_prv + n_len)
For k = 1 To n_len
For i = 1 To n_hidden
h_tmp(i, n_hist_prv + k) = h(i, k)
Next i
Next k
vh_out(iterate) = h_tmp
End If
'Assign output as input to next layer
xin = h
n_input = n_hidden
Next iterate
'Output layer
ReDim y(1 To pn_output, 1 To n_len)
v = WorksheetFunction.MMult(pWOut, xin)
If pOutputType = "SOFTMAX" Then
ReDim h(1 To pn_output)
For k = 1 To n_len
For i = 1 To pn_output
h(i) = v(i, k) + pbOut(i)
Next i
h = f_Activate(h, "SOFTMAX")
For i = 1 To pn_output
y(i, k) = h(i)
Next i
Next k
Else
If pn_output = 1 Then
For k = 1 To n_len
y(1, k) = f_Activate(v(k) + pbOut(1), pOutputType)
Next k
Else
For k = 1 To n_len
For i = 1 To pn_output
y(i, k) = f_Activate(v(i, k) + pbOut(i), pOutputType)
Next i
Next k
End If
End If
FwdPass = y
If storeOutput And n_hist_prv = 0 Then
py_out = y
ElseIf storeOutput And n_hist_prv > 0 Then
ReDim Preserve py_out(1 To pn_output, 1 To n_hist_prv + n_len)
For k = 1 To n_len
For i = 1 To pn_output
py_out(i, n_hist_prv + k) = y(i, k)
Next i
Next k
End If
End Function
'Calculate and accumulate gradients with respect to all weights
'gradients are stored in memory
'returns dE/dx which can be passed to classes that fed inputs to it
Function Backward(grad_out, Optional isdEdx As Boolean = False, Optional y_out, Optional y_tgt As Variant)
Dim i As Long, j As Long, k As Long, m As Long, n As Long
Dim n_output As Long, n_hidden As Long, n_len As Long, n_input As Long
Dim iterate As Long
Dim xW() As Double, xb() As Double, dW() As Double, dBias() As Double
Dim y() As Double
Dim h() As Double
Dim v As Variant, x As Variant, z() As Double
Dim strType As String
Dim grad_curr As Variant, grad_nxt As Variant, grad_loc As Variant
Dim n_hidden_nxt As Long, layerType_nxt As String
Dim tmp_x As Double, isDrop() As Boolean, drop_prob As Double
n_len = UBound(grad_out, 2)
n_output = UBound(grad_out, 1)
If n_output <> pn_output Then
Debug.Print "cNeuralNet: Backward: output size incorrect."
End
End If
'Placeholder to store changes in weights
Call InitWgtChg
ReDim grad_curr(1 To n_output, 1 To n_len)
If isdEdx Then
'dEdx is supplied instead of dEdy
For k = 1 To n_len
For i = 1 To n_output
grad_curr(i, k) = grad_out(i, k)
Next i
Next k
Else
grad_loc = f_LocalGrad(py_out, pOutputType)
For k = 1 To n_len
For i = 1 To n_output
grad_curr(i, k) = grad_out(i, k) * grad_loc(i, k)
Next i
Next k
End If
If pn_layer = 0 Then
m = pn_input
x = px_in
Else
m = pn_hidden(pn_layer)
x = vh_out(pn_layer)
End If
For k = n_len To 1 Step -1
For i = 1 To pn_output
dbOut(i) = dbOut(i) + grad_curr(i, k)
For j = 1 To m
dWOut(i, j) = dWOut(i, j) + grad_curr(i, k) * x(j, k)
Next j
Next i
Next k
layerType_nxt = ""
grad_nxt = grad_curr
n_hidden_nxt = pn_output
For iterate = pn_layer To 1 Step -1
n_hidden = pn_hidden(iterate)
strType = pLayerType(iterate)
If strType <> "DROPOUT" Then
'local gradient
grad_loc = f_LocalGrad(vh_out(iterate), strType)
'Wgts connected to next layer
drop_prob = 0
If layerType_nxt = "DROPOUT" Then
drop_prob = vBias(iterate + 1)(1)
If iterate = pn_layer - 1 Then
xW = pWOut
Else
xW = vWgts(iterate + 2)
End If
Else
If iterate = pn_layer Then
xW = pWOut
Else
xW = vWgts(iterate + 1)
End If
End If
'Input to current layer
If iterate = 1 Then
n_input = pn_input
x = px_in
Else
n_input = pn_hidden(iterate - 1)
x = vh_out(iterate - 1)
End If
dW = vdWgts(iterate)
dBias = vdBias(iterate)
'Wgt sum of next layer's gradient
ReDim grad_curr(1 To n_hidden, 1 To n_len)
If layerType_nxt = "DROPOUT" And drop_prob > 0 Then
isDrop = visDrop(iterate + 1)
tmp_x = 1 / (1 - drop_prob)
v = wkshtMMult(wkshtTranspose(xW), grad_nxt)
For k = n_len To 1 Step -1
For i = 1 To n_hidden
If Not isDrop(i, k) Then
grad_curr(i, k) = grad_curr(i, k) + v(i, k) * tmp_x * grad_loc(i, k)
Else
grad_curr(i, k) = 0
End If
Next i
Next k
' For k = n_len To 1 Step -1
' For i = 1 To n_hidden
' If Not isDrop(i, k) Then
' For j = 1 To n_hidden_nxt
' grad_curr(i, k) = grad_curr(i, k) + grad_nxt(j, k) * xW(j, i) * tmp_x * grad_loc(i, k)
' Next j
' Else
' grad_curr(i, k) = 0
' End If
' Next i
' Next k
Else
v = wkshtMMult(wkshtTranspose(xW), grad_nxt)
For k = n_len To 1 Step -1
For i = 1 To n_hidden
grad_curr(i, k) = grad_curr(i, k) + v(i, k) * grad_loc(i, k)
Next i
Next k
' For k = n_len To 1 Step -1
' For i = 1 To n_hidden
' For j = 1 To n_hidden_nxt
' grad_curr(i, k) = grad_curr(i, k) + grad_nxt(j, k) * xW(j, i) * grad_loc(i, k)
' Next j
' Next i
' Next k
End If
'Accumulate gradients
v = wkshtMMult(x, wkshtTranspose(grad_curr))
For i = 1 To n_hidden
For j = 1 To n_input
dW(i, j) = dW(i, j) + v(j, i)
Next j
Next i
For k = n_len To 1 Step -1
For i = 1 To n_hidden
dBias(i) = dBias(i) + grad_curr(i, k)
' For j = 1 To n_input
' dW(i, j) = dW(i, j) + grad_curr(i, k) * x(j, k)
' Next j
Next i
Next k
vdWgts(iterate) = dW
vdBias(iterate) = dBias
'pass gradient to previous layer
grad_nxt = grad_curr
n_hidden_nxt = n_hidden
End If
layerType_nxt = strType
Next iterate
'On exit, calculate dL/dx to pass to previous networks
If pn_layer = 0 Then
xW = pWOut
m = pn_output
Else
xW = vWgts(1)
m = n_hidden
End If
ReDim grad_curr(1 To pn_input, 1 To n_len)
For k = 1 To n_len
For i = 1 To pn_input
For j = 1 To m
grad_curr(i, k) = grad_curr(i, k) + grad_nxt(j, k) * xW(j, i)
Next j
Next i
Next k
Backward = grad_curr
End Function
'Applied stored gradients to update weights
'stored gradients are erased once used
Sub UpdateWgt(learn_rate As Double, Optional useSpeedUp As String = "")
Dim i As Long, j As Long, k As Long, m As Long, n As Long, n_len As Long
Dim iterate As Long, n_hidden As Long
Dim xW() As Double, xb() As Double
Dim dW() As Double, db() As Double
Dim dW1() As Double, db1() As Double
Dim dW2() As Double, db2() As Double
Dim adam_discount1 As Double, adam_discount2 As Double
If UCase(useSpeedUp) = "RMS" Then
Call RMSProp_CalcRMS
m = UBound(pWOut, 2)
For i = 1 To pn_output
pbOut(i) = pbOut(i) - learn_rate * dbOut(i) / (Sqr(dbOut2(i)) + 0.00000001)
Next i
For j = 1 To m
For i = 1 To pn_output
pWOut(i, j) = pWOut(i, j) - learn_rate * dWOut(i, j) / (Sqr(dWOut2(i, j)) + 0.00000001)
Next i
Next j
For iterate = 1 To pn_layer
If pLayerType(iterate) <> "DROPOUT" Then
xW = vWgts(iterate)
xb = vBias(iterate)
dW = vdWgts(iterate)
db = vdBias(iterate)
dW2 = vdW2(iterate)
db2 = vdB2(iterate)
n = UBound(xW, 1)
m = UBound(xW, 2)
For i = 1 To n
xb(i) = xb(i) - learn_rate * db(i) / (Sqr(db2(i)) + 0.00000001)
Next i
For j = 1 To m
For i = 1 To n
xW(i, j) = xW(i, j) - learn_rate * dW(i, j) / (Sqr(dW2(i, j)) + 0.00000001)
Next i
Next j
vWgts(iterate) = xW
vBias(iterate) = xb
End If
Next iterate
ElseIf UCase(useSpeedUp) = "ADAM" Then
Call ADAM_CalcMoment
adam_discount1 = 1 / (1 - 0.9 ^ pADAM_count)
adam_discount2 = 1 / (1 - 0.999 ^ pADAM_count)
m = UBound(pWOut, 2)
For i = 1 To pn_output
pbOut(i) = pbOut(i) - learn_rate * dbOut1(i) * adam_discount1 / (Sqr(dbOut2(i) * adam_discount2) + 0.00000001)
Next i
For j = 1 To m
For i = 1 To pn_output
pWOut(i, j) = pWOut(i, j) - learn_rate * dWOut1(i, j) * adam_discount1 / (Sqr(dWOut2(i, j) * adam_discount2) + 0.00000001)
Next i