BlackCat_Tensors
A GPU-supported autograd and linear algebra library, designed for neural network construction
layer_base.h
Go to the documentation of this file.
1  /*
2  * Layer.h
3  *
4  * Created on: Mar 1, 2018
5  * Author: joseph
6  */
7 
8 #ifndef LAYER_H_
9 #define LAYER_H_
10 
11 #include "../layer_loader.h"
12 #include "layer_traits.h"
13 #include <string>
14 
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fstream>
18 #include <ostream>
19 
20 namespace bc {
21 namespace nn {
22 
23 template<class OutputTensorDescriptor>
25 
26 template<class OutputTensorDescriptor>
28 {
29  using output_value_type = typename OutputTensorDescriptor::value_type;
30  using output_system_tag = typename OutputTensorDescriptor::system_tag;
31  using output_allocator_type = typename OutputTensorDescriptor::allocator_type;
32  using output_tensor_dim = typename OutputTensorDescriptor::tensor_dim;
34  using output_tensor_type = typename OutputTensorDescriptor::type;
35  using batched_output_tensor_type = typename OutputTensorDescriptor::batched_type;
37 
38 private:
40 
41 protected:
44 
45 public:
46 // virtual const output_tensor_type& get_output() const=0;
47  void set_next(next_layer_type& next) { m_next_layer = &next; }
49  const next_layer_type& next_layer() const { return *m_next_layer; }
51  virtual ~Layer_Output_Base() {}
52 };
53 
54 template<class InputTensorDescriptor>
55 class Layer_Input_Base
56 {
57  using input_value_type = typename InputTensorDescriptor::value_type;
58  using input_system_tag = typename InputTensorDescriptor::system_tag;
59  using input_allocator_type = typename InputTensorDescriptor::allocator_type;
60  using input_tensor_dim = typename InputTensorDescriptor::tensor_dim;
61  using input_shape_type = bc::Dim<input_tensor_dim::value>;
62  using input_tensor_type = typename InputTensorDescriptor::type;
63  using batched_input_tensor_type = typename InputTensorDescriptor::batched_type;
64  using prev_layer_type = Layer_Output_Base<InputTensorDescriptor>;
65 private:
67 
68 protected:
71 
72 public:
73 // virtual const input_tensor_type& get_input() const=0;
74  void set_prev(prev_layer_type& prev) { m_prev_layer = &prev; }
75  input_shape_type input_shape() const { return m_input_shape; }
76  const prev_layer_type& prev_layer() const { return *m_prev_layer; }
77  prev_layer_type& prev_layer() { return *m_prev_layer; }
78  virtual ~Layer_Input_Base() {}
79 };
80 
81 
82 template<
83  class DerivedLayer,
84  class InputTensorDescriptor,
85  class OutputTensorDescriptor=InputTensorDescriptor>
86 struct Layer_Base:
87  Layer_Input_Base<InputTensorDescriptor>,
88  Layer_Output_Base<OutputTensorDescriptor>
89 {
90  using value_type = typename InputTensorDescriptor::value_type;
91  using system_tag = typename InputTensorDescriptor::system_tag;
92  using allocator_type = typename InputTensorDescriptor::allocator_type;
93  using input_tensor_dim = typename InputTensorDescriptor::tensor_dim;
94 
96  using input_tensor_type = typename InputTensorDescriptor::type;
97  using batched_input_tensor_type = typename InputTensorDescriptor::batched_type;
98 
99  using output_value_type = typename OutputTensorDescriptor::value_type;
100  using output_system_tag = typename OutputTensorDescriptor::system_tag;
101  using output_allocator_type = typename OutputTensorDescriptor::allocator_type;
102  using output_tensor_dim = typename OutputTensorDescriptor::tensor_dim;
104  using output_tensor_type = typename OutputTensorDescriptor::type;
105  using batched_output_tensor_type = typename OutputTensorDescriptor::batched_type;
106 
107  static constexpr value_type default_learning_rate = .01;
108 
109 private:
111 
112  value_type m_learning_rate = default_learning_rate;
113  bc::size_t m_batch_sz=1;
114  std::string m_classname;
115 
116 protected:
119 
120 public:
127  std::string classname,
128  shape_type input_shape,
130  m_classname(parse_classname(classname)),
131  m_input_shape(input_shape),
132  m_output_shape(output_shape) {
133  }
134 
135  Layer_Base(std::string classname, shape_type input_shape):
136  m_classname(parse_classname(classname)),
137  m_input_shape(input_shape) {}
138 
139  virtual ~Layer_Base() {}
140 
142  virtual shape_type get_input_shape() const { return m_input_shape; }
143 
144  auto get_batched_input_shape() const { return m_input_shape.concat(m_batch_sz); }
145  auto get_batched_output_shape() const { return m_output_shape.concat(m_batch_sz); }
146 
147  bc::size_t input_size() const { return this->m_input_shape.prod(); }
148  bc::size_t output_size() const { return this->m_output_shape.prod(); }
149  bc::size_t batch_size() const { return m_batch_sz; }
150 
151  bc::size_t batched_input_size() const { return input_size() * m_batch_sz; }
152  bc::size_t batched_output_size() const { return output_size() * m_batch_sz; }
153 
154  void set_batch_size(int batch_size)
155  {
156  m_batch_sz = batch_size;
157  set_batch_size_hook(batch_size);
158  }
159 
160  virtual void set_batch_size_hook(int batch_size) {}
161 
162  void set_learning_rate(value_type learning_rate)
163  {
164  m_learning_rate = learning_rate;
165  set_learning_rate_hook(learning_rate);
166  }
167 
168  virtual void set_learning_rate_hook(value_type learning_rate) {}
169 
170  auto get_learning_rate() const { return m_learning_rate; }
171  auto get_batched_learning_rate() const { return m_learning_rate / m_batch_sz; }
172 
173  virtual void save(Layer_Loader&) const {};
174  virtual void save_from_cache(Layer_Loader&, const Cache&) const {}
175  virtual void load(Layer_Loader&) {};
176  virtual void load_to_cache(Layer_Loader&, const Cache&) {}
177 
178  void copy_training_data_to_single_predict(Cache&, int batch_index) {}
179  void update_weights() {}
181 
185  const std::string& classname() const { return m_classname; }
186 private:
187  template<class T>
188  using query_optimizer_type = typename T::optimizer_type;
189 
190 public:
191  template<int ADL=0>
192  std::string get_string_architecture() const {
193  std::string yaml = classname() + ':'
194  + "\n\tinput_shape: " + get_input_shape().to_string();
195 
196  using optimizer_type = bc::traits::conditional_detected_t<
197  query_optimizer_type, DerivedLayer, bc::traits::None>;
198 
199  if (!std::is_same<optimizer_type, bc::traits::None>::value) {
200  auto opt_name = bc_get_classname_of(optimizer_type());
201  yaml += "\n\toptimizer: ";
202  yaml += parse_classname(opt_name);
203  }
204 
205  std::string other_features = get_string_architecture_hook();
206  if (other_features != "")
207  yaml += "\n\t" + other_features;
208 
209  return yaml;
210  }
211 
212  virtual std::string get_string_architecture_hook() const { return ""; }
213 
214 
215  static std::string parse_classname(std::string classname) {
216  auto classname_ns = std::find(classname.rbegin(), classname.rend(), ':');
217  classname.erase(classname.rend().base(), classname_ns.base());
218  return classname;
219  }
220 
222  return [&]() {
223  return input_tensor_type(get_input_shape()).zero();
224  };
225  }
226 
228  return [&]() {
229  return output_tensor_type(get_output_shape()).zero();
230  };
231  }
232 
234  return [&]() {
235  return batched_input_tensor_type(
236  get_batched_input_shape()).zero();
237  };
238  }
239 
241  return [&]() {
243  get_batched_output_shape()).zero();
244  };
245  }
246 };
247 
248 }
249 }
250 
251 
252 
253 #endif /* LAYER_H_ */
virtual shape_type get_input_shape() const
Definition: layer_base.h:142
typename Tensor_Descriptor< ValueType, SystemTag, Integer< 3 > > ::tensor_dim output_tensor_dim
Definition: layer_base.h:32
const next_layer_type & next_layer() const
Definition: layer_base.h:49
auto get_learning_rate() const
Definition: layer_base.h:170
virtual std::string get_string_architecture_hook() const
Definition: layer_base.h:212
const char * bc_get_classname_of(const T &arg)
Definition: common.h:330
auto default_output_tensor_factory() const
Definition: layer_base.h:227
virtual ~Layer_Input_Base()
Definition: layer_base.h:78
typename Tensor_Descriptor< ValueType, SystemTag, Integer< 3 > > ::type output_tensor_type
Definition: layer_base.h:34
std::string get_string_architecture() const
Definition: layer_base.h:192
next_layer_type & next_layer()
Definition: layer_base.h:50
static std::string parse_classname(std::string classname)
Definition: layer_base.h:215
prev_layer_type & prev_layer()
Definition: layer_base.h:77
void set_batch_size(int batch_size)
Definition: layer_base.h:154
void set_next(next_layer_type &next)
Definition: layer_base.h:47
input_shape_type m_input_shape
Definition: layer_base.h:70
Definition: layer_base.h:86
A Dictionary designed to store any type using the &#39;store&#39; and &#39;load&#39; functions.
Definition: layer_cache.h:46
typename Tensor_Descriptor< ValueType, SystemTag, Integer< 3 > > ::allocator_type output_allocator_type
Definition: layer_base.h:31
Definition: layer_loader.h:19
bc::size_t batched_output_size() const
Definition: layer_base.h:152
input_shape_type input_shape() const
Definition: layer_base.h:75
bc::size_t batched_input_size() const
Definition: layer_base.h:151
typename Tensor_Descriptor< ValueType, SystemTag, Integer< 3 > > ::system_tag output_system_tag
Definition: layer_base.h:30
bc::size_t input_size() const
Definition: layer_base.h:147
output_shape_type output_shape() const
Definition: layer_base.h:48
Definition: layer_traits.h:42
virtual void load_to_cache(Layer_Loader &, const Cache &)
Definition: layer_base.h:176
Layer_Base(std::string classname, shape_type input_shape, output_shape_type output_shape)
m_classname should be initialized by supplying __func__ to the first argument of the Layer_Base...
Definition: layer_base.h:126
virtual ~Layer_Output_Base()
Definition: layer_base.h:51
class::::::Args static auto find(bc::streams::Stream< bc::host_tag > stream, Begin begin, End end, Args... args)
Definition: algorithms.h:124
auto get_batched_input_shape() const
Definition: layer_base.h:144
auto get_batched_output_shape() const
Definition: layer_base.h:145
auto default_batched_input_tensor_factory() const
Definition: layer_base.h:233
next_layer_type * m_next_layer
Definition: layer_base.h:42
typename Tensor_Descriptor< ValueType, SystemTag, Integer< 3 > > ::value_type output_value_type
Definition: layer_base.h:29
bc::size_t output_size() const
Definition: layer_base.h:148
int size_t
Definition: common.h:283
Definition: type_traits.h:187
Definition: layer_base.h:24
const prev_layer_type & prev_layer() const
Definition: layer_base.h:76
prev_layer_type * m_prev_layer
Definition: layer_base.h:69
void update_weights()
Definition: layer_base.h:179
typename conditional_detected< func, TestType, DefaultType >::type conditional_detected_t
Definition: type_traits.h:87
virtual ~Layer_Base()
Definition: layer_base.h:139
const std::string & classname() const
Returns the derived_classes class namepse.
Definition: layer_base.h:185
virtual void save_from_cache(Layer_Loader &, const Cache &) const
Definition: layer_base.h:174
auto default_batched_output_tensor_factory() const
Definition: layer_base.h:240
bc::size_t batch_size() const
Definition: layer_base.h:149
void copy_training_data_to_single_predict(Cache &, int batch_index)
Definition: layer_base.h:178
Definition: layer_base.h:27
output_shape_type m_output_shape
Definition: layer_base.h:118
auto get_batched_learning_rate() const
Definition: layer_base.h:171
void set_learning_rate(value_type learning_rate)
Definition: layer_base.h:162
virtual void set_learning_rate_hook(value_type learning_rate)
Definition: layer_base.h:168
shape_type m_input_shape
Definition: layer_base.h:117
virtual void load(Layer_Loader &)
Definition: layer_base.h:175
void clear_bp_storage(Cache &)
Definition: layer_base.h:180
virtual output_shape_type get_output_shape() const
Definition: layer_base.h:141
Layer_Base(std::string classname, shape_type input_shape)
Definition: layer_base.h:135
virtual void save(Layer_Loader &) const
Definition: layer_base.h:173
auto default_input_tensor_factory() const
Definition: layer_base.h:221
void set_prev(prev_layer_type &prev)
Definition: layer_base.h:74
BCINLINE auto concat(Ints... value) const
Definition: dim.h:105
bc::Dim< output_tensor_dim::value > m_output_shape
Definition: layer_base.h:43
typename Tensor_Descriptor< ValueType, SystemTag, Integer< 3 > > ::batched_type batched_output_tensor_type
Definition: layer_base.h:35
virtual void set_batch_size_hook(int batch_size)
Definition: layer_base.h:160
The Evaluator determines if an expression needs to be greedily optimized.
Definition: algorithms.h:22
BCINLINE value_type prod(size_t start, size_t end) const
Definition: dim.h:244