Debian Codegraph @

current/ml/Sample.java

raw symbols SCIP
1 package ml;
2
3 import java.util.Arrays;
4
5 import structures.ByteBuilder;
6
7 public class Sample implements Comparable<Sample> {
8
9 public Sample(float[] in_, float[] out_, int id_) {
10 in=in_;
11 goal=out_;
12 result=new float[goal.length];
13 id=id_;
14 positive=(goal[0]>=0.5f);
15 }
16
17 @Override
18 public int compareTo(Sample o) {
19 final float a=pivot, b=o.pivot;
20 return a>b ? -1 : b>a ? 1 : id-o.id;
21 }
22
23 public boolean checkPivot() {
24 return pivot==calcPivot();
25 }
26
27 synchronized void setPivot() {
28 pivot=calcPivot();
29 }
30
31 synchronized float calcPivot() {
32 final float v=result[0];
33 final boolean positiveError=v>goal[0];
34 final boolean excess=(positiveError == positive);
35 final float mult=(excess ? excessPivotMult*0.5f : 0.5f);
36 return (errorMagnitude+weightedErrorMagnitude)*mult-epoch*EPOCH_MULT;
37 // return (errorMagnitude+weightedErrorMagnitude)*0.5f-epoch*EPOCH_MULT;
38 }
39
40 public String toString() {
41 // String s="S%d\t%s\t%s\tep=%d\tg=%4f\tr=%4f\tem=%6f\tev=%.6f\tpv=%.6f";
42 String s="S%d\t%s\t%s\tep=%d\tg=%4f\tr=%4f\tem=%6f\tpv=%.6f";
43
44
45 boolean gol=(goal[0]>=0.5f);
46 boolean pred=(result[0]>=0.5f);
47 String type=(gol && pred) ? "TP" : (!gol && !pred) ? "TN" : (!gol && pred) ? "FP" : (gol && !pred) ? "FN" : "??";
48 String sign=(positive ? "+" : "-");
49
50 // s=String.format(s, id, sign, type, epoch, goal[0], result[0], errorMagnitude, errorValue, calcPivot());
51 s=String.format(s, id, sign, type, epoch, goal[0], result[0], errorMagnitude, calcPivot());
52 return s+"\t"+Arrays.toString(in);
53 }
54
55 public ByteBuilder toBytes() {
56 return toBytes(new ByteBuilder());
57 }
58
59 public ByteBuilder toBytes(ByteBuilder bb) {
60 for(float f : in) {bb.append(f, 6).tab();}
61 for(float f : goal) {bb.append(f, 6).tab();}
62 bb.trimLast(1);
63 bb.nl();
64 return bb;
65 }
66
67 // synchronized boolean positive() {
68 // return goal[0]>=0.5f;
69 // }
70
71 public void calcError(float weightMult){
72 double error=0;
73 for(int i=0; i<result.length; i++){
74 float r=result[i];
75 float g=goal[i];
76 float e=calcError(g, r);
77 error+=e;
78 }
79 errorMagnitude=(float)error;
80 weightedErrorMagnitude=Cell.toWeightedError(error, result[0], goal[0], weightMult);
81 }
82
83 public synchronized int epoch() {return epoch;}
84 public synchronized int lastTID() {return lastTID;}
85 public synchronized void setEpoch(long x) {
86 epoch=(int)x;
87 }
88
89 public synchronized void setLastTID(int x) {
90 lastTID=x;
91 }
92
93 public static final float calcError(float goal, float pred) {
94 float e=goal-pred;
95 return 0.5f*e*e;
96 }
97
98 final boolean positive;
99 float errorMagnitude=1;
100 float weightedErrorMagnitude=1;
101 // float errorValue=1;//Unused, commented for efficiency
102 private int epoch=-1;
103 private int lastTID=-1;
104 float pivot=0;
105
106 final float[] in;
107 final float[] goal;
108 final float[] result;//Can't be volatile
109 final int id;
110
111 //0.2f is good for binary classifiers
112 public static float excessPivotMult=0.2f;
113 public static final float EPOCH_MULT=1/256f;
114 }