| 1 |
package ml; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
|
| 5 |
import structures.ByteBuilder; |
| 6 |
|
| 7 |
public class Matrix { |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
void initializeRange() { |
| 14 |
detectRange(); |
| 15 |
if(convertTo01) { |
| 16 |
convertToZeroOne(outputMidpoint); |
| 17 |
} |
| 18 |
if(setTargetOutputRangeMin || setTargetOutputRangeMax) { |
| 19 |
adjustRange(); |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
void detectRange() { |
| 26 |
outputMin=Float.MAX_VALUE; |
| 27 |
outputMax=-Float.MAX_VALUE; |
| 28 |
double sum=0; |
| 29 |
long count=0; |
| 30 |
for(float[] line : outputs){ |
| 31 |
for(float f : line){ |
| 32 |
outputMin=Math.min(f, outputMin); |
| 33 |
outputMax=Math.max(f, outputMax); |
| 34 |
sum+=f; |
| 35 |
count++; |
| 36 |
} |
| 37 |
} |
| 38 |
assert(outputMin<outputMax) : outputMin+", "+outputMax; |
| 39 |
outputMean=(float)(sum/count); |
| 40 |
outputRange=outputMax-outputMin; |
| 41 |
outputMidpoint=outputMin+outputRange*0.5f; |
| 42 |
} |
| 43 |
|
| 44 |
void convertToZeroOne(final float cutoff) { |
| 45 |
double sum=0; |
| 46 |
long count=0; |
| 47 |
for(float[] line : outputs){ |
| 48 |
for(int j=0; j<line.length; j++){ |
| 49 |
final float f=line[j]<cutoff ? 0 : 1; |
| 50 |
line[j]=f; |
| 51 |
sum+=f; |
| 52 |
} |
| 53 |
} |
| 54 |
outputMin=0; |
| 55 |
outputMax=1; |
| 56 |
outputMean=(float)(sum/count); |
| 57 |
outputRange=outputMax-outputMin; |
| 58 |
outputMidpoint=outputMin+outputRange*0.5f; |
| 59 |
} |
| 60 |
|
| 61 |
void adjustRange() { |
| 62 |
assert(setTargetOutputRangeMin || setTargetOutputRangeMax) : "Must set minoutput or maxoutput"; |
| 63 |
assert(outputMin<outputMax) : outputMin+", "+outputMax; |
| 64 |
if(!setTargetOutputRangeMin){targetOutputRangeMin=outputMin;} |
| 65 |
if(!setTargetOutputRangeMax){targetOutputRangeMax=outputMax;} |
| 66 |
if(targetOutputRangeMin==outputMin && targetOutputRangeMax==outputMax) {return;} |
| 67 |
|
| 68 |
final float range2=targetOutputRangeMax-targetOutputRangeMin; |
| 69 |
assert(range2!=outputRange); |
| 70 |
final float mult=range2/outputRange; |
| 71 |
|
| 72 |
double sum=0; |
| 73 |
long count=0; |
| 74 |
for(float[] line : outputs){ |
| 75 |
for(int i=0; i<line.length; i++){ |
| 76 |
float f=((line[i]-outputMin)*mult)+targetOutputRangeMin; |
| 77 |
line[i]=f; |
| 78 |
sum+=f; |
| 79 |
count++; |
| 80 |
} |
| 81 |
} |
| 82 |
outputMin=targetOutputRangeMin; |
| 83 |
outputMax=targetOutputRangeMax; |
| 84 |
outputMean=(float)(sum/count); |
| 85 |
outputRange=outputMax-outputMin; |
| 86 |
outputMidpoint=outputMin+outputRange*0.5f; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public String toString(){ |
| 92 |
ByteBuilder bb=new ByteBuilder(); |
| 93 |
bb.append(columns.toString()).nl(); |
| 94 |
bb.append("lines="+inputs.length).nl(); |
| 95 |
bb.append("inputs="+numInputs).nl(); |
| 96 |
bb.append("outputs="+numOutputs).nl(); |
| 97 |
bb.append("mean="+outputMean).nl(); |
| 98 |
bb.append("midpoint="+outputMidpoint).nl(); |
| 99 |
bb.append("range="+outputRange).nl(); |
| 100 |
bb.append("inputs="+numInputs).nl(); |
| 101 |
return bb.toString(); |
| 102 |
} |
| 103 |
|
| 104 |
int numInputs() {return numInputs;} |
| 105 |
int numOutputs() {return numOutputs;} |
| 106 |
public float outputMidpoint() {return outputMidpoint;} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
ArrayList<String> columns; |
| 113 |
int[] dims; |
| 114 |
int numInputs; |
| 115 |
int numOutputs; |
| 116 |
int numPositive=0, numNegative=0; |
| 117 |
int validLines=0; |
| 118 |
int invalidLines=0; |
| 119 |
|
| 120 |
private float outputMin; |
| 121 |
private float outputMax; |
| 122 |
private float outputMean; |
| 123 |
private float outputMidpoint; |
| 124 |
private float outputRange; |
| 125 |
|
| 126 |
float[][][] data; |
| 127 |
float inputs[][], outputs[][]; |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
static boolean convertTo01=false; |
| 133 |
static float targetOutputRangeMin=0; |
| 134 |
static float targetOutputRangeMax=0; |
| 135 |
|
| 136 |
static boolean setTargetOutputRangeMin=false; |
| 137 |
static boolean setTargetOutputRangeMax=false; |
| 138 |
|
| 139 |
} |