Feat C++ API
A feature engineering automation tool
state.h
Go to the documentation of this file.
1 /* FEAT
2 copyright 2017 William La Cava
3 license: GNU/GPL v3
4 */
5 
6 #ifndef STATE_H
7 #define STATE_H
8 
9 #ifdef USE_CUDA
10  #include "../pop/cuda-op/state_utils.h"
11 #endif
12 
13 #include <string>
14 #include <Eigen/Dense>
15 #include <vector>
16 #include <map>
17 #include <iostream>
18 
19 using std::vector;
20 using Eigen::MatrixXf;
21 using Eigen::VectorXf;
22 using Eigen::ArrayXf;
23 using Eigen::ArrayXi;
24 typedef Eigen::Array<bool,Eigen::Dynamic,1> ArrayXb;
25 using namespace std;
26 
27 //#include "node/node.h"
28 //external includes
29 
30 namespace FT
31 {
32  namespace Dat{
33  template<typename type>
38  class Stack
39  {
40  private:
41  std::vector<type> st;
42 
43  public:
44 
47  {
48  st = std::vector<type>();
49  }
50 
52  void push(type element){ st.push_back(element); }
53 
55  type pop()
56  {
57  type ret = st.back();
58  st.pop_back();
59  return ret;
60  }
61 
63  bool empty(){ return st.empty(); }
64 
66  unsigned int size(){ return st.size(); }
67 
69  type& top(){ return st.back(); }
70 
72  type& at(int i){ return st.at(i); }
73 
74  type& operator[](int i){ return at(i); }
75 
76  void resize(int i){ st.resize(i); };
77 
79  void clear(){ st.clear(); }
80 
82  typename vector<type>::iterator begin(){ return st.begin(); }
83 
85  typename vector<type>::iterator end(){ return st.end(); }
86 
88  typename vector<type>::const_iterator begin() const{ return st.begin(); }
89 
91  typename vector<type>::const_iterator end() const{ return st.end(); }
92 
93  ~Stack(){}
94  };
95 
96 #ifndef USE_CUDA
101  struct State
102  {
106  Stack<std::pair<vector<ArrayXf>, vector<ArrayXf> > > z;
111 
113  bool check(std::map<char, unsigned int> &arity);
114 
116  bool check_s(std::map<char, unsigned int> &arity);
117 
118  template <typename T> inline Stack<Eigen::Array<T,Eigen::Dynamic,1> >& get()
119  {
120  return get<Eigen::Array<T,Eigen::Dynamic,1> >();
121  }
122 
123  template <typename T> void push(Eigen::Array<T,Eigen::Dynamic,1> value)
124  {
125  get<T>().push(value);
126  }
127 
128  template <typename T> Eigen::Array<T,Eigen::Dynamic,1> pop()
129  {
130  return get<T>().pop();
131  }
132 
133  template <typename T> inline Stack<string>& getStr()
134  {
135  return getStr<T>();
136  }
137 
138  template <typename T> void push(string value)
139  {
140  getStr<T>().push(value);
141  }
142 
143  template <typename T> string popStr()
144  {
145  return getStr<T>().pop();
146  }
147 
148  template <typename T> unsigned int size()
149  {
150  return get<T>().size();
151  }
152 
153  };
154 
155  template <> inline Stack<ArrayXf>& State::get(){ return f; }
156 
157  template <> inline Stack<ArrayXb>& State::get(){ return b; }
158 
159  template <> inline Stack<ArrayXi>& State::get(){ return c; }
160 
161 #else
162 
163  struct State
164  {
165  Eigen::Array<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> f;
166  Eigen::Array<int, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> c;
167  Eigen::Array<bool, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> b;
168  Stack<std::pair<vector<ArrayXf>, vector<ArrayXf> > > z;
169  Stack<string> fs;
170  Stack<string> cs;
171  Stack<string> bs;
172  Stack<string> zs;
173 
174  float * dev_f;
175  int * dev_c;
176  bool * dev_b;
177  std::map<char, size_t> idx;
178  size_t N;
179 
180  State();
181 
182  void update_idx(char otype, std::map<char, unsigned>& arity);
183 
184  bool check(std::map<char, unsigned int> &arity);
185 
186  bool check_s(std::map<char, unsigned int> &arity);
187 
188  void allocate(const std::map<char, size_t>& stack_size, size_t N);
189 
190  void limit();
191 
193  void trim();
194 
195  void copy_to_host();
196 
197  void copy_to_host(float* host_f, int increment);
198 
199  void copy_to_host(int* host_f, int increment);
200 
201  ~State();
202 
203  template <typename T> inline Stack<string>& getStr()
204  {
205  return getStr<T>();
206  }
207 
208  template <typename T> void push(string value)
209  {
210  getStr<T>().push(value);
211  }
212 
213  template <typename T> string popStr()
214  {
215  return getStr<T>().pop();
216  }
217  };
218 
219 #endif
220 
221  template <> inline Stack<string>& State::getStr<float>(){ return fs; }
222 
223  template <> inline Stack<string>& State::getStr<bool>(){ return bs; }
224 
225  template <> inline Stack<string>& State::getStr<int>(){ return cs; }
226 
231  struct Trace
232  {
233  vector<ArrayXf> f;
234  vector<ArrayXi> c;
235  vector<ArrayXb> b;
236 
237  template <typename T> inline vector<Eigen::Array<T,Eigen::Dynamic,1> >& get()
238  {
239  return get<Eigen::Array<T,Eigen::Dynamic,1> >();
240  }
241 
242  template <typename T> unsigned int size()
243  {
244  return get<T>().size();
245  }
246 
247  void copy_to_trace(State& state, std::map<char, unsigned int> &arity);
248  };
249 
250  template <> inline vector<ArrayXf>& Trace::get(){ return f; }
251 
252  template <> inline vector<ArrayXb>& Trace::get(){ return b; }
253 
254  template <> inline vector<ArrayXi>& Trace::get(){ return c; }
255  }
256 }
257 
258 #endif
template stack class which holds various stack types for feat
Definition: state.h:39
type & operator[](int i)
Definition: state.h:74
type pop()
returns true or false depending on stack is empty or not
Definition: state.h:55
type & top()
returns element at particular location in stack
Definition: state.h:69
unsigned int size()
returns top element of stack
Definition: state.h:66
vector< type >::const_iterator begin() const
returns const iterator of stack
Definition: state.h:88
void clear()
clears the stack
Definition: state.h:79
std::vector< type > st
vector representing the stack
Definition: state.h:41
type & at(int i)
Definition: state.h:72
vector< type >::const_iterator end() const
Definition: state.h:91
bool empty()
returns size of stack
Definition: state.h:63
void resize(int i)
Definition: state.h:76
void push(type element)
pops element from back of vector and removes it
Definition: state.h:52
vector< type >::iterator begin()
returns end iterator of stack
Definition: state.h:82
vector< type >::iterator end()
returns const start iterator of stack
Definition: state.h:85
Stack()
< constructor initializing the vector
Definition: state.h:46
std::string trim(std::string str, const std::string &chars)
Definition: utils.cc:43
main Feat namespace
Definition: data.cc:13
int i
Definition: params.cc:552
Eigen::Array< bool, Eigen::Dynamic, 1 > ArrayXb
Definition: state.h:24
contains various types of State actually used by feat
Definition: state.h:102
Stack< ArrayXb > b
boolean node stack
Definition: state.h:104
Stack< string > fs
floating node string stack
Definition: state.h:107
Stack< string > zs
longitudinal node string stack
Definition: state.h:110
Stack< string > bs
boolean node string stack
Definition: state.h:108
Stack< std::pair< vector< ArrayXf >, vector< ArrayXf > > > z
longitudinal node stack
Definition: state.h:106
string popStr()
Definition: state.h:143
unsigned int size()
Definition: state.h:148
Stack< Eigen::Array< T, Eigen::Dynamic, 1 > > & get()
Definition: state.h:118
Eigen::Array< T, Eigen::Dynamic, 1 > pop()
Definition: state.h:128
Stack< string > cs
categorical node string stack
Definition: state.h:109
Stack< ArrayXi > c
categorical stack
Definition: state.h:105
Stack< string > & getStr()
Definition: state.h:133
Stack< ArrayXf > f
floating node stack
Definition: state.h:103
void push(string value)
Definition: state.h:138
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
vector< ArrayXf > f
Definition: state.h:233
vector< ArrayXb > b
Definition: state.h:235
unsigned int size()
Definition: state.h:242
vector< ArrayXi > c
Definition: state.h:234
vector< Eigen::Array< T, Eigen::Dynamic, 1 > > & get()
Definition: state.h:237