-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharow.d
301 lines (239 loc) · 5.95 KB
/
arow.d
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/**
* Authors: Kazuya Gokita (@kazoo04)
*/
import std.stdio;
import std.math;
import std.file;
import std.stream;
import std.string;
import std.conv;
import std.random;
struct feature {
uint index;
double weight;
}
class example {
int label; //(-1, +1)
feature[] fv;
}
/***
* Adaptive Regularization of Weight Vectors
*
* See_Also:
* K. Crammer, A. Kulesza, and M. Dredze. "Adaptive regularization of weight vectors" NIPS 2009
*/
class Arow {
private:
size_t size; /// Dimention
double[] mean; /// Average vector: μ
double[] cov; /// Variance Matrix: ∑ (diagonal matrix)
immutable double hyperparameter = 0.1; ///Hyper parameter: r (r > 0)
invariant()
{
assert(size > 0);
assert(mean != null);
assert(cov != null);
assert(mean.length == size);
assert(cov.length == size);
assert(hyperparameter > 0);
}
public:
this(size_t num_feature) {
size = num_feature;
mean = new double[size];
cov = new double[size];
for(int i = 0; i < size; i++) {
mean[i] = 0.0;
cov[i] = 1.0;
}
}
/**
* Calculate the distance between a vector and the hyperplane
* Params:
* fv = feature
* Returns: Margin(Euclidean distance)
*/
double GetMargin(feature[] fv)
in
{
assert(fv != null);
}
out (result)
{
assert(result != double.nan);
}
body
{
// margin = x_t^T μ_t
double margin = 0.0;
// inner product
foreach(v; fv) {
margin += mean[v.index] * v.weight;
}
return margin;
}
/**
* Calculate confidence
* Params:
* fv = feature
*
* Returns: confidence
*/
double GetConfidence(feature[] fv)
// confidence = x_t^T ∑_{t-1} x_t
in
{
assert(fv != null);
}
out(result)
{
assert(result != double.nan);
}
body
{
//calculate confidence
double confidence = 0.0;
foreach(v; fv) {
confidence += cov[v.index] * v.weight * v.weight;
}
return confidence;
}
/**
* Update weight vector
* Params:
* fv = feature
* label = class label (+1, -1)
* Returns: loss (0 | 1)
*/
int Update(feature[] fv, int label)
in
{
assert(label == -1 || label == +1);
assert(fv != null);
}
out (result)
{
assert(result == 0 || result == 1);
}
body
{
double m = GetMargin(fv);
if(m * label >= 1) return 0;
double confidence = GetConfidence(fv);
double beta = 1.0 / (confidence + hyperparameter);
double alpha = (1.0 - label * m) * beta;
//Update mean(μ)
foreach (v; fv) {
mean[v.index] += alpha * cov[v.index] * label * v.weight;
}
//Update covariance(∑)
foreach (v; fv) {
cov[v.index] = 1.0
/ ((1.0 / cov[v.index]) + v.weight * v.weight / hyperparameter);
}
//Squared Hinge-loss
return m * label < 0 ? 1 : 0;
}
/**
* Predict
* Params:
* fv = feature
* Returns: class label (+1, -1)
*/
int Predict(feature[] fv)
in
{
assert(fv != null);
}
out(result)
{
assert(result == -1 || result == +1);
}
body
{
double m = GetMargin(fv);
return m > 0 ? 1 : -1;
}
feature[] ParseLine(string line, int label) {
immutable string delim_value = ":";
immutable string delim_cols = " ";
feature[] fv;
string[] columns = line.split(delim_cols);
for(int i = 1; i < columns.length; i++) {
string[] arr = columns[i].split(delim_value);
feature f;
if(arr.length != 2)
continue;
assert(arr != null);
assert(arr.length == 2);
f.index = to!int(arr[0]);
f.weight = to!double(arr[1]);
fv ~= f;
}
return fv;
}
example[] ReadData(string filename){
Stream file = new BufferedFile(filename);
size_t num_lines = 0;
example[] data;
foreach (char[] _line; file) {
string line = cast(string)_line;
if (line.length == 0) continue;
if (line[0] == '#') continue;
assert(line[0] == '-' || line[0] == '+');
int label = line[0] == '+' ? +1 : -1;
feature[] vec = ParseLine(line, label);
if(vec != null) {
example ex = new example();
ex.label = label;
ex.fv = vec;
assert(vec != null);
assert(ex.fv != null);
assert(ex.label == -1 || ex.label == +1);
data ~= ex;
}
}
file.close();
return data;
}
}
void main(string[] args)
{
version(all)
{
immutable uint dimention = 1355192;
Arow arow = new Arow(dimention);
example[] data = arow.ReadData("news20.binary");
Random rand;
randomShuffle(data, rand);
ulong num_example = data.length;
ulong train_size = cast(uint)(num_example * 0.75);
ulong test_size = num_example - train_size;
writefln("train: %d", cast(int)train_size);
writefln("test: %d", cast(int)test_size);
example[] train;
example[] test;
train.length = train_size;
test.length = test_size;
for(int i = 0; i < train_size; i++)
train[i] = data[i];
for(int i = 0; i < test_size; i++)
test[i] = data[i + train_size];
for(int i = 0; i < 3; i++) {
//Train
foreach(t; train) {
arow.Update(t.fv, t.label);
}
//Predict
int mistake = 0;
foreach(t; test) {
int label = arow.Predict(t.fv);
if(label != t.label) mistake++;
}
writefln("%dth iteration:", i);
writefln("Number of mistake: %d", mistake);
writefln("Error rate: %f", mistake * 1.0 / test_size);
writefln("");
}
}
}