Debian Codegraph @

current/ml/Functions.java

raw symbols SCIP
1 package ml;
2
3 public class Functions {
4
5 /*--------------------------------------------------------------*/
6 /*---------------- Sigmoid ----------------*/
7 /*--------------------------------------------------------------*/
8
9 public static double sigmoid(double x) {return 1.0/(1.0+Math.exp(-x));}
10
11 static final double sigmoidDerivativeX(double x) {return sigmoidDerivativeFX(sigmoid(x));}
12
13 static final double sigmoidDerivativeFX(double fx) {return fx*(1-fx);}
14
15 static final double sigmoidDerivativeXFX(double x, double fx) {return fx*(1-fx);}
16
17 /*--------------------------------------------------------------*/
18 /*---------------- Extended Sigmoid ----------------*/
19 /*--------------------------------------------------------------*/
20
21 public static double eSigmoid(double x) {
22 return 2*sigmoid(x)-1;
23 }
24
25 static final double eSigmoidDerivativeX(double x) {
26 return 2*sigmoidDerivativeX(x);
27 }
28
29 static final double eSigmoidDerivativeFX(double fx) {
30 final double fx2=fx+1;
31 // final double sfx=fx2*0.5;
32 // final double d=2*sigmoidDerivativeFX(sfx); //This should be correct
33 final double d2=0.5*fx2*(2-fx2); //This now matches the correct one and should be faster.
34 // assert(d==d2) : fx+", "+fx2+", "+sfx+", "+d+", "+d2;
35 return d2;
36 }
37
38 static final double eSigmoidDerivativeXFX(double x, double fx) {
39 return eSigmoidDerivativeFX(fx);//TODO: Change to x if x is faster
40 }
41
42 /*--------------------------------------------------------------*/
43 /*---------------- TanH ----------------*/
44 /*--------------------------------------------------------------*/
45
46 public static float tanh(double x) {
47 // return (float)Math.tanh(x); //This is slower and gives the same results
48 if(x<-20) {return -1;}
49 if(x>20) {return 1;}
50
51 double ex=Math.exp(x);
52 double emx=Math.exp(-x);
53 return (float)((ex-emx)/(ex+emx));
54
55 // float ex=(float)Math.exp(x);This gives totally different results
56 // float emx=(float)Math.exp(-x);
57 // return ((ex-emx)/(ex+emx));
58 }
59
60 static final double tanhDerivativeX(double x) {
61 return tanhDerivativeFX(tanh(x));
62 }
63
64 static final double tanhDerivativeFX(double fx) {
65 return 1-fx*fx;
66 }
67
68 static final double tanhDerivativeXFX(double x, double fx) {
69 return 1-fx*fx;
70 }
71
72 /*--------------------------------------------------------------*/
73 /*---------------- Swish ----------------*/
74 /*--------------------------------------------------------------*/
75
76 public static double swish(double x) {
77 // double b=1;
78 // return x*sigmoidD(b*x);
79 // return x*sigmoidD(x);
80 final double y=x/(1+Math.exp(-x)); //Maybe faster
81 // return y<100000 ? y : Math.min(y, 100000+Math.log(y));
82 return y;
83 }
84
85 public static double swishDerivativeX(double x) {
86 double sigx=sigmoid(x);
87 double fx=x*sigx;
88 //double fx=swish(x);
89 return swishDerivativeFXSIGX(fx, sigx);
90 }
91
92 static final double swishDerivativeFX(double fx) {
93 throw new RuntimeException("Unimplemented.");
94 }
95
96 public static double swishDerivativeXFX(double x, double fx) {
97 double sigx=sigmoid(x);
98 return swishDerivativeFXSIGX(fx, sigx);
99 }
100
101 public static double swishDerivativeFXSIGX(double fx, double sigx) {
102 return fx+sigx*(1-fx);
103 }
104
105 /*--------------------------------------------------------------*/
106 /*---------------- RSLog ----------------*/
107 /*--------------------------------------------------------------*/
108
109 public static double rslog(double x) {
110 if(x<0) {return -rslog(-x);}
111 return Math.log(x+1);
112 }
113
114 static final double rslogDerivativeX(double x) {
115 if(x<0) {x=-x;}
116 return 1/(x+1);
117 }
118
119 static final double rslogDerivativeFX(double fx) {
120 assert(false);
121 if(fx<0) {fx=-fx;}
122 //if fx=10 then log(x+1)=10 so e^10=x+1
123 double x=Math.exp(fx)-1;
124 return rslogDerivativeX(x);
125 }
126
127 static final double rslogDerivativeXFX(double x, double fx) {
128 return rslogDerivativeX(x);
129 }
130
131 /*--------------------------------------------------------------*/
132 /*---------------- Mirrored Sigmoid ----------------*/
133 /*--------------------------------------------------------------*/
134
135 private static final double MSIG_X_OFFSET=5; //Bigger makes the peak higher, wider, and less sharp
136 private static final double MSIG_X_MULT=2; //Useful if you design around a 0-1 range
137 private static final double MSIG_Y_MULT=1.0/sigmoid(MSIG_X_OFFSET);//Should be very slightly higher than 1.
138
139 public static double mSig(double x) {
140 final double offset=MSIG_X_OFFSET;
141 final double xmult=MSIG_X_MULT;
142 final double ymult=MSIG_Y_MULT;
143
144 //sigmoid: 1.0/(1.0+Math.exp(-x));
145 if(x<0) {
146 //=1/(1+EXP(-(2*C7+$E$5)))
147 double y=1.0/(1.0+Math.exp(-(xmult*x+offset)));
148 // double y2=sigmoid(mult*x+offset);
149 // assert(y2==y) : y+", "+y2+", "+x;
150 return ymult*y;
151 }else {
152 double y=1.0/(1.0+Math.exp(xmult*x-offset));
153 // double y2=sigmoid(-(mult*x-offset));
154 // assert(y2==y) : y+", "+y2+", "+x;
155 return ymult*y;
156 }
157 }
158
159 static final double mSigDerivativeX(double x) {
160 double fx=mSig(x);
161 return mSigDerivativeXFX(x, fx);
162 }
163
164 static final double mSigDerivativeFX(double fx) {
165 throw new RuntimeException("Cannot be calculated.");
166 }
167
168 static final double mSigDerivativeXFX(double x, double fx) {
169 final double xmult=MSIG_X_MULT;
170 double d=sigmoidDerivativeFX(fx);
171 if(x<0){return xmult*d;}
172 return -xmult*d;
173 }
174
175 /*--------------------------------------------------------------*/
176 /*---------------- Extended Mirrored Sigmoid ----------------*/
177 /*--------------------------------------------------------------*/
178
179 public static double emSig(double x) {
180 return 2*mSig(x)-1;
181 }
182
183 static final double emSigDerivativeX(double x) {
184 return 2*mSigDerivativeX(x);
185 }
186
187 static final double emSigDerivativeFX(double fx) {
188 throw new RuntimeException("Cannot be calculated.");
189 }
190
191 static final double emSigDerivativeXFX(double x, double fx) {
192 return 2*mSigDerivativeX(x); //Possibly slow, but simple.
193 }
194
195 /*--------------------------------------------------------------*/
196 /*---------------- Gaussian ----------------*/
197 /*--------------------------------------------------------------*/
198
199 public static double bell(double x) {
200 return Math.exp(-(x*x));
201 }
202
203 static final double bellDerivativeX(double x) {
204 return -2*x*Math.exp(-x*x); //i.e. -2*x*fx
205 }
206
207 static final double bellDerivativeFX(double fx) {
208 throw new RuntimeException("Cannot be calculated.");
209 }
210
211 static final double bellDerivativeXFX(double x, double fx) {
212 return -2*x*fx;
213 }
214
215 /*--------------------------------------------------------------*/
216 /*---------------- Other ----------------*/
217 /*--------------------------------------------------------------*/
218
219 //For multiple tests of a single output neuron
220 public static double mse(float[] target, float[] actual) {
221 assert(target.length==actual.length);
222 double sum=0;
223 final int len=target.length;
224 for(int i=0; i<len; i++) {
225 float t=target[i], a=actual[i];
226 float dif=t-a;
227 sum+=(dif*dif);
228 }
229 return sum/len;
230 }
231
232 }