OLD | NEW |
1 /* Copyright (c) 2008-2011 Octasic Inc. | 1 /* Copyright (c) 2008-2011 Octasic Inc. |
2 Written by Jean-Marc Valin */ | 2 Written by Jean-Marc Valin */ |
3 /* | 3 /* |
4 Redistribution and use in source and binary forms, with or without | 4 Redistribution and use in source and binary forms, with or without |
5 modification, are permitted provided that the following conditions | 5 modification, are permitted provided that the following conditions |
6 are met: | 6 are met: |
7 | 7 |
8 - Redistributions of source code must retain the above copyright | 8 - Redistributions of source code must retain the above copyright |
9 notice, this list of conditions and the following disclaimer. | 9 notice, this list of conditions and the following disclaimer. |
10 | 10 |
(...skipping 14 matching lines...) Expand all Loading... |
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 */ | 26 */ |
27 | 27 |
28 #ifndef _MLP_TRAIN_H_ | 28 #ifndef _MLP_TRAIN_H_ |
29 #define _MLP_TRAIN_H_ | 29 #define _MLP_TRAIN_H_ |
30 | 30 |
31 #include <math.h> | 31 #include <math.h> |
32 #include <stdlib.h> | 32 #include <stdlib.h> |
33 | 33 |
34 double tansig_table[501]; | 34 double tansig_table[501]; |
35 static inline double tansig_double(double x) | 35 static inline double tansig_double(double x) |
36 { | 36 { |
37 » return 2./(1.+exp(-2.*x)) - 1.; | 37 return 2./(1.+exp(-2.*x)) - 1.; |
38 } | 38 } |
39 static inline void build_tansig_table() | 39 static inline void build_tansig_table() |
40 { | 40 { |
41 » int i; | 41 int i; |
42 » for (i=0;i<501;i++) | 42 for (i=0;i<501;i++) |
43 » » tansig_table[i] = tansig_double(.04*(i-250)); | 43 tansig_table[i] = tansig_double(.04*(i-250)); |
44 } | 44 } |
45 | 45 |
46 static inline double tansig_approx(double x) | 46 static inline double tansig_approx(double x) |
47 { | 47 { |
48 » int i; | 48 int i; |
49 » double y, dy; | 49 double y, dy; |
50 » if (x>=10) | 50 if (x>=10) |
51 » » return 1; | 51 return 1; |
52 » if (x<=-10) | 52 if (x<=-10) |
53 » » return -1; | 53 return -1; |
54 » i = lrint(25*x); | 54 i = lrint(25*x); |
55 » x -= .04*i; | 55 x -= .04*i; |
56 » y = tansig_table[250+i]; | 56 y = tansig_table[250+i]; |
57 » dy = 1-y*y; | 57 dy = 1-y*y; |
58 » y = y + x*dy*(1 - y*x); | 58 y = y + x*dy*(1 - y*x); |
59 » return y; | 59 return y; |
60 } | 60 } |
61 | 61 |
62 inline float randn(float sd) | 62 inline float randn(float sd) |
63 { | 63 { |
64 float U1, U2, S, x; | 64 float U1, U2, S, x; |
65 do { | 65 do { |
66 U1 = ((float)rand())/RAND_MAX; | 66 U1 = ((float)rand())/RAND_MAX; |
67 U2 = ((float)rand())/RAND_MAX; | 67 U2 = ((float)rand())/RAND_MAX; |
68 U1 = 2*U1-1; | 68 U1 = 2*U1-1; |
69 U2 = 2*U2-1; | 69 U2 = 2*U2-1; |
70 S = U1*U1 + U2*U2; | 70 S = U1*U1 + U2*U2; |
71 } while (S >= 1 || S == 0.0f); | 71 } while (S >= 1 || S == 0.0f); |
72 x = sd*sqrt(-2 * log(S) / S) * U1; | 72 x = sd*sqrt(-2 * log(S) / S) * U1; |
73 return x; | 73 return x; |
74 } | 74 } |
75 | 75 |
76 | 76 |
77 typedef struct { | 77 typedef struct { |
78 » int layers; | 78 int layers; |
79 » int *topo; | 79 int *topo; |
80 » double **weights; | 80 double **weights; |
81 » double **best_weights; | 81 double **best_weights; |
82 » double *in_rate; | 82 double *in_rate; |
83 } MLPTrain; | 83 } MLPTrain; |
84 | 84 |
85 | 85 |
86 #endif /* _MLP_TRAIN_H_ */ | 86 #endif /* _MLP_TRAIN_H_ */ |
OLD | NEW |