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
|
#ifndef _DTRAIN_UPDATE_H_
#define _DTRAIN_UPDATE_H_
namespace dtrain
{
bool
_cmp(ScoredHyp a, ScoredHyp b)
{
return a.gold > b.gold;
}
bool
_cmpHope(ScoredHyp a, ScoredHyp b)
{
return (a.model+a.gold) > (b.model+b.gold);
}
bool
_cmpFear(ScoredHyp a, ScoredHyp b)
{
return (a.model-a.gold) > (b.model-b.gold);
}
inline bool
_good(ScoredHyp& a, ScoredHyp& b, weight_t margin)
{
if ((a.model-b.model)>margin
|| a.gold==b.gold)
return true;
return false;
}
inline bool
_goodS(ScoredHyp& a, ScoredHyp& b)
{
if (a.gold==b.gold)
return true;
return false;
}
/*
* multipartite ranking
* sort (descending) by bleu
* compare top X (hi) to middle Y (med) and low X (lo)
* cmp middle Y to low X
*/
inline size_t
CollectUpdates(vector<ScoredHyp>* s,
SparseVector<weight_t>& updates,
weight_t margin=0.)
{
size_t num_up = 0;
size_t sz = s->size();
sort(s->begin(), s->end(), _cmp);
size_t sep = round(sz*0.1);
for (size_t i = 0; i < sep; i++) {
for (size_t j = sep; j < sz; j++) {
if (_good((*s)[i], (*s)[j], margin))
continue;
updates += (*s)[i].f-(*s)[j].f;
num_up++;
}
}
size_t sep_lo = sz-sep;
for (size_t i = sep; i < sep_lo; i++) {
for (size_t j = sep_lo; j < sz; j++) {
if (_good((*s)[i], (*s)[j], margin))
continue;
updates += (*s)[i].f-(*s)[j].f;
num_up++;
}
}
return num_up;
}
inline size_t
CollectUpdatesStruct(vector<ScoredHyp>* s,
SparseVector<weight_t>& updates,
weight_t unused=-1)
{
// hope
sort(s->begin(), s->end(), _cmpHope);
ScoredHyp hope = (*s)[0];
// fear
sort(s->begin(), s->end(), _cmpFear);
ScoredHyp fear = (*s)[0];
if (!_goodS(hope, fear))
updates += hope.f - fear.f;
return updates.size();
}
} // namespace
#endif
|