Vowpal Wabbit
gd.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 #pragma once
7 #ifdef __FreeBSD__
8 #include <sys/socket.h>
9 #endif
10 
11 #include "parse_regressor.h"
12 #include "constant.h"
13 #include "interactions.h"
14 #include "array_parameters.h"
15 #include "gd_predict.h"
16 
17 namespace GD
18 {
20 
21 struct gd;
22 
23 float finalize_prediction(shared_data* sd, float ret);
24 void print_audit_features(vw&, example& ec);
25 void save_load_regressor(vw& all, io_buf& model_file, bool read, bool text);
26 void save_load_online_state(vw& all, io_buf& model_file, bool read, bool text, double& total_weight,
27  GD::gd* g = nullptr, uint32_t ftrl_size = 0);
28 
29 template <class T>
31 {
32  size_t count;
33  size_t step;
35  const T& weights; /* & for l1: */
36  float gravity;
37 };
38 
39 template <class T>
40 inline void vec_add_multipredict(multipredict_info<T>& mp, const float fx, uint64_t fi)
41 {
42  if ((-1e-10 < fx) && (fx < 1e-10))
43  return;
44  uint64_t mask = mp.weights.mask();
45  polyprediction* p = mp.pred;
46  fi &= mask;
47  uint64_t top = fi + (uint64_t)((mp.count - 1) * mp.step);
48  uint64_t i = 0;
49  if (top <= mask)
50  {
51  i += fi;
52  for (; i <= top; i += mp.step, ++p)
53  p->scalar +=
54  fx * mp.weights[i]; // TODO: figure out how to use weight_parameters::iterator (not using change_begin())
55  }
56  else // TODO: this could be faster by unrolling into two loops
57  for (size_t c = 0; c < mp.count; ++c, fi += (uint64_t)mp.step, ++p)
58  {
59  fi &= mask;
60  p->scalar += fx * mp.weights[fi];
61  }
62 }
63 
64 // iterate through one namespace (or its part), callback function T(some_data_R, feature_value_x, feature_weight)
65 template <class R, typename T>
66 inline void foreach_feature(vw& all, features& fs, R& dat, uint64_t offset = 0, float mult = 1.)
67 {
68  if (all.weights.sparse)
69  foreach_feature(all.weights.sparse_weights, fs, dat, offset, mult);
70  else
71  foreach_feature(all.weights.dense_weights, fs, dat, offset, mult);
72 }
73 
74 template <class R, class S, void (*T)(R&, float, S)>
75 inline void foreach_feature(vw& all, example& ec, R& dat)
76 {
77  return all.weights.sparse
78  ? foreach_feature<R, S, T, sparse_parameters>(all.weights.sparse_weights, all.ignore_some_linear,
79  all.ignore_linear, *ec.interactions, all.permutations, ec, dat)
80  : foreach_feature<R, S, T, dense_parameters>(all.weights.dense_weights, all.ignore_some_linear, all.ignore_linear,
81  *ec.interactions, all.permutations, ec, dat);
82 }
83 
84 // iterate through all namespaces and quadratic&cubic features, callback function T(some_data_R, feature_value_x,
85 // feature_weight)
86 template <class R, void (*T)(R&, float, float&)>
87 inline void foreach_feature(vw& all, example& ec, R& dat)
88 {
89  foreach_feature<R, float&, T>(all, ec, dat);
90 }
91 
92 template <class R, void (*T)(R&, float, const float&)>
93 inline void foreach_feature(vw& all, example& ec, R& dat)
94 {
95  foreach_feature<R, const float&, T>(all, ec, dat);
96 }
97 
98 inline float inline_predict(vw& all, example& ec)
99 {
100  return all.weights.sparse ? inline_predict<sparse_parameters>(all.weights.sparse_weights, all.ignore_some_linear,
101  all.ignore_linear, *ec.interactions, all.permutations, ec, ec.l.simple.initial)
102  : inline_predict<dense_parameters>(all.weights.dense_weights, all.ignore_some_linear,
103  all.ignore_linear, *ec.interactions, all.permutations, ec, ec.l.simple.initial);
104 }
105 
106 inline float sign(float w)
107 {
108  if (w < 0.)
109  return -1.;
110  else
111  return 1.;
112 }
113 
114 inline float trunc_weight(const float w, const float gravity)
115 {
116  return (gravity < fabsf(w)) ? w - sign(w) * gravity : 0.f;
117 }
118 
119 } // namespace GD
float finalize_prediction(shared_data *sd, float ret)
Definition: gd.cc:339
size_t count
Definition: gd.h:32
float gravity
Definition: gd.h:36
bool ignore_some_linear
Definition: global_data.h:464
parameters weights
Definition: global_data.h:537
base_learner * setup(options_i &options, vw &all)
Definition: gd.cc:1119
void print_audit_features(vw &all, example &ec)
Definition: gd.cc:331
float scalar
Definition: example.h:45
std::vector< std::string > * interactions
the core definition of a set of features.
void vec_add_multipredict(multipredict_info< T > &mp, const float fx, uint64_t fi)
Definition: gd.h:40
label_data simple
Definition: example.h:28
float sign(float w)
Definition: gd.h:106
size_t step
Definition: gd.h:33
float inline_predict(vw &all, example &ec)
Definition: gd.h:98
float trunc_weight(const float w, const float gravity)
Definition: gd.h:114
Definition: gd.cc:43
void save_load_online_state(vw &all, io_buf &model_file, bool read, bool text, gd *g, std::stringstream &msg, uint32_t ftrl_size, T &weights)
Definition: gd.cc:776
Definition: gd.cc:41
std::array< bool, NUM_NAMESPACES > ignore_linear
Definition: global_data.h:465
dense_parameters dense_weights
float initial
Definition: simple_label.h:16
Definition: io_buf.h:54
const T & weights
Definition: gd.h:35
polylabel l
Definition: example.h:57
sparse_parameters sparse_weights
void foreach_feature(vw &all, features &fs, R &dat, uint64_t offset=0, float mult=1.)
Definition: gd.h:66
bool permutations
Definition: global_data.h:454
polyprediction * pred
Definition: gd.h:34
void save_load_regressor(vw &all, io_buf &model_file, bool read, bool text, T &weights)
Definition: gd.cc:707
constexpr uint64_t c
Definition: rand48.cc:12