Vowpal Wabbit
simple_label.cc
Go to the documentation of this file.
1 #include <cstring>
2 #include <float.h>
3 #include <math.h>
4 #include <stdio.h>
5 
6 #include "cache.h"
7 #include "accumulate.h"
8 #include "best_constant.h"
9 
11 {
12  memcpy(&ld->label, c, sizeof(ld->label));
13  // std::cout << ld->label << " " << sd->is_more_than_two_labels_observed << " " << sd->first_observed_label <<
14  // std::endl;
15  c += sizeof(ld->label);
16  memcpy(&ld->weight, c, sizeof(ld->weight));
17  c += sizeof(ld->weight);
18  memcpy(&ld->initial, c, sizeof(ld->initial));
19  c += sizeof(ld->initial);
20 
21  count_label(sd, ld->label);
22  return c;
23 }
24 
25 size_t read_cached_simple_label(shared_data* sd, void* v, io_buf& cache)
26 {
27  label_data* ld = (label_data*)v;
28  char* c;
29  size_t total = sizeof(ld->label) + sizeof(ld->weight) + sizeof(ld->initial);
30  if (cache.buf_read(c, total) < total)
31  return 0;
32  bufread_simple_label(sd, ld, c);
33 
34  return total;
35 }
36 
37 float get_weight(void* v)
38 {
39  label_data* ld = (label_data*)v;
40  return ld->weight;
41 }
42 
44 {
45  memcpy(c, &ld->label, sizeof(ld->label));
46  c += sizeof(ld->label);
47  memcpy(c, &ld->weight, sizeof(ld->weight));
48  c += sizeof(ld->weight);
49  memcpy(c, &ld->initial, sizeof(ld->initial));
50  c += sizeof(ld->initial);
51  return c;
52 }
53 
54 void cache_simple_label(void* v, io_buf& cache)
55 {
56  char* c;
57  label_data* ld = (label_data*)v;
58  cache.buf_write(c, sizeof(ld->label) + sizeof(ld->weight) + sizeof(ld->initial));
59  bufcache_simple_label(ld, c);
60 }
61 
62 void default_simple_label(void* v)
63 {
64  label_data* ld = (label_data*)v;
65  ld->label = FLT_MAX;
66  ld->weight = 1.;
67  ld->initial = 0.;
68 }
69 
70 bool test_label(void* v)
71 {
72  label_data* ld = (label_data*)v;
73  return ld->label == FLT_MAX;
74 }
75 
76 void delete_simple_label(void*) {}
77 
79 {
80  label_data* ld = (label_data*)v;
81 
82  switch (words.size())
83  {
84  case 0:
85  break;
86  case 1:
87  ld->label = float_of_substring(words[0]);
88  break;
89  case 2:
90  ld->label = float_of_substring(words[0]);
91  ld->weight = float_of_substring(words[1]);
92  break;
93  case 3:
94  ld->label = float_of_substring(words[0]);
95  ld->weight = float_of_substring(words[1]);
96  ld->initial = float_of_substring(words[2]);
97  break;
98  default:
99  std::cout << "Error: " << words.size() << " is too many tokens for a simple label: ";
100  for (unsigned int i = 0; i < words.size(); ++i) print_substring(words[i]);
101  std::cout << std::endl;
102  }
103  count_label(sd, ld->label);
104 }
105 
108 
109 void print_update(vw& all, example& ec)
110 {
112  !all.bfgs)
113  {
115  all.progress_add, all.progress_arg);
116  }
117 }
118 
120 {
121  label_data ld = ec.l.simple;
122 
123  all.sd->update(ec.test_only, ld.label != FLT_MAX, ec.loss, ec.weight, ec.num_features);
124  if (ld.label != FLT_MAX && !ec.test_only)
125  all.sd->weighted_labels += ((double)ld.label) * ec.weight;
126 
127  all.print(all.raw_prediction, ec.partial_prediction, -1, ec.tag);
128  for (size_t i = 0; i < all.final_prediction_sink.size(); i++)
129  {
130  int f = (int)all.final_prediction_sink[i];
131  all.print(f, ec.pred.scalar, 0, ec.tag);
132  }
133 
134  print_update(all, ec);
135 }
136 
137 void return_simple_example(vw& all, void*, example& ec)
138 {
140  VW::finish_example(all, ec);
141 }
142 
143 bool summarize_holdout_set(vw& all, size_t& no_win_counter)
144 {
145  float thisLoss = (all.sd->weighted_holdout_examples_since_last_pass > 0)
147  : FLT_MAX * 0.5f;
148  if (all.all_reduce != nullptr)
149  thisLoss = accumulate_scalar(all, thisLoss);
150 
153 
154  if (thisLoss < all.sd->holdout_best_loss)
155  {
156  all.sd->holdout_best_loss = thisLoss;
157  all.sd->holdout_best_pass = all.current_pass;
158  no_win_counter = 0;
159  return true;
160  }
161 
162  if ((thisLoss != FLT_MAX) ||
163  (std::isfinite(
164  all.sd->holdout_best_loss))) // it's only a loss if we're not infinite when the previous one wasn't infinite
165  no_win_counter++;
166  return false;
167 }
float get_weight(void *v)
Definition: simple_label.cc:37
v_array< char > tag
Definition: example.h:63
int raw_prediction
Definition: global_data.h:519
label_parser simple_label
double holdout_sum_loss_since_last_pass
Definition: global_data.h:163
float scalar
Definition: example.h:45
double weighted_unlabeled_examples
Definition: global_data.h:143
void count_label(shared_data *sd, float l)
Definition: best_constant.h:5
v_array< int > final_prediction_sink
Definition: global_data.h:518
double holdout_best_loss
Definition: global_data.h:161
float partial_prediction
Definition: example.h:68
bool quiet
Definition: global_data.h:487
float weight
Definition: simple_label.h:15
void default_simple_label(void *v)
Definition: simple_label.cc:62
void parse_simple_label(parser *, shared_data *sd, void *v, v_array< substring > &words)
Definition: simple_label.cc:78
float label
Definition: simple_label.h:14
label_data simple
Definition: example.h:28
bool holdout_set_off
Definition: global_data.h:499
bool summarize_holdout_set(vw &all, size_t &no_win_counter)
bool progress_add
Definition: global_data.h:545
char * bufcache_simple_label(label_data *ld, char *c)
Definition: simple_label.cc:43
size_t size() const
Definition: v_array.h:68
AllReduce * all_reduce
Definition: global_data.h:381
float float_of_substring(substring s)
void delete_simple_label(void *)
Definition: simple_label.cc:76
shared_data * sd
Definition: global_data.h:375
float progress_arg
Definition: global_data.h:546
void output_and_account_example(vw &all, example &ec)
void print_update(bool holdout_set_off, size_t current_pass, float label, float prediction, size_t num_features, bool progress_add, float progress_arg)
Definition: global_data.h:225
bool bfgs
Definition: global_data.h:412
size_t num_features
Definition: example.h:67
double weighted_holdout_examples_since_last_pass
Definition: global_data.h:162
double weighted_labels
Definition: global_data.h:144
char * bufread_simple_label(shared_data *sd, label_data *ld, char *c)
Definition: simple_label.cc:10
uint64_t current_pass
Definition: global_data.h:396
float initial
Definition: simple_label.h:16
Definition: io_buf.h:54
void finish_example(vw &, example &)
Definition: parser.cc:881
void print_update(vw &all, example &ec)
void update(bool test_example, bool labeled_example, float loss, float weight, size_t num_features)
Definition: global_data.h:190
float loss
Definition: example.h:70
void buf_write(char *&pointer, size_t n)
Definition: io_buf.cc:94
polylabel l
Definition: example.h:57
float accumulate_scalar(vw &all, float local_sum)
Definition: accumulate.cc:44
bool test_label(void *v)
Definition: simple_label.cc:70
double weighted_labeled_examples
Definition: global_data.h:141
Definition: parser.h:38
polyprediction pred
Definition: example.h:60
void print_substring(substring s)
size_t read_cached_simple_label(shared_data *sd, void *v, io_buf &cache)
Definition: simple_label.cc:25
float weight
Definition: example.h:62
float dump_interval
Definition: global_data.h:147
constexpr uint64_t c
Definition: rand48.cc:12
void cache_simple_label(void *v, io_buf &cache)
Definition: simple_label.cc:54
void(* print)(int, float, float, v_array< char >)
Definition: global_data.h:521
size_t holdout_best_pass
Definition: global_data.h:164
float f
Definition: cache.cc:40
bool test_only
Definition: example.h:76
void return_simple_example(vw &all, void *, example &ec)
size_t buf_read(char *&pointer, size_t n)
Definition: io_buf.cc:12