Feat C++ API
A feature engineering automation tool
n_relu.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_relu.h"
6 
7 namespace FT{
8 
9  namespace Pop{
10  namespace Op{
11  NodeRelu::NodeRelu(vector<float> W0)
12  {
13  name = "relu";
14  otype = 'f';
15  arity['f'] = 1;
16  arity['b'] = 0;
17  complexity = 2;
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 NodeRelu::evaluate(const Data& data, State& state)
32  {
33  ArrayXf x = state.pop<float>();
34  ArrayXf res = (W[0] * x > 0).select(W[0]*x, ArrayXf::Zero(x.size())+0.01);
35  state.push<float>(res);
36  }
37  #else
39  void NodeRelu::evaluate(const Data& data, State& state)
40  {
41  GPU_Relu(state.dev_f, state.idx[otype], state.N, (float)W[0]);
42  }
43  #endif
44 
47  {
48  state.push<float>("relu(" + to_string(W[0], 4) + "*" + state.popStr<float>() +")");
49  }
50 
51  ArrayXf NodeRelu::getDerivative(Trace& state, int loc)
52  {
53 
54  ArrayXf& x = state.get<float>()[state.size<float>()-1];
55 
56  switch (loc) {
57  case 1: // d/dW
58  return (x>0).select(x,ArrayXf::Zero(x.size())+0.01);
59  case 0: // d/dx
60  default:
61  return (x>0).select(W[0],ArrayXf::Zero(x.size())+0.01);
62  }
63  }
64 
65  NodeRelu* NodeRelu::clone_impl() const { return new NodeRelu(*this); }
66 
67  NodeRelu* NodeRelu::rnd_clone_impl() const { return new NodeRelu(); }
68  }
69  }
70 }
data holding X, y, and Z data
Definition: data.h:42
std::vector< float > W
Definition: n_Dx.h:16
ArrayXf getDerivative(Trace &state, int loc)
Definition: n_relu.cc:51
void eval_eqn(State &state)
Evaluates the node symbolically.
Definition: n_relu.cc:46
NodeRelu * clone_impl() const override
Definition: n_relu.cc:65
void evaluate(const Data &data, State &state)
Evaluates the node and updates the state states.
Definition: n_relu.cc:31
NodeRelu(vector< float > W0=vector< float >())
Definition: n_relu.cc:11
NodeRelu * rnd_clone_impl() const override
Definition: n_relu.cc:67
string name
node type
Definition: node.h:56
std::map< char, unsigned int > arity
arity of the operator
Definition: node.h:59
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_Relu(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