Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ezexample_predict_threaded.cc « library - github.com/moses-smt/vowpal_wabbit.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0fa5b1e6bcf0b179c0312c5165b2c349f8933975 (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
#include <stdio.h>
#include "../vowpalwabbit/vw.h"
#include "../vowpalwabbit/ezexample.h"

#include <boost/thread/thread.hpp>

using namespace std;

int runcount = 100;

class Worker
{
public:
  Worker(vw & instance, string & vw_init_string, vector<double> & ref)
    : m_vw(instance)
    , m_referenceValues(ref)
    , vw_init_string(vw_init_string)
  { }

  void operator()()
  {
    m_vw_parser = VW::initialize(vw_init_string);
    if (m_vw_parser == NULL) {
      cerr << "cannot initialize vw parser" << endl;
      exit(-1);
    }

    int errorCount = 0;
    for (int i = 0; i < runcount; ++i)
    {
      vector<double>::iterator it = m_referenceValues.begin();
      ezexample ex(&m_vw, false, m_vw_parser);
      
      ex(vw_namespace('s'))
        ("p^the_man")
        ("w^the")
        ("w^man")
        (vw_namespace('t'))
        ("p^le_homme")
        ("w^le")
        ("w^homme");
      ex.set_label("1");
      if (*it != ex()) { cerr << "fail!" << endl; ++errorCount; }
      //if (*it != pred) { cerr << "fail!" << endl; ++errorCount; }
      //VW::finish_example(m_vw, vec2);
      ++it;

      --ex;   // remove the most recent namespace
      ex(vw_namespace('t'))
        ("p^un_homme")
        ("w^un")
        ("w^homme");
      ex.set_label("1");
      if (*it != ex()) { cerr << "fail!" << endl; ++errorCount; }
      ++it;

      --ex;   // remove the most recent namespace
      // add features with explicit ns
      ex('t', "p^un_homme")
        ('t', "w^un")
        ('t', "w^homme");
      ex.set_label("1");
      if (*it != ex()) { cerr << "fail!" << endl; ++errorCount; }
      ++it;

      //cout << "."; cout.flush();
    }
    cerr << "error count = " << errorCount << endl;
    VW::finish(*m_vw_parser);
    m_vw_parser = NULL;
  }

private:
  vw & m_vw;
  vw * m_vw_parser;
  vector<double> & m_referenceValues;
  string & vw_init_string;
};

int main(int argc, char *argv[])
{
  if (argc != 3)
  {
    cerr << "need two args: threadcount runcount" << endl;
    return 1;
  }
  int threadcount = atoi(argv[1]);
  runcount = atoi(argv[2]);
  // INITIALIZE WITH WHATEVER YOU WOULD PUT ON THE VW COMMAND LINE -- THIS READS IN A MODEL FROM train.w
  string vw_init_string_all    = "-t --ldf_override s --quiet -q st --noconstant --hash all -i train.w";
  string vw_init_string_parser = "-t --ldf_override s --quiet -q st --noconstant --hash all --noop";   // this needs to have enough arguments to get the parser right
  vw*vw = VW::initialize(vw_init_string_all);
  vector<double> results;

  // HAL'S SPIFFY INTERFACE USING C++ CRAZINESS
  {
    ezexample ex(vw, false);
    ex(vw_namespace('s'))
      ("p^the_man")
      ("w^the")
      ("w^man")
      (vw_namespace('t'))
      ("p^le_homme")
      ("w^le")
      ("w^homme");
    ex.set_label("1");
    results.push_back(ex.predict_partial());
    cerr << "should be near zero = " << ex.predict_partial() << endl;

    --ex;   // remove the most recent namespace
    ex(vw_namespace('t'))
      ("p^un_homme")
      ("w^un")
      ("w^homme");
    ex.set_label("1");
    results.push_back(ex.predict_partial());
    cerr << "should be near one  = " << ex.predict_partial() << endl;

    --ex;   // remove the most recent namespace
    // add features with explicit ns
    ex('t', "p^un_homme")
      ('t', "w^un")
      ('t', "w^homme");
    ex.set_label("1");
    results.push_back(ex.predict_partial());
    cerr << "should be near one  = " << ex.predict_partial() << endl;
  }

  if (threadcount == 0)
  {
    Worker w(*vw, vw_init_string_parser, results);
    w();
  }
  else
  {
    boost::thread_group tg;
    for (int t = 0; t < threadcount; ++t)
    {
      cerr << "starting thread " << t << endl;
      boost::thread * pt = tg.create_thread(Worker(*vw, vw_init_string_parser, results));
    }
    tg.join_all();
    cerr << "finished!" << endl;
  }


  // AND FINISH UP
  VW::finish(*vw);
}