Feat C++ API
A feature engineering automation tool
n_multiply.cc
Go to the documentation of this file.
1 /* FEAT
2 copyright 2017 William La Cava
3 license: GNU/GPL v3
4 */
5 #include "n_multiply.h"
6 
7 namespace FT{
8 
9  namespace Pop{
10  namespace Op{
11  NodeMultiply::NodeMultiply(vector<float> W0)
12  {
13  name = "*";
14  otype = 'f';
15  arity['f'] = 2;
16  complexity = 2;
17 
18  if (W0.empty())
19  {
20  for (int i = 0; i < arity['f']; i++) {
21  W.push_back(r.rnd_dbl());
22  }
23  }
24  else
25  W = W0;
26  }
27 
28  #ifndef USE_CUDA
30  void NodeMultiply::evaluate(const Data& data, State& state)
31  {
32  ArrayXf x1 = state.pop<float>();
33  ArrayXf x2 = state.pop<float>();
34 
35  state.push<float>(limited(W[0]*x1 * W[1]*x2));
36  }
37  #else
38  void NodeMultiply::evaluate(const Data& data, State& state)
39  {
40  GPU_Multiply(state.dev_f, state.idx[otype], state.N, W[0], W[1]);
41  }
42  #endif
43 
46  {
47  state.push<float>("(" + to_string(W[0]*W[1], 4) + "*"
48  + state.popStr<float>() + "*" + state.popStr<float>() + ")");
49  }
50 
51  ArrayXf NodeMultiply::getDerivative(Trace& state, int loc)
52  {
53  ArrayXf& x1 = state.get<float>()[state.size<float>()-1];
54  ArrayXf& x2 = state.get<float>()[state.size<float>()-2];
55 
56  switch (loc) {
57  case 3: // d/dW[1]
58  return x1 * this->W[0] * x2;
59  case 2: // d/dW[0]
60  return x1 * this->W[1] * x2;
61  case 1: // d/dx2
62  return this->W[0] * this->W[1] * x1;
63  case 0: // d/dx1
64  default:
65  return this->W[1] * this->W[0] * x2;
66  }
67  }
68 
69  NodeMultiply* NodeMultiply::clone_impl() const { return new NodeMultiply(*this); }
70 
72 
73  }
74  }
75 }
data holding X, y, and Z data
Definition: data.h:42
std::vector< float > W
Definition: n_Dx.h:16
void evaluate(const Data &data, State &state)
Evaluates the node and updates the state states.
Definition: n_multiply.cc:30
NodeMultiply(vector< float > W0=vector< float >())
Definition: n_multiply.cc:11
ArrayXf getDerivative(Trace &state, int loc)
Definition: n_multiply.cc:51
NodeMultiply * rnd_clone_impl() const override
Definition: n_multiply.cc:71
void eval_eqn(State &state)
Evaluates the node symbolically.
Definition: n_multiply.cc:45
NodeMultiply * clone_impl() const override
Definition: n_multiply.cc:69
string name
node type
Definition: node.h:56
std::map< char, unsigned int > arity
arity of the operator
Definition: node.h:59
ArrayXf limited(ArrayXf x)
limits node output to be between MIN_FLT and MAX_FLT
Definition: node.cc:37
char otype
output type
Definition: node.h:58
int complexity
complexity of node
Definition: node.h:60
float rnd_dbl(float min=0.0, float max=1.0)
Definition: rnd.cc:89
void GPU_Multiply(float *x, size_t idx, size_t N, float W0, float W1)
static Rnd & r
Definition: rnd.h:135
std::string to_string(const T &value)
template function to convert objects to string for logging
Definition: utils.h:422
main Feat namespace
Definition: data.cc:13
int i
Definition: params.cc:552
contains various types of State actually used by feat
Definition: state.h:102
string popStr()
Definition: state.h:143
Eigen::Array< T, Eigen::Dynamic, 1 > pop()
Definition: state.h:128
void push(Eigen::Array< T, Eigen::Dynamic, 1 > value)
Definition: state.h:123
used for tracing stack outputs for backprop algorithm.
Definition: state.h:232
unsigned int size()
Definition: state.h:242
vector< Eigen::Array< T, Eigen::Dynamic, 1 > > & get()
Definition: state.h:237