Feat C++ API
A feature engineering automation tool
n_Dx.cc
Go to the documentation of this file.
1 #include "n_Dx.h"
2 
3 namespace FT{
4 
5  namespace Pop{
6  namespace Op{
7 
9 
10  void NodeDx::derivative(vector<ArrayXf>& gradients, Trace& state, int loc)
11  {
12  gradients.push_back(getDerivative(state, loc));
13  }
14 
15  void NodeDx::update(vector<ArrayXf>& gradients, Trace& state, float n, float a)
16  {
23  if (V.empty()) // first time through, V is zeros
24  {
25  for (const auto& w : W)
26  V.push_back(0.0);
27  }
28  // std::cout << "***************************\n";
29  // std::cout << "Updating " << this->name << "\n";
30  ArrayXf update_value = ArrayXf::Ones(state.f[0].size());
31  for(const ArrayXf& g : gradients) {
32  /* std::cout << "Using gradient: " << g << "\n"; */
33  update_value *= g;
34  }
35 
36  // Update all weights
37  /* std::cout << "Update value: " << update_value << "\n"; */
38  /* std::cout << "Input: " << state.f[state.f.size() - 1] << "\n"; */
39  /* std::cout << "Input: " << state.f[state.f.size() - 2] << "\n"; */
40  vector<float> W_temp(W);
41  vector<float> V_temp(V);
42 
43  // cout << "*****n value is "<< n<<"\n";
44  // Have to use temporary weights so as not to compute updates with updated weights
45  for (int i = 0; i < arity['f']; ++i) {
46  ArrayXf d_w = getDerivative(state, arity['f'] + i);
47  /* std::cout << "Derivative: " << (d_w*update_value).sum() << "\n"; */
48  /* std::cout << "V[i]: " << V[i] << "\n"; */
49  // V_temp[i] = a * V[i] - n/update_value.size() * (d_w * update_value).sum();
50  V_temp[i] = - n/update_value.size() * (d_w * update_value).sum();
51  /* std::cout << "V_temp: " << V_temp[i] << "\n"; */
52  /* dW[i] = a*dW[i] + (1-a)*( n/update_value.size() * (d_w * update_value).sum()); */
53  /* W_temp[i] = W[i] + dW_temp[i]; */
54  /* W_temp[i] = W[i] - n/update_value.size() * (d_w * update_value).sum(); */
55  /* std::cout << "Updated with " << (d_w * update_value).sum() << "\n"; */
56  }
57  for (int i = 0; i < W.size(); ++i)
58  {
59  if (std::isfinite(V_temp[i]) && !std::isnan(V_temp[i]))
60  {
61  this->W[i] += V_temp[i];
62  this->V[i] = V_temp[i];
63  }
64  }
65 
66  // std::cout << "Updated\n";
67  // std::cout << "***************************\n";
68  // print_weight();
69  }
70 
72  {
73  std::cout << this->name << "|W has value";
74  for (int i = 0; i < this->arity['f']; i++) {
75  std::cout << " " << this->W[i];
76  }
77  std::cout << "\n";
78  }
79 
80  bool NodeDx::isNodeDx(){ return true;}
81  }
82  }
83 }
bool isNodeDx()
check of node type
Definition: n_Dx.cc:80
void print_weight()
Definition: n_Dx.cc:71
virtual ~NodeDx()
Definition: n_Dx.cc:8
void update(vector< ArrayXf > &gradients, Trace &state, float n, float a)
Definition: n_Dx.cc:15
std::vector< float > V
Definition: n_Dx.h:17
virtual ArrayXf getDerivative(Trace &state, int loc)=0
void derivative(vector< ArrayXf > &gradients, Trace &state, int loc)
Definition: n_Dx.cc:10
std::vector< float > W
Definition: n_Dx.h:16
string name
node type
Definition: node.h:56
std::map< char, unsigned int > arity
arity of the operator
Definition: node.h:59
ArrayXb isnan(const ArrayXf &x)
returns true for elements of x that are NaN
Definition: utils.cc:226
main Feat namespace
Definition: data.cc:13
int i
Definition: params.cc:552
used for tracing stack outputs for backprop algorithm.
Definition: state.h:232
vector< ArrayXf > f
Definition: state.h:233