| 1 |
package ml; |
| 2 |
|
| 3 |
public class Functions { |
| 4 |
|
| 5 |
|
| 6 |
|
| 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 |
|
| 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 |
|
| 32 |
|
| 33 |
final double d2=0.5*fx2*(2-fx2); |
| 34 |
|
| 35 |
return d2; |
| 36 |
} |
| 37 |
|
| 38 |
static final double eSigmoidDerivativeXFX(double x, double fx) { |
| 39 |
return eSigmoidDerivativeFX(fx); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public static float tanh(double x) { |
| 47 |
|
| 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 |
|
| 56 |
|
| 57 |
|
| 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 |
|
| 74 |
|
| 75 |
|
| 76 |
public static double swish(double x) { |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
final double y=x/(1+Math.exp(-x)); |
| 81 |
|
| 82 |
return y; |
| 83 |
} |
| 84 |
|
| 85 |
public static double swishDerivativeX(double x) { |
| 86 |
double sigx=sigmoid(x); |
| 87 |
double fx=x*sigx; |
| 88 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 133 |
|
| 134 |
|
| 135 |
private static final double MSIG_X_OFFSET=5; |
| 136 |
private static final double MSIG_X_MULT=2; |
| 137 |
private static final double MSIG_Y_MULT=1.0/sigmoid(MSIG_X_OFFSET); |
| 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 |
|
| 145 |
if(x<0) { |
| 146 |
|
| 147 |
double y=1.0/(1.0+Math.exp(-(xmult*x+offset))); |
| 148 |
|
| 149 |
|
| 150 |
return ymult*y; |
| 151 |
}else { |
| 152 |
double y=1.0/(1.0+Math.exp(xmult*x-offset)); |
| 153 |
|
| 154 |
|
| 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 |
|
| 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); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 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); |
| 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 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 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 |
} |