FV.java
11.2 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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
package is2.data;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public final class FV extends IFV {
private FV subfv1;
private FV subfv2;
private boolean negateSecondSubFV = false;
private int size;
// content of the nodes NxC
private int m_index[];
// type of the nodes NxT
public FV() {
this(10);
}
public FV(int initialCapacity) {
m_index = new int[initialCapacity];
}
public FV(FV fv1, FV fv2) {
subfv1 = fv1;
subfv2 = fv2;
}
public FV(FV fv1, FV fv2, boolean negSecond) {
this(0);
subfv1 = fv1;
subfv2 = fv2;
negateSecondSubFV = negSecond;
}
/**
* Read a feature vector
*
* @param index
* @param value
*/
public FV(DataInputStream dos, int capacity) throws IOException {
this(capacity);
size = m_index.length;
for (int i = 0; i < size; i++)
m_index[i] = dos.readInt();
}
/**
* Read a feature vector
*
* @param index
* @param value
*/
public FV(DataInputStream dos) throws IOException {
this(dos.readInt());
size = m_index.length;
for (int i = 0; i < size; i++)
m_index[i] = dos.readInt();
}
/**
* Increases the capacity of this <tt>Graph</tt> instance, if necessary, to
* ensure that it can hold at least the number of nodes specified by the
* minimum capacity argument.
*
* @param minCapacity
* the desired minimum capacity.
*/
private void ensureCapacity(int minCapacity) {
if (minCapacity > m_index.length) {
int oldIndex[] = m_index;
int newCapacity = (m_index.length * 3) / 2 + 1;
if (newCapacity < minCapacity)
newCapacity = minCapacity;
m_index = new int[newCapacity];
System.arraycopy(oldIndex, 0, m_index, 0, oldIndex.length);
}
}
final public int size() {
return size;
}
final public boolean isEmpty() {
return size == 0;
}
@Override
final public void clear() {
size = 0;
}
final public int createFeature(int i, double v) {
ensureCapacity(size + 1);
m_index[size] = i;
size++;
return size - 1;
}
final public int createFeature(int i) {
ensureCapacity(size + 1);
m_index[size] = i;
size++;
return size - 1;
}
final public int getIndex(int i) {
return m_index[i];
}
public void setIndex(int p, int i) {
m_index[p] = i;
}
/**
* Trims the capacity of this <tt>Graph</tt> instance to true size. An
* application can use this operation to minimize the storage of an
* <tt>Graph</tt> instance.
*/
public void trimToSize() {
if (size < m_index.length) {
int oldIndex[] = m_index;
m_index = new int[size];
System.arraycopy(oldIndex, 0, m_index, 0, size);
}
}
@Override
final public void add(int i) {
if (i >= 0) {
ensureCapacity(size + 1);
m_index[size] = i;
size++;
}
}
final public void add(int[] i) {
for (int element : i)
add(element);
}
final public void put(int i, double f) {
if (i >= 0)
createFeature(i, f);
}
// fv1 - fv2
public FV getDistVector(FV fl2) {
return new FV(this, fl2, true);
}
public double getScore(double[] parameters, boolean negate) {
double score = 0.0;
if (null != subfv1) {
score += subfv1.getScore(parameters, negate);
if (null != subfv2) {
if (negate)
score += subfv2.getScore(parameters, !negateSecondSubFV);
else
score += subfv2.getScore(parameters, negateSecondSubFV);
}
}
if (negate)
for (int i = 0; i < size; i++)
score -= parameters[m_index[i]];
else
for (int i = 0; i < size; i++)
score += parameters[m_index[i]];
return score;
}
final public float getScore(float[] parameters, boolean negate) {
float score = 0.0F;
if (null != subfv1) {
score += subfv1.getScore(parameters, negate);
if (null != subfv2) {
if (negate)
score += subfv2.getScore(parameters, !negateSecondSubFV);
else
score += subfv2.getScore(parameters, negateSecondSubFV);
}
}
// warning changed the the value
if (negate)
for (int i = 0; i < size; i++)
score -= parameters[m_index[i]];// *m_value[i];
else
for (int i = 0; i < size; i++)
score += parameters[m_index[i]];// *m_value[i];
return score;
}
final public int getScore(short[] parameters, boolean negate) {
int score = 0;
if (null != subfv1) {
score += subfv1.getScore(parameters, negate);
if (null != subfv2) {
if (negate)
score += subfv2.getScore(parameters, !negateSecondSubFV);
else
score += subfv2.getScore(parameters, negateSecondSubFV);
}
}
// warning changed the value
if (negate)
for (int i = 0; i < size; i++)
score -= parameters[m_index[i]];// *m_value[i];
else
for (int i = 0; i < size; i++)
score += parameters[m_index[i]];// *m_value[i];
return score;
}
public void update(double[] parameters, double[] total, double alpha_k, double upd) {
update(parameters, total, alpha_k, upd, false);
}
public final void update(double[] parameters, double[] total, double alpha_k, double upd, boolean negate) {
if (null != subfv1) {
subfv1.update(parameters, total, alpha_k, upd, negate);
if (null != subfv2) {
if (negate)
subfv2.update(parameters, total, alpha_k, upd, !negateSecondSubFV);
else
subfv2.update(parameters, total, alpha_k, upd, negateSecondSubFV);
}
}
if (negate) {
for (int i = 0; i < size; i++) {
parameters[m_index[i]] -= alpha_k;// *getValue(i);
total[m_index[i]] -= upd * alpha_k;// *getValue(i);
}
} else {
for (int i = 0; i < size; i++) {
parameters[m_index[i]] += alpha_k;// *getValue(i);
total[m_index[i]] += upd * alpha_k;// *getValue(i);
}
}
}
public final void update(short[] parameters, short[] total, double alpha_k, double upd, boolean negate) {
if (null != subfv1) {
subfv1.update(parameters, total, alpha_k, upd, negate);
if (null != subfv2) {
if (negate)
subfv2.update(parameters, total, alpha_k, upd, !negateSecondSubFV);
else
subfv2.update(parameters, total, alpha_k, upd, negateSecondSubFV);
}
}
if (negate) {
for (int i = 0; i < size; i++) {
parameters[m_index[i]] -= alpha_k;// *getValue(i);
total[m_index[i]] -= upd * alpha_k;// *getValue(i);
}
} else {
for (int i = 0; i < size; i++) {
parameters[m_index[i]] += alpha_k;// *getValue(i);
total[m_index[i]] += upd * alpha_k;// *getValue(i);
}
}
}
public final void update(float[] parameters, float[] total, double alpha_k, double upd, boolean negate) {
if (null != subfv1) {
subfv1.update(parameters, total, alpha_k, upd, negate);
if (null != subfv2 && negate) {
subfv2.update(parameters, total, alpha_k, upd, !negateSecondSubFV);
} else {
subfv2.update(parameters, total, alpha_k, upd, negateSecondSubFV);
}
}
if (negate) {
for (int i = 0; i < size; i++) {
parameters[getIndex(i)] -= alpha_k;
total[getIndex(i)] -= upd * alpha_k;
}
} else {
for (int i = 0; i < size; i++) {
parameters[getIndex(i)] += alpha_k;
total[getIndex(i)] += upd * alpha_k; //
}
}
}
public final void update(float[] parameters, float[] total, double alpha_k, double upd, boolean negate,
float[] totalp, Long2IntInterface li) {
if (null != subfv1) {
subfv1.update(parameters, total, alpha_k, upd, negate, totalp, li);
if (null != subfv2 && negate) {
subfv2.update(parameters, total, alpha_k, upd, !negateSecondSubFV, totalp, li);
} else {
subfv2.update(parameters, total, alpha_k, upd, negateSecondSubFV, totalp, li);
}
}
if (negate) {
for (int i = 0; i < size; i++) {
parameters[getIndex(i)] -= alpha_k;
total[getIndex(i)] -= upd * alpha_k;
totalp[li.l2i(getIndex(i))] -= upd * alpha_k;
// totalp[getIndex(i)] -=upd*alpha_k;
}
} else {
for (int i = 0; i < size; i++) {
parameters[getIndex(i)] += alpha_k;
total[getIndex(i)] += upd * alpha_k; //
totalp[li.l2i(getIndex(i))] += upd * alpha_k;
// totalp[getIndex(i)] +=upd*alpha_k;
}
}
}
private static IntIntHash hm1;
private static IntIntHash hm2;
public int dotProduct(FV fl2) {
if (hm1 == null)
hm1 = new IntIntHash(size(), 0.4F);
else
hm1.clear();
addFeaturesToMap(hm1);
if (hm2 == null)
hm2 = new IntIntHash(fl2.size, 0.4F);
else
hm2.clear();
fl2.addFeaturesToMap(hm2);
int[] keys = hm1.keys();
int result = 0;
for (int key : keys)
result += hm1.get(key) * hm2.get(key);
return result;
}
public double twoNorm(FV fl2) {
if (hm1 == null)
hm1 = new IntIntHash(size(), 0.4F);
else
hm1.clear();
addFeaturesToMap(hm1);
if (hm2 == null)
hm2 = new IntIntHash(fl2.size, 0.4F);
else
hm2.clear();
fl2.addFeaturesToMap(hm2);
int[] keys = hm1.keys();
int result = 0;
for (int key : keys)
result += hm1.get(key) * hm2.get(key);
return Math.sqrt(result);
}
public void addFeaturesToMap(IntIntHash map) {
if (null != subfv1) {
subfv1.addFeaturesToMap(map);
if (null != subfv2) {
subfv2.addFeaturesToMap(map, negateSecondSubFV);
}
}
for (int i = 0; i < size; i++)
if (!map.adjustValue(getIndex(i), 1))
map.put(getIndex(i), 1);
}
private void addFeaturesToMap(IntIntHash map, boolean negate) {
if (null != subfv1) {
subfv1.addFeaturesToMap(map, negate);
if (null != subfv2) {
if (negate)
subfv2.addFeaturesToMap(map, !negateSecondSubFV);
else
subfv2.addFeaturesToMap(map, negateSecondSubFV);
}
}
if (negate) {
for (int i = 0; i < size; i++)
if (!map.adjustValue(getIndex(i), -1))
map.put(getIndex(i), -1);
} else {
for (int i = 0; i < size; i++)
if (!map.adjustValue(getIndex(i), 1))
map.put(getIndex(i), 1);
}
}
@Override
public final String toString() {
StringBuilder sb = new StringBuilder();
toString(sb);
return sb.toString();
}
private final void toString(StringBuilder sb) {
if (null != subfv1) {
subfv1.toString(sb);
if (null != subfv2)
subfv2.toString(sb);
}
for (int i = 0; i < size; i++)
sb.append(getIndex(i)).append(' ');
}
public void writeKeys(DataOutputStream dos) throws IOException {
// int keys[] = keys();
// dos.writeInt(keys.length);
// for(int i=0;i<keys.length;i++) {
// dos.writeInt(keys[i]);
// }
// int keys[] = keys();
dos.writeInt(size);
for (int i = 0; i < size; i++) {
dos.writeInt(m_index[i]);
}
}
public void readKeys(DataInputStream dos) throws IOException {
int keys = dos.readInt();
for (int i = 0; i < keys; i++)
createFeature(dos.readInt(), 1.0);
}
final public static FV cat(FV f1, FV f2) {
if (f1 == null)
return f2;
if (f2 == null)
return f1;
return new FV(f1, f2);
}
final public static FV cat(FV f1, FV f2, FV f3) {
return FV.cat(f1, FV.cat(f2, f3));
}
final public static FV cat(FV f1, FV f2, FV f3, FV f4) {
return FV.cat(f1, FV.cat(f2, FV.cat(f3, f4)));
}
final public static FV read(DataInputStream dis) throws IOException {
int cap = dis.readInt();
if (cap == 0)
return null;
return new FV(dis, cap);
}
/*
* (non-Javadoc)
*
* @see is2.IFV#getScore()
*/
@Override
public double getScore() {
// System.out.println("not implemented");
// TODO Auto-generated method stub
return 0;
}
/*
* (non-Javadoc)
*
* @see is2.IFV#clone()
*/
@Override
public IFV clone() {
FV f = new FV(this.size);
for (int i = 0; i < this.size; i++) {
f.m_index[i] = m_index[i];
}
f.size = this.size;
return f;
}
}