Feat C++ API
A feature engineering automation tool
n_ifthenelse.cc
Go to the documentation of this file.
1 /* FEAT
2 copyright 2017 William La Cava
3 license: GNU/GPL v3
4 */
5 
6 #include "n_ifthenelse.h"
7 
8 namespace FT{
9 
10 
11  namespace Pop{
12  namespace Op{
14  {
15  name = "ite";
16  otype = 'f';
17  arity['f'] = 2;
18  arity['b'] = 1;
19  complexity = 5;
20  W = {0.0, 0.0};
21  }
22 
23  #ifndef USE_CUDA
25  void NodeIfThenElse::evaluate(const Data& data, State& state)
26  {
27  ArrayXf f1 = state.pop<float>();
28  ArrayXf f2 = state.pop<float>();
29  state.push<float>(limited(state.pop<bool>().select(f1,f2)));
30  }
31  #else
32  void NodeIfThenElse::evaluate(const Data& data, State& state)
33  {
34  GPU_IfThenElse(state.dev_f, state.dev_b, state.idx[otype], state.idx['b'], state.N);
35  }
36  #endif
37 
40  {
41  state.push<float>("if-then-else(" + state.popStr<bool>() +
42  "," + state.popStr<float>() + "," +
43  state.popStr<float>() + ")");
44  }
45 
46  ArrayXf NodeIfThenElse::getDerivative(Trace& state, int loc)
47  {
48  ArrayXf& xf = state.get<float>()[state.size<float>()-1];
49  ArrayXb& xb = state.get<bool>()[state.size<bool>()-1];
50 
51  switch (loc) {
52  case 3: // d/dW[0]
53  case 2:
54  return ArrayXf::Zero(xf.size());
55  case 1: // d/dx2
56  return (!xb).cast<float>();
57  case 0: // d/dx1
58  default:
59  return xb.cast<float>();
60  /* .select(ArrayXf::Ones(state.f[state.f.size()-1].size(), */
61  /* ArrayXf::Zero(state.f[state.f.size()-1].size()); */
62  }
63  }
64 
65 
67 
69  }
70  }
71 }
data holding X, y, and Z data
Definition: data.h:42
std::vector< float > W
Definition: n_Dx.h:16
NodeIfThenElse * rnd_clone_impl() const override
Definition: n_ifthenelse.cc:68
void eval_eqn(State &state)
Evaluates the node symbolically.
Definition: n_ifthenelse.cc:39
NodeIfThenElse * clone_impl() const override
Definition: n_ifthenelse.cc:66
ArrayXf getDerivative(Trace &state, int loc)
Definition: n_ifthenelse.cc:46
void evaluate(const Data &data, State &state)
Evaluates the node and updates the state states.
Definition: n_ifthenelse.cc:25
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
Eigen::Array< bool, Eigen::Dynamic, 1 > ArrayXb
Definition: data.h:21
void GPU_IfThenElse(float *xf, bool *xb, size_t idxf, size_t idxb, size_t N)
main Feat namespace
Definition: data.cc:13
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