-
Notifications
You must be signed in to change notification settings - Fork 8
/
nn.cpp
1011 lines (888 loc) · 22.3 KB
/
nn.cpp
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
/**************************************************************************
* copyright : (C) 2004-2006 by Petr Schwarz & Pavel Matejka *
* UPGM,FIT,VUT,Brno *
* email : {schwarzp,matejkap}@fit.vutbr.cz *
**************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
**************************************************************************/
#include <stdio.h>
#include "nn.h"
#include "fexp.h"
#ifdef USE_BLAS
extern "C"
{
#include "cblas.h"
}
#endif
#ifdef WIN32
#define NN_SLASH '\\'
#else
#define NN_SLASH '/'
#endif
NeuralNet::NeuralNet() :
mUseNorms(false),
mAllocated(false),
mBunchSize(0)
{
}
NeuralNet::~NeuralNet()
{
if(mAllocated)
{
Free();
}
}
char *NeuralNet::SkipSpaces(char *pText)
{
char *pt = pText;
while(pt != '\0' && strchr(" \t\n\r", *pt) != 0)
{
pt++;
}
return pt;
}
char *NeuralNet::SkipText(char *pText)
{
char *pt = pText;
while(pt != '\0' && strchr(" \t\n\r", *pt) == 0)
{
pt++;
}
return pt;
}
char *NeuralNet::SkipLines(char *pText, int n)
{
char *pt = pText;
int line = 0;
while(pt != '\0' && line != n)
{
if(*pt == '\n')
{
line++;
}
pt++;
}
if(pt == '\0' && line != n)
{
return 0;
}
return pt;
}
char *NeuralNet::LoadText(char *pFile)
{
FILE *pf;
pf = fopen(pFile, "rb");
if(pf == 0)
{
return 0;
}
int len = FileLen(pf);
char *ptext = new char[len + 1];
if(!ptext)
{
fclose(pf);
return 0;
}
if((int)fread(ptext, sizeof(char), len, pf) != len)
{
fclose(pf);
delete [] ptext;
return 0;
}
fclose(pf);
ptext[len] = '\0';
return ptext;
}
bool NeuralNet::GetInfo(char *pData, int *pNInp, int *pNHid, int *pNOut)
{
// input to hidden matrix
char *pt = pData;
pt = SkipSpaces(pt);
if(strncmp(pt, "weigvec", strlen("weigvec")) != 0)
{
return false;
}
pt += strlen("weigvec");
pt = SkipSpaces(pt);
int n_inp_hid;
if(!GetIntValue(pt, &n_inp_hid))
{
return false;
}
pt = SkipLines(pt, n_inp_hid + 1); // goto next line and skip n_inp_hid lines
if(pt == 0)
{
return false;
}
// hidden to output matrix
if(strncmp(pt, "weigvec", strlen("weigvec")) != 0)
{
return false;
}
pt += strlen("weigvec");
pt = SkipSpaces(pt);
int n_hid_out;
if(!GetIntValue(pt, &n_hid_out))
{
return false;
}
pt = SkipLines(pt, n_hid_out + 1); // goto next line and skip n_hid_out lines
if(pt == 0)
{
return false;
}
// hidden biases
if(strncmp(pt, "biasvec", strlen("biasvec")) != 0)
{
return false;
}
pt += strlen("biasvec");
pt = SkipSpaces(pt);
int n_biases_hid;
if(sscanf(pt, "%d", &n_biases_hid) != 1)
{
return false;
}
pt = SkipLines(pt, n_biases_hid + 1); // goto next line and skip n_biases_hid lines
if(pt == 0)
{
return false;
}
// output biases
if(strncmp(pt, "biasvec", strlen("biasvec")) != 0)
{
return false;
}
pt += strlen("biasvec");
pt = SkipSpaces(pt);
int n_biases_out;
if(sscanf(pt, "%d", &n_biases_out) != 1)
{
return false;
}
pt = SkipLines(pt, n_biases_out + 1); // goto next line and skip n_biases_out lines
if(pt == 0)
{
return false;
}
*pNOut = n_biases_out;
*pNHid = n_biases_hid;
*pNInp = n_inp_hid / n_biases_hid;
return true;
}
bool NeuralNet::ParseWeights(char *pData)
{
// null matrixes
memset((void *)mpInpHidMatrix, 0, mNHid16 * mNInp16 * sizeof(float));
memset((void *)mpHidOutMatrix, 0, mNOut16 * mNHid16 * sizeof(float));
memset((void *)mpHiddenBiases, 0, mNHid16 * sizeof(float));
memset((void *)mpOutputBiases, 0, mNOut16 * sizeof(float));
// input to hidden matrix
char *pt = pData;
pt = SkipSpaces(pt);
if(strncmp(pt, "weigvec", strlen("weigvec")) != 0)
{
return false;
}
pt += strlen("weigvec");
pt = SkipSpaces(pt);
int n_inp_hid;
if(!GetIntValue(pt, &n_inp_hid))
{
return false;
}
pt = SkipText(pt);
float *pmat = mpInpHidMatrix16;
int i;
for(i = 0; i < mNHid; i++)
{
int j;
for(j = 0; j < mNInp; j++)
{
pt = SkipSpaces(pt);
if(!GetFloatValue(pt, pmat))
{
return false;
}
pmat++;
pt = SkipText(pt);
}
for(j = mNInp; j < mNInp16; j++)
{
*pmat = 0.0f;
pmat++;
}
}
// hidden to output matrix
pt = SkipSpaces(pt);
if(strncmp(pt, "weigvec", strlen("weigvec")) != 0)
{
return false;
}
pt += strlen("weigvec");
pt = SkipSpaces(pt);
int n_hid_out;
if(!GetIntValue(pt, &n_hid_out))
{
return false;
}
pt = SkipText(pt);
pmat = mpHidOutMatrix16;
for(i = 0; i < mNOut; i++)
{
int j;
for(j = 0; j < mNHid; j++)
{
pt = SkipSpaces(pt);
if(!GetFloatValue(pt, pmat))
{
return false;
}
pmat++;
pt = SkipText(pt);
}
for(j = mNHid; j < mNHid16; j++)
{
*pmat = 0.0f;
pmat++;
}
}
// hidden biases
pt = SkipSpaces(pt);
if(strncmp(pt, "biasvec", strlen("biasvec")) != 0)
{
return false;
}
pt += strlen("biasvec");
pt = SkipSpaces(pt);
int n_biases_hid;
if(!GetIntValue(pt, &n_biases_hid))
{
return false;
}
pt = SkipText(pt);
float *pvec = mpHiddenBiases16;
for(i = 0; i < mNHid; i++)
{
pt = SkipSpaces(pt);
if(!GetFloatValue(pt, pvec))
{
return false;
}
pvec++;
pt = SkipText(pt);
}
// output biases
pt = SkipSpaces(pt);
if(strncmp(pt, "biasvec", strlen("biasvec")) != 0)
{
return false;
}
pt += strlen("biasvec");
pt = SkipSpaces(pt);
int n_biases_out;
if(!GetIntValue(pt, &n_biases_out))
{
return false;
}
pt = SkipText(pt);
pvec = mpOutputBiases16;
for(i = 0; i < mNOut; i++)
{
pt = SkipSpaces(pt);
if(!GetFloatValue(pt, pvec))
{
return false;
}
pvec++;
pt = SkipText(pt);
}
return true;
}
bool NeuralNet::ParseNorms(char *pData)
{
// null vectors
int i;
for(i = 0; i < mNInp16; i++)
{
mpNormMeans16[i] = 0.0f;
mpNormDevs16[i] = 1.0f;
}
// mean vector
char *pt = pData;
pt = SkipSpaces(pt);
if(strncmp(pt, "vec", strlen("vec")) != 0)
{
return false;
}
pt += strlen("vec");
pt = SkipSpaces(pt);
int len;
if(!GetIntValue(pt, &len))
{
return false;
}
pt = SkipText(pt);
float *pvct = mpNormMeans16;
for(i = 0; i < mNInp; i++)
{
pt = SkipSpaces(pt);
if(!GetFloatValue(pt, pvct))
{
return false;
}
pvct++;
pt = SkipText(pt);
}
// standard deviation vector
pt = SkipSpaces(pt);
if(strncmp(pt, "vec", strlen("vec")) != 0)
{
return false;
}
pt += strlen("vec");
pt = SkipSpaces(pt);
if(!GetIntValue(pt, &len))
{
return false;
}
pt = SkipText(pt);
pvct = mpNormDevs16;
for(i = 0; i < mNInp; i++)
{
pt = SkipSpaces(pt);
if(!GetFloatValue(pt, pvct))
{
return false;
}
pvct++;
pt = SkipText(pt);
}
// puts("means");
// DumpMatrix(mpNormMeans16, 1, mNInp16, mNInp16);
// puts("devs");
// DumpMatrix(mpNormDevs16, 1, mNInp16, mNInp16);
return true;
}
int NeuralNet::LoadAscii(char *pWeightFile, char *pNormFile, int bunchSize)
{
mBunchSize = bunchSize;
// Weights
char *pweights = LoadText(pWeightFile);
if(pweights == 0)
{
return NN_NOWEIGHTS;
}
if(!GetInfo(pweights, &mNInp, &mNHid, &mNOut))
{
delete [] pweights;
return NN_BADWEIGHTS;
}
Alloc();
if(!ParseWeights(pweights))
{
delete [] pweights;
return NN_BADWEIGHTS;
}
delete [] pweights;
// Norms
if(pNormFile)
{
char *pnorms = LoadText(pNormFile);
if(pnorms == 0)
{
delete [] pweights;
return NN_NONORMS;
}
if(!ParseNorms(pnorms))
{
delete [] pweights;
return NN_BADWEIGHTS;
}
mUseNorms = true;
delete [] pnorms;
}
return NN_OK;
}
int NeuralNet::LoadBinary(char *pFile, int bunchSize)
{
mBunchSize = bunchSize;
FILE *pf;
pf = fopen(pFile, "rb");
if(pf == 0)
{
return NN_NOWEIGHTS;
}
// number of layers (2 layers of neurons + 1 input layer)
int nlayers = 2;
if(fread(&nlayers, sizeof(int), 1, pf) != 1 || nlayers != 2)
{
return NN_BADWEIGHTS;
}
// sizes of layers
int size[3];
if((int)fread(size, sizeof(int), nlayers + 1, pf) != nlayers + 1)
{
return NN_WRITEERR;
}
mNInp = size[0];
mNHid = size[1];
mNOut = size[2];
// allocate memory
Alloc();
// read weights
if((int)fread(mpInpHidMatrix16, sizeof(float), mNInp16 * mNHid16, pf) != mNInp16 * mNHid16)
{
return NN_WRITEERR;
}
if((int)fread(mpHidOutMatrix16, sizeof(float), mNHid16 * mNOut16, pf) != mNHid16 * mNOut16)
{
return NN_WRITEERR;
}
// read biasses
if((int)fread(mpHiddenBiases16, sizeof(float), mNHid16, pf) != mNHid16)
{
return NN_WRITEERR;
}
if((int)fread(mpOutputBiases16, sizeof(float), mNOut16, pf) != mNOut16)
{
return NN_WRITEERR;
}
// read means and deviations
if((int)fread(mpNormMeans16, sizeof(float), mNInp16, pf) != mNInp16)
{
return NN_WRITEERR;
}
if((int)fread(mpNormDevs16, sizeof(float), mNInp16, pf) != mNInp16)
{
return NN_WRITEERR;
}
fclose(pf);
mUseNorms = true;
return NN_OK;
}
int NeuralNet::SaveBinary(char *pFile)
{
FILE *pf;
pf = fopen(pFile, "wb");
if(pf == 0)
{
return NN_CREATEERR;
}
// number of layers (2 layers of neurons + 1 input layer)
int nlayers = 2;
if((int)fwrite(&nlayers, sizeof(int), 1, pf) != 1)
{
return NN_WRITEERR;
}
// sizes of layers
int size[3];
size[0] = mNInp;
size[1] = mNHid;
size[2] = mNOut;
if((int)fwrite(size, sizeof(int), nlayers + 1, pf) != nlayers + 1)
{
return NN_WRITEERR;
}
// save weights
if((int)fwrite(mpInpHidMatrix16, sizeof(float), mNInp16 * mNHid16, pf) != mNInp16 * mNHid16)
{
return NN_WRITEERR;
}
if((int)fwrite(mpHidOutMatrix16, sizeof(float), mNHid16 * mNOut16, pf) != mNHid16 * mNOut16)
{
return NN_WRITEERR;
}
// save biasses
if((int)fwrite(mpHiddenBiases16, sizeof(float), mNHid16, pf) != mNHid16)
{
return NN_WRITEERR;
}
if((int)fwrite(mpOutputBiases16, sizeof(float), mNOut16, pf) != mNOut16)
{
return NN_WRITEERR;
}
// save means and deviations
if((int)fwrite(mpNormMeans16, sizeof(float), mNInp16, pf) != mNInp16)
{
return NN_WRITEERR;
}
if((int)fwrite(mpNormDevs16, sizeof(float), mNInp16, pf) != mNInp16)
{
return NN_WRITEERR;
}
fclose(pf);
return NN_OK;
}
int NeuralNet::Load(char *pWeightFile, char *pNormFile, int bunchSize)
{
// cut off suffix and add '.nbin'
char pbin_file[1024];
strcpy(pbin_file, pWeightFile);
char *pdot = strrchr(pbin_file, '.');
char *pslash = strrchr(pbin_file, NN_SLASH);
if(pdot != 0 && (pslash == 0 || pdot > pslash))
{
*pdot = '\0';
}
strcat(pbin_file, ".nbin");
// load binary if the file exists
if(LoadBinary(pbin_file, bunchSize) == NN_OK)
{
return NN_OK;
}
// load ascii otherwise and save it binary
int ret = LoadAscii(pWeightFile, pNormFile, bunchSize);
if(ret == NN_OK)
{
SaveBinary(pbin_file);
}
return ret;
}
float *NeuralNet::MemAlign16(float *pBuff)
{
char *pbuff = (char *)pBuff;
while(((unsigned int)pbuff & 0xF) != 0)
{
pbuff++;
}
return (float *)pbuff;
}
int NeuralNet::Align16(int n)
{
while((n & 0xF) != 0)
{
n++;
}
return n;
}
void NeuralNet::Alloc()
{
if(mAllocated)
{
Free();
}
mNInp16 = Align16(mNInp * sizeof(float)) / sizeof(float);
mNHid16 = Align16(mNHid * sizeof(float)) / sizeof(float);
mNOut16 = Align16(mNOut * sizeof(float)) / sizeof(float);
mpInputPatterns = new float[mBunchSize * mNInp16 + 0xF];
mpHiddenPatterns = new float[mBunchSize * mNHid16 + 0xF];
mpOutputPatterns = new float[mBunchSize * mNOut16 + 0xF];
mpInputPatterns16 = MemAlign16(mpInputPatterns);
mpHiddenPatterns16 = MemAlign16(mpHiddenPatterns);
mpOutputPatterns16 = MemAlign16(mpOutputPatterns);
// mNInp16 * mNHid16: calculate more (imaginar) neurons in the hidden
// layer to bypass copping of patterns between hidden and output layer
mpInpHidMatrix = new float [mNHid16 * mNInp16 + 0xF];
mpHidOutMatrix = new float [mNOut16 * mNHid16 + 0xF];
mpInpHidMatrix16 = MemAlign16(mpInpHidMatrix);
mpHidOutMatrix16 = MemAlign16(mpHidOutMatrix);
mpHiddenBiases = new float [mNHid16 + 0xF];
mpOutputBiases = new float [mNOut16 + 0xF];
mpHiddenBiases16 = MemAlign16(mpHiddenBiases);
mpOutputBiases16 = MemAlign16(mpOutputBiases);
mpNormMeans = new float [mNInp16 + 0xF];
mpNormDevs = new float [mNInp16 + 0xF];
mpNormMeans16 = MemAlign16(mpNormMeans);
mpNormDevs16 = MemAlign16(mpNormDevs);
mAllocated = true;
}
void NeuralNet::Free()
{
delete [] mpInputPatterns;
delete [] mpHiddenPatterns;
delete [] mpOutputPatterns;
delete [] mpInpHidMatrix;
delete [] mpHidOutMatrix;
delete [] mpHiddenBiases;
delete [] mpOutputBiases;
delete [] mpNormMeans;
delete [] mpNormDevs;
mAllocated = false;
}
void NeuralNet::Normalize(int bunchOccupation, float *pData, int len, float *pMeans, float *pDevs)
{
float *pdata = pData;
int i;
for(i = 0; i < bunchOccupation; i++)
{
int j;
for(j = 0; j < len; j++)
{
pdata[j] -= pMeans[j];
pdata[j] *= pDevs[j];
}
pdata += len;
}
}
#ifdef USE_BLAS
// C = A * TR(B) + C
// BLAS version
void NeuralNet::MatrixMultiplyAndAdd(int nARows, int nACols, int nTRBRows, float *pA, float *pB, float *pC)
{
if(nARows >= NN_BLAS_MINBUNCH) // use Blas matrix rutine
{
// C = aplha * A + TR(B) + beta * C
// args: matrix format (a11 a12 ... ; a21 a22 ... ; ...)
// A matrix preprocessing (NONE)
// B matrix preprocessing (TR)
// m, n, k - matrix dimensions
// alpha = 1.0
// A = (m x k)
// lda - length of one row (this values is added if the pointer is moved from
// the begin of one row to begin of next row)
// B = (k x n)
// ldb
// beta = 1.0
// C = (m x n)
// ldc
cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasTrans, nARows, nTRBRows, nACols,
1.0f, pA, nACols, pB, nACols /* B is stored transposed */, 1.0f, pC, nTRBRows);
}
else // calculate matrix multiplication vector by vector
// this should be faster for small amount of vectors according Standa
{
int i;
for(i = 0; i < nARows; i++)
{
// y = alpha * A * x + beta * y
// args: matrix format (a11 a12 ... ; a21 a22 ... ; ...)
// A matrix preprocessing (NONE)
// n, m - matrix dimensions
// alpha = 1
// A = (m x n)
// lda
// x
// incx = 1
// beta
// y
// incy = 1
cblas_sgemv(CblasRowMajor, CblasNoTrans, nTRBRows, nACols,
1.0f, pB, nACols, pA + (i * nACols), 1, 1.0f, pC + (i * nTRBRows), 1);
}
}
}
#else
#warning "Compilation without BLAS acceleration!"
// C = A * TR(B) + C
// unaccelerated version
void NeuralNet::MatrixMultiplyAndAdd(int nARows, int nACols, int nTRBRows, float *pA, float *pB, float *pC)
{
int i;
int j;
int k;
float *p_a = pA;
float *p_c = pC;
for(i = 0; i < nARows; i++)
{
float *p_b = pB;
for(j = 0; j < nTRBRows; j++)
{
for(k = 0; k < nACols; k++)
{
*p_c += p_a[k] * p_b[k];
}
p_c++;
p_b += nACols;
}
p_a += nACols;
}
}
#endif
void NeuralNet::Sigmoid(int bunchOccupation, float *pData, int len, int lenAligned)
{
float *pdata = pData;
int i;
for(i = 0; i < bunchOccupation; i++)
{
// Sigmoid
int j;
for(j = 0; j < len; j++)
{
#ifdef NN_FAST_EXP
*pdata = fexp_sigmoid(*pdata);
#else
*pdata = 1.0f / (1.0f + expf(-(*pdata)));
#endif
pdata++;
}
// null values for alligment
for(j = len; j < lenAligned; j++)
{
*pdata = 0.0f;
pdata++;
}
}
}
void NeuralNet::SoftMax(int bunchOccupation, float *pData, int len, int lenAligned)
{
float *pdata = pData;
int i;
for(i = 0; i < bunchOccupation; i++)
{
// Softmax
int j;
#ifdef NN_FAST_EXP
fexp_softmax_v(len, pdata);
#else
float sum = 0.0f;
for(j = 0; j < len; j++)
{
pdata[j] = expf(pdata[j]);
sum += pdata[j];
}
sum = 1.0f / sum;
for(j = 0; j < len; j++)
{
pdata[j] *= sum;
}
#endif
pdata += len;
// null values for alligment
for(j = len; j < lenAligned; j++)
{
*pdata = 0.0f;
pdata++;
}
}
}
void NeuralNet::PrepareBiases(int bunchOccupation, int len, float *pBiases, float *pPatterns)
{
float *ppatterns = pPatterns;
int i;
for(i = 0; i < bunchOccupation; i++)
{
int j;
for(j = 0; j < len; j++)
{
*ppatterns = pBiases[j];
ppatterns++;
}
}
}
void NeuralNet::ForwardPass1Bunch(int bunchOccupation, float *pInp, float *pOut)
{
// DumpMatrix(mpInputPatterns16, bunchOccupation, mNInp16, mNInp16);
// normalize features
if(mUseNorms)
{
Normalize(bunchOccupation, pInp, mNInp16, mpNormMeans16, mpNormDevs16);
}
// forward pass to hidden layer
PrepareBiases(bunchOccupation, mNHid16, mpHiddenBiases16, mpHiddenPatterns16);
MatrixMultiplyAndAdd(bunchOccupation, mNInp16, mNHid16,
mpInputPatterns16, mpInpHidMatrix16, mpHiddenPatterns16);
Sigmoid(bunchOccupation, mpHiddenPatterns16, mNHid, mNHid16);
// forward pass to output
PrepareBiases(bunchOccupation, mNOut16, mpOutputBiases16, mpOutputPatterns16);
MatrixMultiplyAndAdd(bunchOccupation, mNHid16, mNOut16,
mpHiddenPatterns16, mpHidOutMatrix16, mpOutputPatterns16);
// puts("xxx - out");
// DumpMatrix(mpOutputPatterns16, bunchOccupation, mNOut, mNOut16);
SoftMax(bunchOccupation, mpOutputPatterns16, mNOut, mNOut16);
// puts("xxx - out - softmax");
// DumpMatrix(mpOutputPatterns16, bunchOccupation, mNOut, mNOut16);
}
void NeuralNet::Forward(float *pInp, float *pOut, int nVct)
{
int n_bunches = nVct / mBunchSize; // number of the whole bunches
int n_rem = nVct % mBunchSize; // number of vectors in the last bunch
int inp_bunch_len = mNInp * mBunchSize;
int out_bunch_len = mNOut * mBunchSize;
// process vectors in bunches
int i;
int j;
for(i = 0; i < n_bunches; ++i)
{
// - copy unaligned memory block to aligned memory block
for(j = 0; j < mBunchSize; j++)
{
memcpy(mpInputPatterns16 + j * mNInp16, pInp + i * inp_bunch_len + j * mNInp, mNInp * sizeof(float));
memset(mpInputPatterns16 + j * mNInp16 + mNInp, 0, (mNInp16 - mNInp) * sizeof(float));
}
// - forward pass of one full bunch
ForwardPass1Bunch(mBunchSize, mpInputPatterns16, mpOutputPatterns16);
// - copy output data to unaligned memory block
for(j = 0; j < mBunchSize; j++)
{
memcpy(pOut + i * out_bunch_len + j * mNOut, mpOutputPatterns16 + j * mNOut16, mNOut * sizeof(float));
}
}
// process remained vectors
if(n_rem != 0)
{
// -copy unaligned memory block to aligned memory block
for(j = 0; j < n_rem; j++)
{
memcpy(mpInputPatterns16 + j * mNInp16, pInp + n_bunches * inp_bunch_len + j * mNInp, mNInp * sizeof(float));
memset(mpInputPatterns16 + j * mNInp16 + mNInp, 0, (mNInp16 - mNInp) * sizeof(float));
}
// forward pass of one full bunch
ForwardPass1Bunch(n_rem, mpInputPatterns16, mpOutputPatterns16);
// copy output data to unaligned memory block
for(j = 0; j < n_rem; j++)
{
memcpy(pOut + n_bunches * out_bunch_len + j * mNOut, mpOutputPatterns16 + j * mNOut16, mNOut * sizeof(float));
}
}
}
bool NeuralNet::GetFloatValue(char *pText, float *pRet)
{
char pbuff[100];
char *pt = pText;
int i = 0;
pt = SkipSpaces(pt);
while(pt != '\0' && i < 100 && strchr(" \t\n\r", pt[i]) == 0)
{
pbuff[i] = pt[i];
i++;
}
pbuff[i] = '\0';
return (sscanf(pbuff, "%e", pRet) == 1);
}
bool NeuralNet::GetIntValue(char *pText, int *pRet)
{
char pbuff[100];
char *pt = pText;
int i = 0;
pt = SkipSpaces(pt);
while(pt != '\0' && i < 100 && strchr(" \t\n\r", pt[i]) == 0)
{
pbuff[i] = pt[i];
i++;
}
pbuff[i] = '\0';
return (sscanf(pbuff, "%d", pRet) == 1);
}
void NeuralNet::DumpMatrix(float *pMat, int nRows, int nColumns, int nColumnsAligned)
{
int i;
float *prow = pMat;
for(i = 0; i < nRows; i++)
{
int j;
for(j = 0; j < nColumns; j++)
{
printf(" %f", prow[j]);
}
printf("\n");
prow += nColumnsAligned;
}
}
int NeuralNet::GetInputSize()
{
return mNInp;