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


#include "kbestget.h"


namespace dtrain
{


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

typedef vector<TPair> TrainingInstances;


void
sample_all( KBestList* kb, TrainingInstances &training )
{
  for ( size_t i = 0; i < kb->GetSize()-1; i++ ) {
    for ( size_t j = i+1; j < kb->GetSize(); j++ ) {
      TPair 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 );
    }
  }
}

void
sample_rand( KBestList* kb, TrainingInstances &training )
{
  srand( time(NULL) );
  for ( size_t i = 0; i < kb->GetSize()-1; i++ ) {
    for ( size_t j = i+1; j < kb->GetSize(); j++ ) {
      if ( rand() % 2 ) {
        TPair 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 );
      }
    }
  }
}


} // namespace


#endif