Feat C++ API
A feature engineering automation tool
n_log.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_log.h"
7 
8 namespace FT{
9 
10  namespace Pop{
11  namespace Op{
12  NodeLog::NodeLog(vector<float> W0)
13  {
14  name = "log";
15  otype = 'f';
16  arity['f'] = 1;
17  complexity = 4;
18 
19  if (W0.empty())
20  {
21  for (int i = 0; i < arity['f']; i++) {
22  W.push_back(r.rnd_dbl());
23  }
24  }
25  else
26  W = W0;
27  }
28 
29  #ifndef USE_CUDA
31  void NodeLog::evaluate(const Data& data, State& state)
32  {
33  ArrayXf x = state.pop<float>();
34  state.push<float>( (abs(x) > NEAR_ZERO).select(log(abs(W[0] * x)),MIN_FLT));
35  }
36  #else
37  void NodeLog::evaluate(const Data& data, State& state)
38  {
39  GPU_Log(state.dev_f, state.idx[otype], state.N, W[0]);
40  }
41  #endif
42 
44  void NodeLog::eval_eqn(State& state)
45  {
46  state.push<float>("log(" + to_string(W[0], 4) + "*" + state.popStr<float>() + ")");
47  }
48 
49  ArrayXf NodeLog::getDerivative(Trace& state, int loc)
50  {
51  ArrayXf& x = state.get<float>()[state.size<float>()-1];
52 
53  switch (loc) {
54  case 1: // d/dw0
55  return limited(1/(W[0] * ArrayXf::Ones(x.size())));
56  case 0: // d/dx0
57  default:
58  return limited(1/x);
59  }
60  }
61 
62  NodeLog* NodeLog::clone_impl() const { return new NodeLog(*this); }
63 
64  NodeLog* NodeLog::rnd_clone_impl() const { return new NodeLog(); }
65  }
66  }
67 }
data holding X, y, and Z data
Definition: data.h:42
std::vector< float > W
Definition: n_Dx.h:16
void eval_eqn(State &state)
Evaluates the node symbolically.
Definition: n_log.cc:44
NodeLog * rnd_clone_impl() const override
Definition: n_log.cc:64
ArrayXf getDerivative(Trace &state, int loc)
Definition: n_log.cc:49
NodeLog(vector< float > W0=vector< float >())
Definition: n_log.cc:12
NodeLog * clone_impl() const override
Definition: n_log.cc:62
void evaluate(const Data &data, State &state)
Safe log: pushes log(abs(x)) or MIN_FLT if x is near zero.
Definition: n_log.cc:31
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_Log(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
static float NEAR_ZERO
Definition: init.h:46
static float MIN_FLT
Definition: init.h:48
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