Debian Codegraph @

current/ml/Function.java

raw symbols SCIP
1 package ml;
2
3 import java.util.Arrays;
4 import java.util.Random;
5
6 import shared.Tools;
7
8 public abstract class Function {
9
10 /*--------------------------------------------------------------*/
11 /*---------------- Methods ----------------*/
12 /*--------------------------------------------------------------*/
13
14 public abstract double activate(double x);
15
16 public abstract double derivativeX(double x);
17
18 public abstract double derivativeFX(double fx);
19
20 public abstract double derivativeXFX(double x, double fx);
21
22 public abstract int type();
23
24 public abstract String name();
25
26 /*--------------------------------------------------------------*/
27 /*---------------- Static Methods ----------------*/
28 /*--------------------------------------------------------------*/
29
30 static final int toType(String b) {
31 return toType(b, true);
32 }
33
34 static final int toType(String b, boolean assertValid) {
35 int type;
36 if(Tools.startsWithLetter(b)) {
37 type=Tools.findIC(b, TYPES);
38 if(type<0) {type=Tools.findIC(b, TYPES_LONG);}
39 }else{
40 type=Integer.parseInt(b);
41 throw new RuntimeException("Numbers are not allowed for defining types: "+b);
42 }
43 assert(!assertValid || type>=0 && type<TYPES.length) : type;
44 // System.err.println(b+" -> "+type);
45 return type;
46 }
47
48 public static synchronized final void normalizeTypeRates() {
49 assert(TYPE_RATES_CUM==null);
50 double sum=shared.Vector.sum(TYPE_RATES);
51 assert(sum>=0) : sum;
52
53 if(sum<=0) {
54 TYPE_RATES_CUM=null;
55 return;
56 }
57 if(Tools.absdif(sum, 1)>0.000001){
58 final double mult=1.0/sum;
59 for(int i=0; i<TYPE_RATES.length; i++) {
60 double r=TYPE_RATES[i];
61 assert(r>=0) : i+": "+r;
62 TYPE_RATES[i]=(float)(r*mult);
63 }
64 }
65
66 TYPE_RATES_CUM=new float[TYPE_RATES.length];
67 double c=0;
68 for(int i=0; i<TYPE_RATES.length; i++) {
69 double r=TYPE_RATES[i];
70 assert(r>=0) : i+": "+r;
71 c+=r;
72 TYPE_RATES_CUM[i]=(float)c;
73 }
74 assert(Tools.absdif(c, 1)<0.00001);
75 TYPE_RATES_CUM[TYPE_RATES_CUM.length-1]=1;
76 }
77
78 /*--------------------------------------------------------------*/
79
80 public static final Function getFunction(int type) {
81 return functions[type];
82 }
83
84 private static final Function[] makeFunctions() {
85 assert(functions==null);
86 Function[] array=new Function[TYPES.length];
87 array[SIG]=Sigmoid.instance;
88 array[TANH]=Tanh.instance;
89 array[RSLOG]=RSLog.instance;
90 array[MSIG]=MSig.instance;
91 array[SWISH]=Swish.instance;
92 array[ESIG]=ExtendedSigmoid.instance;
93 array[EMSIG]=ExtendedMSig.instance;
94 array[BELL]=Bell.instance;
95 for(int i=0; i<array.length; i++) {
96 Function f=array[i];
97 assert(f!=null) : i+", "+TYPES[i]+", "+f;
98 assert(f.type()==i) : i+", "+TYPES[i]+", "+f;
99 assert(f.name().equals(TYPES[i])) : i+", "+f;
100 }
101 return array;
102 }
103
104 /*--------------------------------------------------------------*/
105
106 static final Function randomFunction(Random randy) {
107 final int type=randomType(randy, TYPE_RATES_CUM);
108 return functions[type];
109 }
110
111 static final int randomType(Random randy, float[] cumRate) {
112 float f=randy.nextFloat();
113 for(int i=0; i<cumRate.length; i++) {
114 if(cumRate[i]>=f) {return i;}
115 }
116 assert(false) : f+", "+Arrays.toString(cumRate);
117 return cumRate.length-1;
118 }
119
120 /*--------------------------------------------------------------*/
121 /*---------------- Fields ----------------*/
122 /*--------------------------------------------------------------*/
123
124 public static final int SIG=0, TANH=1, RSLOG=2, MSIG=3, SWISH=4, ESIG=5, EMSIG=6, BELL=7;
125
126 static final String[] TYPES=new String[] {"SIG", "TANH", "RSLOG", "MSIG", "SWISH", "ESIG", "EMSIG", "BELL"};
127
128 static final String[] TYPES_LONG=new String[] {"SIGMOID", "HYPERBOLICTANGENT",
129 "ROTATIONALLYSYMMETRICLOGARITHM", "MIRROREDSIGMOID", "SWISH",
130 "EXTENDEDSIGMOID", "EXTENDEDMIRROREDSIGMOID", "GAUSSIAN"};
131
132 private static final Function[] functions=makeFunctions();
133
134 public static final float[] TYPE_RATES=new float[TYPES.length];
135 //tanh=.4 sig=.6 msig=.02 rslog=.02 swish=0
136
137 public static float[] TYPE_RATES_CUM=null;
138
139 static {
140 TYPE_RATES[TANH]=0.4f;
141 TYPE_RATES[SIG]=0.6f;
142 TYPE_RATES[MSIG]=0.02f;
143 TYPE_RATES[RSLOG]=0.02f;
144 }
145 }