summaryrefslogtreecommitdiff
path: root/training/dtrain/update.h
blob: 405a3f76691dccea005a9d8cc8f01014c16c28d3 (plain)
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
#ifndef _DTRAIN_UPDATE_H_
#define _DTRAIN_UPDATE_H_

namespace dtrain
{

/*
 * multipartite [multi=3] ranking
 *  partitions are determined by the 'cut' parameter
 *   0. sort sample (descending) by bleu
 *   1. compare top X(=sz*cut) to middle Y(=sz-2*(sz*cut)) and bottom X
 *      -"- middle Y to bottom X
 *
 */
inline size_t
updates_multipartite(vector<Hyp>* sample,
                     SparseVector<weight_t>& updates,
                     weight_t cut,
                     weight_t margin,
                     size_t max_up,
                     weight_t threshold,
                     bool adjust,
                     WriteFile& output,
                     size_t id)
{
  size_t up = 0;
  size_t sz = sample->size();
  if (sz < 2) return 0;
  sort(sample->begin(), sample->end(), [](Hyp first, Hyp second)
    {
      return first.gold > second.gold;
    });
  size_t sep = round(sz*cut);

  size_t sep_hi = sep;
  if (adjust) {
    if (sz > 4) {
      while (sep_hi<sz && (*sample)[sep_hi-1].gold==(*sample)[sep_hi].gold)
        ++sep_hi;
    } else {
      sep_hi = 1;
    }
  }
  for (size_t i = 0; i < sep_hi; i++) {
    for (size_t j = sep_hi; j < sz; j++) {
      Hyp& first=(*sample)[i], second=(*sample)[j];
      if ((first.model-second.model)>margin
           || (first.gold==second.gold)
           || (threshold && (first.gold-second.gold < threshold)))
        continue;
      if (output)
        *output << id << "\t" << first.f-second.f << endl;
      updates += first.f-second.f;
      if (++up==max_up)
        return up;
    }
  }

  size_t sep_lo = sz-sep;
  if (adjust) {
    while (sep_lo>0 && (*sample)[sep_lo-1].gold==(*sample)[sep_lo].gold)
        --sep_lo;
  }
  for (size_t i = sep_hi; i < sep_lo; i++) {
    for (size_t j = sep_lo; j < sz; j++) {
      Hyp& first=(*sample)[i], second=(*sample)[j];
      if ((first.model-second.model)>margin
           || (first.gold==second.gold)
           || (threshold && (first.gold-second.gold < threshold)))
        continue;
      if (output)
        *output << id << "\t" << first.f-second.f << endl;
      updates += first.f-second.f;
      if (++up==max_up)
        break;
    }
  }

  return up;
}

/*
 * all pairs
 *  only ignore a pair if gold scores are
 *  identical
 *
 */
inline size_t
updates_all(vector<Hyp>* sample,
            SparseVector<weight_t>& updates,
            size_t max_up,
            weight_t threshold,
            WriteFile output,
            size_t id)
{
  size_t up = 0;
  size_t sz = sample->size();
  sort(sample->begin(), sample->end(), [](Hyp first, Hyp second)
    {
      return first.gold > second.gold;
    });
  for (size_t i = 0; i < sz-1; i++) {
    for (size_t j = i+1; j < sz; j++) {
      Hyp& first=(*sample)[i], second=(*sample)[j];
      if ((first.gold == second.gold)
           || (threshold && (first.gold-second.gold < threshold)))
        continue;
      if (output)
        *output << id << "\t" << first.f-second.f << endl;
      updates += first.f-second.f;
      if (++up==max_up)
        break;
    }
  }

  return up;
}

/*
 * hope/fear
 *  just one pair: hope - fear
 *
 */
inline size_t
update_structured(vector<Hyp>* sample,
                  SparseVector<weight_t>& updates,
                  weight_t margin,
                  WriteFile output,
                  size_t id)
{
  // hope
  sort(sample->begin(), sample->end(), [](Hyp first, Hyp second)
    {
      return (first.model+first.gold) > (second.model+second.gold);
    });
  Hyp hope = (*sample)[0];
  // fear
  sort(sample->begin(), sample->end(), [](Hyp first, Hyp second)
    {
      return (first.model-first.gold) > (second.model-second.gold);
    });
  Hyp fear = (*sample)[0];

  if (hope.gold != fear.gold) {
    updates += hope.f - fear.f;
    if (output)
      *output << id << "\t" << hope.f << "\t" << fear.f << endl;

    return 1;
  }

  if (output)
    *output << endl;

  return 0;
}


/*
 * pair sampling as in
 * 'Tuning as Ranking' (Hopkins & May, 2011)
 *     count = 5000    [maxs]
 * threshold = 5% BLEU [threshold=0.05]
 *       cut = top 50  [max_up]
 */
inline size_t
updates_pro(vector<Hyp>* sample,
           SparseVector<weight_t>& updates,
           size_t maxs,
           size_t max_up,
           weight_t threshold,
           WriteFile& output,
           size_t id)
{

  size_t sz = sample->size(), s;
  vector<pair<Hyp*,Hyp*> > g;
  while (s < maxs) {
    size_t i=rand()%sz, j=rand()%sz;
    Hyp& first=(*sample)[i], second=(*sample)[j];
    if (i==j || fabs(first.gold-second.gold)<threshold)
      continue;
    if (first.gold > second.gold)
      g.emplace_back(make_pair(&first,&second));
    else
      g.emplace_back(make_pair(&second,&first));
    s++;
  }

  if (g.size() > max_up) {
    sort(g.begin(), g.end(), [](pair<Hyp*,Hyp*> a, pair<Hyp*,Hyp*> b)
    {
      return fabs(a.first->gold-a.second->gold)
              > fabs(b.first->gold-b.second->gold);
    });
    g.erase(g.begin()+max_up, g.end());
  }

  for (auto i: g) {
    if (output)
      *output << id << "\t" << i.first->f-i.second->f << endl;
    updates += i.first->f-i.second->f;
  }

  return g.size();
}

/*
 * output (sorted) items in sample (k-best list)
 *
 */
inline void
output_sample(vector<Hyp>* sample,
              WriteFile& output,
              size_t id=0,
              bool sorted=true)
{
  if (sorted) {
    sort(sample->begin(), sample->end(), [](Hyp first, Hyp second)
      {
        return first.gold > second.gold;
      });
  }
  size_t j = 0;
  for (auto k: *sample) {
    *output << id << "\t" << j << "\t" << k.gold << "\t" << k.model
       << "\t" << k.f << endl;
    j++;
  }
}

} // namespace

#endif