Feat C++ API
A feature engineering automation tool
auto_backprop.h
Go to the documentation of this file.
1 #ifndef AUTO_BACKPROP_H
2 #define AUTO_BACKPROP_H
3 
4 #include <Eigen/Dense>
5 #include <iostream>
6 #include <map>
7 #include <vector>
8 #include "../pop/nodevector.h"
9 #include "../dat/state.h"
10 #include "../pop/op/n_Dx.h"
11 #include "../eval/metrics.h"
12 #include "../pop/individual.h"
13 #include "../model/ml.h"
14 #include "../init.h"
15 #include "../params.h"
16 
17 #include <cmath>
18 #include <shogun/labels/Labels.h>
19 
20 using shogun::CLabels;
21 using Eigen::MatrixXf;
22 using Eigen::VectorXf;
23 typedef Eigen::Array<bool,Eigen::Dynamic,1> ArrayXb;
24 using std::cout;
33 namespace FT {
34 
39  namespace Opt{
40 
41  struct BP_NODE
42  {
44  vector<ArrayXf> deriv_list;
45  };
46 
47 
48  template <class T>
49  T pop(vector<T>* v) {
50  T value = v->back();
51  v->pop_back();
52  return value;
53  }
54 
55  template <class T>
56  T pop_front(vector<T>* v) {
57  T value = v->front();
58  v->erase(v->begin());
59  return value;
60  }
61 
62  class AutoBackProp
63  {
64  /* @class AutoBackProp
65  * @brief performs back propagation on programs to adapt weights.
66  */
67  public:
68 
69  typedef VectorXf (*callback)(const VectorXf&, shared_ptr<CLabels>&, const vector<float>&);
70 
71  std::map<string, callback> d_score_hash;
72  std::map<string, callback> score_hash;
73 
74  AutoBackProp(string scorer, int iters=1000, float n=0.1, float a=0.9);
75 
77  void run(Individual& ind, const Data& d,
78  const Parameters& params);
79 
80  /* ~AutoBackProp() */
81  /* { */
82  /* /1* for (const auto& p: program) *1/ */
83  /* /1* p = nullptr; *1/ */
84  /* } */
85 
86 
87  private:
88  float n; //< learning rate
89  float a; //< momentum
90  callback d_cost_func; //< derivative of cost function pointer
91  callback cost_func; //< cost function pointer
92 
93  int iters; //< iterations
94  float epk; //< current learning rate
95  float epT; //< min learning rate
96 
97  void print_weights(NodeVector& program);
98 
100  vector<Trace> forward_prop(Individual& ind, const Data& d,
101  MatrixXf& Phi, const Parameters& params);
102 
104  void next_branch(vector<BP_NODE>& executing, vector<Node*>& bp_program,
105  vector<ArrayXf>& derivatives);
106 
108  void backprop(Trace& f_stack, NodeVector& program, int start, int end,
109  float Beta, shared_ptr<CLabels>& yhat,
110  const Data& d,
111  vector<float> sw);
112 
114  void backprop2(Trace& f_stack, NodeVector& program, int start, int end,
115  float Beta, const VectorXf& yhat,
116  const Data& d,
117  vector<float> sw);
118 
119  };
120  }
121 }
122 
123 #endif
Eigen::Array< bool, Eigen::Dynamic, 1 > ArrayXb
Definition: auto_backprop.h:23
data holding X, y, and Z data
Definition: data.h:42
void run(Individual &ind, const Data &d, const Parameters &params)
adapt weights
std::map< string, callback > score_hash
Definition: auto_backprop.h:72
VectorXf(* callback)(const VectorXf &, shared_ptr< CLabels > &, const vector< float > &)
Definition: auto_backprop.h:69
vector< Trace > forward_prop(Individual &ind, const Data &d, MatrixXf &Phi, const Parameters &params)
Return the f_stack.
void backprop(Trace &f_stack, NodeVector &program, int start, int end, float Beta, shared_ptr< CLabels > &yhat, const Data &d, vector< float > sw)
Compute gradients and update weights.
std::map< string, callback > d_score_hash
Definition: auto_backprop.h:71
void next_branch(vector< BP_NODE > &executing, vector< Node * > &bp_program, vector< ArrayXf > &derivatives)
Updates stacks to have proper value on top.
void print_weights(NodeVector &program)
void backprop2(Trace &f_stack, NodeVector &program, int start, int end, float Beta, const VectorXf &yhat, const Data &d, vector< float > sw)
Compute gradients and update weights.
AutoBackProp(string scorer, int iters=1000, float n=0.1, float a=0.9)
individual programs in the population
Definition: individual.h:31
T pop(vector< T > *v)
Definition: auto_backprop.h:49
T pop_front(vector< T > *v)
Definition: auto_backprop.h:56
main Feat namespace
Definition: data.cc:13
used for tracing stack outputs for backprop algorithm.
Definition: state.h:232
vector< ArrayXf > deriv_list
Definition: auto_backprop.h:44
holds the hyperparameters for Feat.
Definition: params.h:25
an extension of a vector of unique pointers to nodes
Definition: nodevector.h:23