ParametersFloat.java
3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package is2.data;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import is2.util.DB;
final public class ParametersFloat {
public float[] parameters;
public float[] total;
public ParametersFloat(int size) {
parameters = new float[size];
total = new float[size];
for (int i = 0; i < parameters.length; i++) {
parameters[i] = 0F;
total[i] = 0F;
}
}
/**
* @param parameters2
*/
public ParametersFloat(float[] p) {
parameters = p;
}
public void average(double avVal) {
for (int j = 0; j < total.length; j++) {
parameters[j] = total[j] / ((float) avVal);
}
total = null;
}
public ParametersFloat average2(double avVal) {
float[] px = new float[this.parameters.length];
for (int j = 0; j < total.length; j++) {
px[j] = total[j] / ((float) avVal);
}
ParametersFloat pf = new ParametersFloat(px);
return pf;
}
public void update(FV pred, FV act, float upd, float err) {
float lam_dist = act.getScore(parameters, false) - pred.getScore(parameters, false);
float loss = err - lam_dist;
FV dist = act.getDistVector(pred);
float alpha;
float A = dist.dotProduct(dist);
if (A <= 0.0000000000000001)
alpha = 0.0f;
else
alpha = loss / A;
// alpha = Math.min(alpha, 0.00578125F);
dist.update(parameters, total, alpha, upd, false);
}
public void update(FV pred, FV act, float upd, float err, float C) {
float lam_dist = act.getScore(parameters, false) - pred.getScore(parameters, false);
float loss = err - lam_dist;
FV dist = act.getDistVector(pred);
float alpha;
float A = dist.dotProduct(dist);
if (A <= 0.0000000000000001)
alpha = 0.0f;
else
alpha = loss / A;
alpha = Math.min(alpha, C);
dist.update(parameters, total, alpha, upd, false);
}
public double update(FV a, double b) {
double A = a.dotProduct(a);
if (A <= 0.0000000000000000001)
return 0.0;
return b / A;
}
public double getScore(FV fv) {
if (fv == null)
return 0.0F;
return fv.getScore(parameters, false);
}
final public void write(DataOutputStream dos) throws IOException {
dos.writeInt(parameters.length);
for (float d : parameters)
dos.writeFloat(d);
}
public void read(DataInputStream dis) throws IOException {
parameters = new float[dis.readInt()];
int notZero = 0;
for (int i = 0; i < parameters.length; i++) {
parameters[i] = dis.readFloat();
if (parameters[i] != 0.0F)
notZero++;
}
DB.println("read parameters " + parameters.length + " not zero " + notZero);
}
public int countNZ() {
int notZero = 0;
for (float parameter : parameters) {
if (parameter != 0.0F)
notZero++;
}
return notZero;
}
public F2SF getFV() {
return new F2SF(parameters);
}
public int size() {
return parameters.length;
}
public void update(FVR act, FVR pred, Instances isd, int instc, Parse dx, double upd, double e, float lam_dist) {
e++;
float b = (float) e - lam_dist;
FVR dist = act.getDistVector(pred);
dist.update(parameters, total, hildreth(dist, b), upd, false);
}
public void update(FVR pred, FVR act, float upd, float e) {
e++;
float lam_dist = act.getScore(parameters, false) - pred.getScore(parameters, false);
float b = e - lam_dist;
FVR dist = act.getDistVector(pred);
dist.update(parameters, total, hildreth(dist, b), upd, false);
}
protected double hildreth(FVR a, double b) {
double A = a.dotProduct(a);
if (A <= 0.0000000000000000001)
return 0.0;
return b / A;
}
public float getScore(FVR fv) { // xx
if (fv == null)
return 0.0F;
return fv.getScore(parameters, false);
}
}