Feat C++ API
A feature engineering automation tool
n_logit.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_logit.h"
6 
7 namespace FT{
8 
9  namespace Pop{
10  namespace Op{
11  NodeLogit::NodeLogit(vector<float> W0)
12  {
13  name = "logit";
14  otype = 'f';
15  arity['f'] = 1;
16  complexity = 4;
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 NodeLogit::evaluate(const Data& data, State& state)
31  {
32  state.push<float>(1/(1+(limited(exp(-W[0]*state.pop<float>())))));
33  }
34  #else
35  void NodeLogit::evaluate(const Data& data, State& state)
36  {
37  GPU_Logit(state.dev_f, state.idx[otype], state.N, W[0]);
38  }
39  #endif
40 
43  {
44  /* state.push<float>("1/(1+exp(-" + state.popStr<float>() + "))"); */
45  state.push<float>("logit(" + to_string(W[0], 4) + "*"+ state.popStr<float>() + ")");
46  }
47 
48  ArrayXf NodeLogit::getDerivative(Trace& state, int loc)
49  {
50  ArrayXf numerator, denom;
51 
52  ArrayXf& x = state.get<float>()[state.size<float>()-1];
53 
54  switch (loc) {
55  case 1: // d/dw0
56  numerator = x * exp(-W[0] * x);
57  denom = pow(1 + exp(-W[0] * x), 2);
58  return numerator/denom;
59  case 0: // d/dx0
60  default:
61  numerator = W[0] * exp(-W[0] * x);
62  denom = pow(1 + exp(-W[0] * x), 2);
63  return numerator/denom;
64  }
65  }
66 
67  NodeLogit* NodeLogit::clone_impl() const { return new NodeLogit(*this); }
68 
69  NodeLogit* NodeLogit::rnd_clone_impl() const { return new NodeLogit(); }
70  }
71  }
72 }
data holding X, y, and Z data
Definition: data.h:42
std::vector< float > W
Definition: n_Dx.h:16
NodeLogit(vector< float > W0=vector< float >())
Definition: n_logit.cc:11
NodeLogit * rnd_clone_impl() const override
Definition: n_logit.cc:69
NodeLogit * clone_impl() const override
Definition: n_logit.cc:67
void evaluate(const Data &data, State &state)
Evaluates the node and updates the state states.
Definition: n_logit.cc:30
ArrayXf getDerivative(Trace &state, int loc)
Definition: n_logit.cc:48
void eval_eqn(State &state)
Evaluates the node symbolically.
Definition: n_logit.cc:42
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_Logit(float *x, size_t idx, size_t N, float W0)
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