Vowpal Wabbit
gd_predict.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 
8 #include "interactions_predict.h"
9 #include "v_array.h"
10 
11 namespace GD
12 {
13 // iterate through one namespace (or its part), callback function T(some_data_R, feature_value_x, feature_index)
14 template <class R, void (*T)(R&, float, uint64_t), class W>
15 void foreach_feature(W& /*weights*/, features& fs, R& dat, uint64_t offset = 0, float mult = 1.)
16 {
17  for (features::iterator& f : fs) T(dat, mult * f.value(), f.index() + offset);
18 }
19 
20 // iterate through one namespace (or its part), callback function T(some_data_R, feature_value_x, feature_weight)
21 template <class R, void (*T)(R&, const float, float&), class W>
22 inline void foreach_feature(W& weights, features& fs, R& dat, uint64_t offset = 0, float mult = 1.)
23 {
24  for (features::iterator& f : fs) T(dat, mult * f.value(), weights[(f.index() + offset)]);
25 }
26 
27 // iterate through one namespace (or its part), callback function T(some_data_R, feature_value_x, feature_weight)
28 template <class R, void (*T)(R&, const float, const float&), class W>
29 inline void foreach_feature(const W& weights, features& fs, R& dat, uint64_t offset = 0, float mult = 1.)
30 {
31  for (features::iterator& f : fs)
32  {
33  const weight& w = weights[(f.index() + offset)];
34  T(dat, mult * f.value(), w);
35  }
36 }
37 
38 template <class R>
39 inline void dummy_func(R&, const audit_strings*)
40 {
41 } // should never be called due to call_audit overload
42 
43 template <class R, class S, void (*T)(R&, float, S), class W> // nullptr func can't be used as template param in old
44  // compilers
45 inline void generate_interactions(std::vector<std::string>& interactions, bool permutations, example_predict& ec,
46  R& dat,
47  W& weights) // default value removed to eliminate
48  // ambiguity in old complers
49 {
50  INTERACTIONS::generate_interactions<R, S, T, false, dummy_func<R>, W>(interactions, permutations, ec, dat, weights);
51 }
52 
53 // iterate through all namespaces and quadratic&cubic features, callback function T(some_data_R, feature_value_x, S)
54 // where S is EITHER float& feature_weight OR uint64_t feature_index
55 template <class R, class S, void (*T)(R&, float, S), class W>
56 inline void foreach_feature(W& weights, bool ignore_some_linear, std::array<bool, NUM_NAMESPACES>& ignore_linear,
57  std::vector<std::string>& interactions, bool permutations, example_predict& ec, R& dat)
58 {
59  uint64_t offset = ec.ft_offset;
60  if (ignore_some_linear)
61  for (example_predict::iterator i = ec.begin(); i != ec.end(); ++i)
62  {
63  if (!ignore_linear[i.index()])
64  {
65  features& f = *i;
66  foreach_feature<R, T, W>(weights, f, dat, offset);
67  }
68  }
69  else
70  for (features& f : ec) foreach_feature<R, T, W>(weights, f, dat, offset);
71 
72  generate_interactions<R, S, T, W>(interactions, permutations, ec, dat, weights);
73 }
74 
75 inline void vec_add(float& p, const float fx, const float& fw) { p += fw * fx; }
76 
77 template <class W>
78 inline float inline_predict(W& weights, bool ignore_some_linear, std::array<bool, NUM_NAMESPACES>& ignore_linear,
79  std::vector<std::string>& interactions, bool permutations, example_predict& ec, float initial = 0.f)
80 {
81  foreach_feature<float, const float&, vec_add, W>(
82  weights, ignore_some_linear, ignore_linear, interactions, permutations, ec, initial);
83  return initial;
84 }
85 } // namespace GD
the core definition of a set of features.
void dummy_func(R &, const audit_strings *)
Definition: gd_predict.h:39
float inline_predict(vw &all, example &ec)
Definition: gd.h:98
Definition: gd.cc:41
void generate_interactions(std::vector< std::string > &interactions, bool permutations, example_predict &ec, R &dat, W &weights)
Definition: gd_predict.h:45
void vec_add(float &p, const float fx, const float &fw)
Definition: gd_predict.h:75
float weight
iterator over values and indicies
void foreach_feature(vw &all, features &fs, R &dat, uint64_t offset=0, float mult=1.)
Definition: gd.h:66
iterator begin()
float f
Definition: cache.cc:40
std::pair< std::string, std::string > audit_strings
Definition: feature_group.h:22