summaryrefslogtreecommitdiff
path: root/dtrain/pairsampling.h
blob: a85214852ff30822c20dce7d8222f8fc464cc4d7 (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
#ifndef _DTRAIN_PAIRSAMPLING_H_
#define _DTRAIN_PAIRSAMPLING_H_

#include "kbestget.h"
#include "sampler.h" // cdec, MT19937

namespace dtrain
{


struct Pair
{
  SparseVector<double> first,       second;
  size_t               first_rank,  second_rank;
  double               first_score, second_score;
};

inline void
sample_all_pairs(Samples* kb, vector<Pair> &training)
{
  for (size_t i = 0; i < kb->GetSize()-1; i++) {
    for (size_t j = i+1; j < kb->GetSize(); j++) {
      Pair p;
      p.first = kb->feats[i];
      p.second = kb->feats[j];
      p.first_rank = i;
      p.second_rank = j;
      p.first_score = kb->scores[i];
      p.second_score = kb->scores[j];
      training.push_back(p);
    } // j
  } // i
}

inline void
sample_rand_pairs(Samples* kb, vector<Pair> &training, MT19937* prng)
{
  for (size_t i = 0; i < kb->GetSize()-1; i++) {
    for (size_t j = i+1; j < kb->GetSize(); j++) {
      if (prng->next() < .5) {
        Pair p;
        p.first = kb->feats[i];
        p.second = kb->feats[j];
        p.first_rank = i;
        p.second_rank = j;
        p.first_score = kb->scores[i];
        p.second_score = kb->scores[j];
        training.push_back(p);
      }
    } // j
  } // i
}


} // namespace

#endif