SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
VowpalWabbit.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 Yahoo! Inc. All rights reserved. The copyrights
3  * embodied in the content of this file are licensed under the BSD
4  * (revised) open source license.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Written (W) 2011 Shashwat Lal Das
12  * Adaptation of Vowpal Wabbit v5.1.
13  * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society.
14  */
15 
16 #include <algorithm>
18 
19 using namespace std;
20 using namespace shogun;
21 
22 CVowpalWabbit::CVowpalWabbit()
24 {
25  reg=NULL;
26  learner=NULL;
27  init(NULL);
28 }
29 
32 {
33  reg=NULL;
34  learner=NULL;
35  init(feat);
36 }
37 
40 {
41  features = vw->features;
42  env = vw->env;
43  reg = new CVwRegressor(env);
44  SG_REF(env);
45  SG_REF(reg);
46 
47  quiet = vw->quiet;
48  no_training = vw->no_training;
49  dump_interval = vw->dump_interval;
50  sum_loss_since_last_dump = 0.;
51  reg_name = vw->reg_name;
52  reg_dump_text = vw->reg_dump_text;
53  save_predictions = vw->save_predictions;
54  prediction_fd = vw->prediction_fd;
55 
56  w = reg->weight_vectors[0];
57  copy(vw->w, vw->w+vw->w_dim, w);
58  w_dim = vw->w_dim;
59  bias = vw->bias;
60 }
61 
63 {
64  SG_UNREF(env);
65  SG_UNREF(reg);
67 }
68 
70 {
71  if (reg->weight_vectors)
72  {
73  if (reg->weight_vectors[0])
76  }
77 
78  reg->init(env);
79  w = reg->weight_vectors[0];
80 }
81 
82 void CVowpalWabbit::set_adaptive(bool adaptive_learning)
83 {
84  if (adaptive_learning)
85  {
86  env->adaptive = true;
87  env->set_stride(2);
88  env->power_t = 0.;
90  }
91  else
92  env->adaptive = false;
93 }
94 
95 void CVowpalWabbit::set_exact_adaptive_norm(bool exact_adaptive)
96 {
97  if (exact_adaptive)
98  {
99  set_adaptive(true);
100  env->exact_adaptive_norm = true;
101  }
102  else
103  env->exact_adaptive_norm = false;
104 }
105 
106 void CVowpalWabbit::load_regressor(char* file_name)
107 {
108  reg->load_regressor(file_name);
109  w = reg->weight_vectors[0];
110  w_dim = 1 << env->num_bits;
111 }
112 
113 void CVowpalWabbit::set_regressor_out(char* file_name, bool is_text)
114 {
115  reg_name = file_name;
116  reg_dump_text = is_text;
117 }
118 
120 {
121  save_predictions = true;
122  prediction_fd = open(file_name, O_CREAT|O_TRUNC|O_WRONLY, 0666);
123  if (prediction_fd < 0)
124  SG_SERROR("Unable to open prediction file %s for writing!\n", file_name);
125 }
126 
128 {
129  env->pairs.push_back(pair);
130 }
131 
133 {
134  ASSERT(features || feat);
135  if (feat && (features != (CStreamingVwFeatures*) feat))
136  {
138  init((CStreamingVwFeatures*) feat);
139  }
140 
141  set_learner();
142 
143  VwExample* example = NULL;
144  vw_size_t current_pass = 0;
145 
146  const char* header_fmt = "%-10s %-10s %8s %8s %10s %8s %8s\n";
147 
148  if (!quiet)
149  {
150  SG_SPRINT(header_fmt,
151  "average", "since", "example", "example",
152  "current", "current", "current");
153  SG_SPRINT(header_fmt,
154  "loss", "last", "counter", "weight", "label", "predict", "features");
155  }
156 
158  while (env->passes_complete < env->num_passes)
159  {
160  while (features->get_next_example())
161  {
162  example = features->get_example();
163 
164  // Check if we shouldn't train (generally used for cache creation)
165  if (!no_training)
166  {
167  if (example->pass != current_pass)
168  {
169  env->eta *= env->eta_decay_rate;
170  current_pass = example->pass;
171  }
172 
173  predict_and_finalize(example);
174 
175  learner->train(example, example->eta_round);
176  example->eta_round = 0.;
177 
178  output_example(example);
179  }
180 
182  }
183  env->passes_complete++;
186  }
187  features->end_parser();
188 
189  if (env->l1_regularization > 0.)
190  {
191  uint32_t length = 1 << env->num_bits;
192  vw_size_t stride = env->stride;
194  for (uint32_t i = 0; i < length; i++)
195  reg->weight_vectors[0][stride*i] = real_weight(reg->weight_vectors[0][stride*i], gravity);
196  }
197 
198  if (reg_name != NULL)
199  reg->dump_regressor(reg_name, reg_dump_text);
200 
201  return true;
202 }
203 
205 {
206  float32_t prediction;
207  if (env->l1_regularization != 0.)
208  prediction = inline_l1_predict(ex);
209  else
210  prediction = inline_predict(ex);
211 
212  ex->final_prediction = 0;
213  ex->final_prediction += prediction;
214  ex->final_prediction = finalize_prediction(ex->final_prediction);
215  float32_t t = ex->example_t;
216 
217  if (ex->ld->label != FLT_MAX)
218  {
219  ex->loss = reg->get_loss(ex->final_prediction, ex->ld->label) * ex->ld->weight;
220  float64_t update = 0.;
222  {
223  float32_t sum_abs_x = 0.;
224  float32_t exact_norm = compute_exact_norm(ex, sum_abs_x);
225  update = (env->eta * exact_norm)/sum_abs_x;
226  env->update_sum += update;
227  ex->eta_round = reg->get_update(ex->final_prediction, ex->ld->label, update, exact_norm);
228  }
229  else
230  {
231  update = (env->eta)/pow(t, env->power_t) * ex->ld->weight;
232  ex->eta_round = reg->get_update(ex->final_prediction, ex->ld->label, update, ex->total_sum_feat_sq);
233  }
234  env->update_sum += update;
235  }
236 
237  return prediction;
238 }
239 
240 void CVowpalWabbit::init(CStreamingVwFeatures* feat)
241 {
242  features = feat;
243  env = feat->get_env();
244  reg = new CVwRegressor(env);
245  SG_REF(env);
246  SG_REF(reg);
247 
248  quiet = true;
249  no_training = false;
250  dump_interval = exp(1.);
251  sum_loss_since_last_dump = 0.;
252  reg_name = NULL;
253  reg_dump_text = true;
254  save_predictions = false;
255  prediction_fd = -1;
256 
257  w = reg->weight_vectors[0];
258  w_dim = 1 << env->num_bits;
259  bias = 0.;
260 }
261 
263 {
264  if (env->adaptive)
266  else
268  SG_REF(learner);
269 }
270 
271 float32_t CVowpalWabbit::inline_l1_predict(VwExample* &ex)
272 {
273  vw_size_t thread_num = 0;
274 
275  float32_t prediction = ex->ld->get_initial();
276 
277  float32_t* weights = reg->weight_vectors[thread_num];
278  vw_size_t thread_mask = env->thread_mask;
279 
280  prediction += features->dense_dot_truncated(weights, ex, env->l1_regularization * env->update_sum);
281 
282  for (int32_t k = 0; k < env->pairs.get_num_elements(); k++)
283  {
284  char* i = env->pairs.get_element(k);
285 
286  v_array<VwFeature> temp = ex->atomics[(int32_t)(i[0])];
287  temp.begin = ex->atomics[(int32_t)(i[0])].begin;
288  temp.end = ex->atomics[(int32_t)(i[0])].end;
289  for (; temp.begin != temp.end; temp.begin++)
290  prediction += one_pf_quad_predict_trunc(weights, *temp.begin,
291  ex->atomics[(int32_t)(i[1])], thread_mask,
293  }
294 
295  return prediction;
296 }
297 
298 float32_t CVowpalWabbit::inline_predict(VwExample* &ex)
299 {
300  vw_size_t thread_num = 0;
301  float32_t prediction = ex->ld->initial;
302 
303  float32_t* weights = reg->weight_vectors[thread_num];
304  vw_size_t thread_mask = env->thread_mask;
305  prediction += features->dense_dot(weights, 0);
306 
307  for (int32_t k = 0; k < env->pairs.get_num_elements(); k++)
308  {
309  char* i = env->pairs.get_element(k);
310 
311  v_array<VwFeature> temp = ex->atomics[(int32_t)(i[0])];
312  temp.begin = ex->atomics[(int32_t)(i[0])].begin;
313  temp.end = ex->atomics[(int32_t)(i[0])].end;
314  for (; temp.begin != temp.end; temp.begin++)
315  prediction += one_pf_quad_predict(weights, *temp.begin,
316  ex->atomics[(int32_t)(i[1])],
317  thread_mask);
318  }
319 
320  return prediction;
321 }
322 
323 float32_t CVowpalWabbit::finalize_prediction(float32_t ret)
324 {
325  if (isnan(ret))
326  return 0.5;
327  if (ret > env->max_label)
328  return env->max_label;
329  if (ret < env->min_label)
330  return env->min_label;
331 
332  return ret;
333 }
334 
335 void CVowpalWabbit::output_example(VwExample* &example)
336 {
337  if (!quiet)
338  {
339  sum_loss_since_last_dump += example->loss;
340  if (env->weighted_examples + example->ld->weight > dump_interval)
341  {
342  print_update(example);
343  dump_interval *= 2;
344  }
345  }
346 
347  if (save_predictions)
348  {
349  float32_t wt = 0.;
350  if (reg->weight_vectors)
351  wt = reg->weight_vectors[0][0];
352 
353  output_prediction(prediction_fd, example->final_prediction, wt * example->global_weight, example->tag);
354  }
355 }
356 
357 void CVowpalWabbit::print_update(VwExample* &ex)
358 {
359  SG_SPRINT("%-10.6f %-10.6f %8lld %8.1f %8.4f %8.4f %8lu\n",
360  (env->sum_loss + ex->loss)/(env->weighted_examples + ex->ld->weight),
361  sum_loss_since_last_dump/(env->weighted_examples + ex->ld->weight - old_weighted_examples),
362  env->example_number + 1,
363  env->weighted_examples + ex->ld->weight,
364  ex->ld->label,
365  ex->final_prediction,
366  (long unsigned int)ex->num_features);
367  sum_loss_since_last_dump = 0.0;
368  old_weighted_examples = env->weighted_examples + ex->ld->weight;
369 }
370 
371 
372 void CVowpalWabbit::output_prediction(int32_t f, float32_t res, float32_t weight, v_array<char> tag)
373 {
374  if (f >= 0)
375  {
376  char temp[30];
377  int32_t num = sprintf(temp, "%f", res);
378  ssize_t t;
379  t = write(f, temp, num);
380  if (t != num)
381  SG_SERROR("Write error!\n");
382 
383  if (tag.begin != tag.end)
384  {
385  temp[0] = ' ';
386  t = write(f, temp, 1);
387  if (t != 1)
388  SG_SERROR("Write error!\n");
389 
390  t = write(f, tag.begin, sizeof(char)*tag.index());
391  if (t != (ssize_t) (sizeof(char)*tag.index()))
392  SG_SERROR("Write error!\n");
393  }
394 
395  temp[0] = '\n';
396  t = write(f, temp, 1);
397  if (t != 1)
398  SG_SERROR("Write error!\n");
399  }
400 }
401 
402 void CVowpalWabbit::set_verbose(bool verbose)
403 {
404  quiet=verbose==false;
405 }
406 
407 
409 {
410  // We must traverse the features in _precisely_ the same order as during training.
411  vw_size_t thread_mask = env->thread_mask;
412  vw_size_t thread_num = 0;
413 
415  if (g == 0) return 0.;
416 
417  float32_t xGx = 0.;
418 
419  float32_t* weights = reg->weight_vectors[thread_num];
420  for (vw_size_t* i = ex->indices.begin; i != ex->indices.end; i++)
421  {
422  for (VwFeature* f = ex->atomics[*i].begin; f != ex->atomics[*i].end; f++)
423  {
424  float32_t* w_vec = &weights[f->weight_index & thread_mask];
425  float32_t t = f->x * CMath::invsqrt(w_vec[1] + g * f->x * f->x);
426  xGx += t * f->x;
427  sum_abs_x += fabsf(f->x);
428  }
429  }
430 
431  for (int32_t k = 0; k < env->pairs.get_num_elements(); k++)
432  {
433  char* i = env->pairs.get_element(k);
434 
435  v_array<VwFeature> temp = ex->atomics[(int32_t)(i[0])];
436  temp.begin = ex->atomics[(int32_t)(i[0])].begin;
437  temp.end = ex->atomics[(int32_t)(i[0])].end;
438  for (; temp.begin != temp.end; temp.begin++)
439  xGx += compute_exact_norm_quad(weights, *temp.begin, ex->atomics[(int32_t)(i[1])], thread_mask, g, sum_abs_x);
440  }
441 
442  return xGx;
443 }
444 
446  vw_size_t mask, float32_t g, float32_t& sum_abs_x)
447 {
448  vw_size_t halfhash = quadratic_constant * page_feature.weight_index;
449  float32_t xGx = 0.;
450  float32_t update2 = g * page_feature.x * page_feature.x;
451  for (VwFeature* elem = offer_features.begin; elem != offer_features.end; elem++)
452  {
453  float32_t* w_vec = &weights[(halfhash + elem->weight_index) & mask];
454  float32_t t = elem->x * CMath::invsqrt(w_vec[1] + update2 * elem->x * elem->x);
455  xGx += t * elem->x;
456  sum_abs_x += fabsf(elem->x);
457  }
458  return xGx;
459 }

SHOGUN Machine Learning Toolbox - Documentation