summaryrefslogtreecommitdiff
path: root/training/dtrain/sample.h
blob: bccc29b7d2a06e471394bf90ab30ceae8e944b13 (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
#ifndef _DTRAIN_SAMPLE_H_
#define _DTRAIN_SAMPLE_H_

#include "kbest.h"
#include "hg_sampler.h"

#include "score.h"

namespace dtrain
{

struct HypSampler : public DecoderObserver
{
  size_t          feature_count, effective_size;
  vector<Hyp>     sample;
  vector<Ngrams>* reference_ngrams;
  vector<size_t>* reference_lengths;

  void
  reset()
  {
    sample.clear();
    effective_size = feature_count = 0;
  }
};

struct KBestSampler : public HypSampler
{
  size_t  k;
  bool    unique;
  Scorer* scorer;

  KBestSampler() {}
  KBestSampler(const size_t k, Scorer* scorer) :
    k(k), scorer(scorer) {}

  virtual void
  NotifyTranslationForest(const SentenceMetadata& /*smeta*/, Hypergraph* hg)
  {
    reset();
    KBest::KBestDerivations<vector<WordID>, ESentenceTraversal,
      KBest::FilterUnique, prob_t, EdgeProb> kbest(*hg, k);
    for (size_t i=0; i<k; ++i) {
      KBest::KBestDerivations<vector<WordID>, ESentenceTraversal,
        KBest::FilterUnique, prob_t, EdgeProb>::Derivation* d =
          kbest.LazyKthBest(hg->nodes_.size() - 1, i);
      if (!d) break;
      sample.emplace_back(
        d->yield,
        d->feature_values,
        log(d->score),
        scorer->score(d->yield, *reference_ngrams, *reference_lengths),
        i
      );
      effective_size++;
      feature_count += sample.back().f.size();
    }
  }
};

struct KBestNoFilterSampler : public KBestSampler
{
  size_t  k;
  bool    unique;
  Scorer* scorer;

  KBestNoFilterSampler(const size_t k, Scorer* scorer) :
    k(k), scorer(scorer) {}

  virtual void
  NotifyTranslationForest(const SentenceMetadata& /*smeta*/, Hypergraph* hg)
  {
    reset();
    KBest::KBestDerivations<vector<WordID>, ESentenceTraversal> kbest(*hg, k);
    for (size_t i=0; i<k; ++i) {
      const KBest::KBestDerivations<vector<WordID>, ESentenceTraversal>::Derivation* d =
        kbest.LazyKthBest(hg->nodes_.size() - 1, i);
      if (!d) break;
      sample.emplace_back(
        d->yield,
        d->feature_values,
        log(d->score),
        scorer->score(d->yield, *reference_ngrams, *reference_lengths),
        i
      );
      effective_size++;
      feature_count += sample.back().f.size();
    }
  }
};

struct KSampler : public HypSampler
{
  const size_t k;
  Scorer* scorer;
  MT19937 rng;

  explicit KSampler(const unsigned k, Scorer* scorer) :
    k(k), scorer(scorer) {}

  virtual void
  NotifyTranslationForest(const SentenceMetadata& /*smeta*/, Hypergraph* hg)
  {
    reset();
    std::vector<HypergraphSampler::Hypothesis> hs;
    HypergraphSampler::sample_hypotheses(*hg, k, &rng, &hs);
    for (size_t i=0; i<k; ++i) {
      sample.emplace_back(
        hs[i].words,
        hs[i].fmap,
        log(hs[i].model_score),
        0,
        0
      );
      effective_size++;
      feature_count += sample.back().f.size();
    }
    sort(sample.begin(), sample.end(), [](Hyp& first, Hyp& second) {
        return first.model > second.model;
      });
    for (unsigned i=0; i<sample.size(); i++) {
      sample[i].rank=i;
      scorer->score(sample[i].w, *reference_ngrams, *reference_lengths);
    }
  }
};

} // namespace

#endif