Vowpal Wabbit
example.h
Go to the documentation of this file.
1 /*
2 Copyright (c) by respective owners including Yahoo!, Microsoft, and
3 individual contributors. All rights reserved. Released under a BSD
4 license as described in the file LICENSE.
5  */
6 
7 #pragma once
8 
9 #include <cstdint>
10 #include "v_array.h"
11 #include "no_label.h"
12 #include "simple_label.h"
13 #include "multiclass.h"
14 #include "multilabel.h"
15 #include "cost_sensitive.h"
16 #include "cb.h"
17 #include "constant.h"
18 #include "feature_group.h"
19 #include "action_score.h"
20 #include "example_predict.h"
22 #include "ccb_label.h"
23 #include <vector>
24 
25 typedef union
26 {
35 } polylabel;
36 
37 inline void delete_scalars(void* v)
38 {
39  v_array<float>* preds = (v_array<float>*)v;
40  preds->delete_v();
41 }
42 
43 typedef union
44 {
45  float scalar;
46  v_array<float> scalars; // a sequence of scalar predictions
47  ACTION_SCORE::action_scores a_s; // a sequence of classes with scores. Also used for probabilities.
49  uint32_t multiclass;
51  float prob; // for --probabilities --csoaa_ldf=mc
53 
54 struct example : public example_predict // core example datatype.
55 {
56  // input fields
58 
59  // output prediction
61 
62  float weight; // a relative importance weight for the example, default = 1
63  v_array<char> tag; // An identifier for the example.
65 
66  // helpers
67  size_t num_features; // precomputed, cause it's fast&easy.
68  float partial_prediction; // shared data for prediction.
69  float updated_prediction; // estimated post-update prediction.
70  float loss;
71  float total_sum_feat_sq; // precomputed, cause it's kind of fast & easy.
72  float confidence;
73  features*
74  passthrough; // if a higher-up reduction wants access to internal state of lower-down reductions, they go here
75 
76  bool test_only;
77  bool end_pass; // special example indicating end of pass.
78  bool sorted; // Are the features sorted or not?
79  bool in_use; // in use or not (for the parser)
80 };
81 
82 struct vw;
83 
85 {
87 
88  size_t tag_len;
89  char* tag; // An identifier for the example.
90 
92  uint64_t ft_offset;
94 
95  size_t num_features; // precomputed, cause it's fast&easy.
96  float total_sum_feat_sq; // precomputed, cause it's kind of fast & easy.
97  features fs; // all the features
98 };
99 
103 
104 inline int example_is_newline(example const& ec)
105 { // if only index is constant namespace or no index
106  if (!ec.tag.empty())
107  return false;
108  return ((ec.indices.empty()) || ((ec.indices.size() == 1) && (ec.indices.last() == constant_namespace)));
109 }
110 
111 inline bool valid_ns(char c) { return !(c == '|' || c == ':'); }
112 
113 inline void add_passthrough_feature_magic(example& ec, uint64_t magic, uint64_t i, float x)
114 {
115  if (ec.passthrough)
116  ec.passthrough->push_back(x, (FNV_prime * magic) ^ i);
117 }
118 
119 #define add_passthrough_feature(ec, i, x) \
120  add_passthrough_feature_magic(ec, __FILE__[0] * 483901 + __FILE__[1] * 3417 + __FILE__[2] * 8490177, i, x);
121 
122 typedef std::vector<example*> multi_ex;
123 
124 namespace VW
125 {
126 void return_multiple_example(vw& all, v_array<example*>& examples);
127 } // namespace VW
float global_weight
Definition: example.h:93
void return_multiple_example(vw &all, v_array< example *> &examples)
Definition: example.cc:251
v_array< char > tag
Definition: example.h:63
v_array< namespace_index > indices
uint32_t multiclass
Definition: example.h:49
size_t example_counter
Definition: example.h:64
ACTION_SCORE::action_scores a_s
Definition: example.h:47
void push_back(feature_value v, feature_index i)
float scalar
Definition: example.h:45
float prob
Definition: example.h:51
CB::label cb
Definition: example.h:31
void free_flatten_example(flat_example *fec)
Definition: example.cc:190
bool sorted
Definition: example.h:78
float total_sum_feat_sq
Definition: example.h:96
the core definition of a set of features.
float confidence
Definition: example.h:72
CB_EVAL::label cb_eval
Definition: example.h:33
float partial_prediction
Definition: example.h:68
float updated_prediction
Definition: example.h:69
label_data simple
Definition: example.h:28
int example_is_newline(example const &ec)
Definition: example.h:104
polylabel l
Definition: example.h:86
features fs
Definition: example.h:97
size_t size() const
Definition: v_array.h:68
flat_example * flatten_sort_example(vw &all, example *ec)
Definition: example.cc:182
MULTICLASS::label_t multi
Definition: example.h:29
flat_example * flatten_example(vw &all, example *ec)
Definition: example.cc:152
COST_SENSITIVE::label cs
Definition: example.h:30
void delete_scalars(void *v)
Definition: example.h:37
char * tag
Definition: example.h:89
size_t num_features
Definition: example.h:67
void add_passthrough_feature_magic(example &ec, uint64_t magic, uint64_t i, float x)
Definition: example.h:113
CCB::decision_scores_t decision_scores
Definition: example.h:48
bool valid_ns(char c)
Definition: example.h:111
no_label::no_label empty
Definition: example.h:27
float loss
Definition: example.h:70
std::vector< example * > multi_ex
Definition: example.h:122
uint64_t ft_offset
Definition: example.h:92
size_t tag_len
Definition: example.h:88
polylabel l
Definition: example.h:57
MULTILABEL::labels multilabels
Definition: example.h:50
size_t num_features
Definition: example.h:95
bool in_use
Definition: example.h:79
float total_sum_feat_sq
Definition: example.h:71
MULTILABEL::labels multilabels
Definition: example.h:34
features * passthrough
Definition: example.h:74
Definition: cb.h:25
Definition: autolink.cc:11
bool empty() const
Definition: v_array.h:59
size_t example_counter
Definition: example.h:91
CCB::label conditional_contextual_bandit
Definition: example.h:32
char no_label
Definition: no_label.h:14
T last() const
Definition: v_array.h:57
constexpr uint32_t FNV_prime
Definition: constant.h:17
polyprediction pred
Definition: example.h:60
void delete_v()
Definition: v_array.h:98
constexpr unsigned char constant_namespace
Definition: constant.h:22
float weight
Definition: example.h:62
bool end_pass
Definition: example.h:77
v_array< float > scalars
Definition: example.h:46
constexpr uint64_t c
Definition: rand48.cc:12
bool test_only
Definition: example.h:76