Feat C++ API
A feature engineering automation tool
n_2dgaussian.cc
Go to the documentation of this file.
1 /* FEAT
2 copyright 2017 William La Cava
3 license: GNU/GPL v3
4 */
5 
6 
7 #include "n_2dgaussian.h"
8 #include "../../../util/utils.h"
9 
10 namespace FT{
11 
12  namespace Pop{
13  namespace Op{
15  {
16  name = "gauss2d";
17  otype = 'f';
18  arity['f'] = 2;
19  complexity = 4;
20 
21  if (W0.empty())
22  {
23  for (int i = 0; i < arity['f']; i++) {
24  W.push_back(r.rnd_dbl());
25  }
26  }
27  else
28  W = W0;
29  }
30 
32  #ifndef USE_CUDA
33  void Node2dGaussian::evaluate(const Data& data, State& state)
34  {
35  ArrayXf x1 = state.pop<float>();
36  ArrayXf x2 = state.pop<float>();
37 
38  state.push<float>(limited(exp(-1*(pow(W[0]*(x1-x1.mean()), 2)/(2*variance(x1))
39  + pow(W[1]*(x2 - x2.mean()), 2)/variance(x2)))));
40  }
41  #else
43  void Node2dGaussian::evaluate(const Data& data, State& state)
44  {
45 
46  ArrayXf x1(state.N);
47  ArrayXf x2(state.N);
48 
49  state.copy_to_host(x1.data(), (state.idx['f']-1)*state.N);
50  state.copy_to_host(x2.data(), (state.idx['f']-2)*state.N);
51 
52  float x1mean = x1.mean();
53  float x2mean = x2.mean();
54 
55  float x1var = variance(x1);
56  float x2var = variance(x2);
57 
58  GPU_Gaussian2D(state.dev_f, state.idx[otype],
59  (float)x1mean, (float)x1var,
60  (float)x2mean, (float)x2var,
61  (float)W[0], (float)W[1], state.N);
62  }
63  #endif
64 
67  {
68  state.push<float>("gauss2d(" +state.popStr<float>()+ "," +state.popStr<float>()+ ")");
69  }
70 
71  ArrayXf Node2dGaussian::getDerivative(Trace& state, int loc)
72  {
73  ArrayXf& x = state.get<float>()[state.size<float>()-1];
74 
75  switch (loc) {
76  case 1: // d/dw0
77  return -2 * W[0] * pow(x, 2) * exp(-pow(W[0] * x, 2));
78  case 0: // d/dx0
79  default:
80  return -2 * pow(W[0], 2) * x * exp(-pow(W[0] * x, 2));
81  }
82  }
83 
84  Node2dGaussian* Node2dGaussian::clone_impl() const { return new Node2dGaussian(*this); }
85 
87  }
88  }
89 }
90 
91 
data holding X, y, and Z data
Definition: data.h:42
Node2dGaussian * clone_impl() const override
Definition: n_2dgaussian.cc:84
Node2dGaussian * rnd_clone_impl() const override
Definition: n_2dgaussian.cc:86
Node2dGaussian(vector< float > W0=vector< float >())
Definition: n_2dgaussian.cc:14
void evaluate(const Data &data, State &state)
Evaluates the node and updates the state states.
Definition: n_2dgaussian.cc:33
ArrayXf getDerivative(Trace &state, int loc)
Definition: n_2dgaussian.cc:71
void eval_eqn(State &state)
Evaluates the node symbolically.
Definition: n_2dgaussian.cc:66
std::vector< float > W
Definition: n_Dx.h:16
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_Gaussian2D(float *x, size_t idx, float x1mean, float x1var, float x2mean, float x2var, float W0, float W1, size_t N)
static Rnd & r
Definition: rnd.h:135
float variance(const ArrayXf &v, float mean)
calculate variance when mean provided
Definition: utils.cc:127
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