BlackCat_Tensors
A GPU-supported autograd and linear algebra library, designed for neural network construction
feedforward.h
Go to the documentation of this file.
1 /*
2  * FeedForward.cu
3  *
4  * Created on: Jan 28, 2018
5  * Author: joseph
6  */
7 
8 #ifndef BLACKCATTENSORS_NEURALNETWORKS_LAYERS_FEEDFORWARD_H_
9 #define BLACKCATTENSORS_NEURALNETWORKS_LAYERS_FEEDFORWARD_H_
10 
11 #include "layer_base.h"
12 
13 namespace bc {
14 namespace nn {
15 
16 template<
17  class SystemTag,
18  class ValueType,
19  class Optimizer=Stochastic_Gradient_Descent>
20 struct FeedForward:
21  public Layer_Base<
22  FeedForward<SystemTag, ValueType, Optimizer>,
23  Tensor_Descriptor<ValueType, SystemTag, Integer<1>>>
24 {
25  using system_tag = SystemTag;
26  using value_type = ValueType;
27 
29 
32 
34  using optimizer_type = Optimizer;
35 
36  using greedy_evaluate_delta = std::true_type;
37 
38 private:
39 
42 
43  using mat_opt_t = typename Optimizer::template Optimizer<mat>;
44  using vec_opt_t = typename Optimizer::template Optimizer<vec>;
45 
47 
48  mat w; //weights
49  vec b; //biases
50 
51  mat w_gradients;
52  vec b_gradients;
53 
54  mat_opt_t w_opt;
55  vec_opt_t b_opt;
56 
57 public:
58 
59  FeedForward(bc::size_t inputs, bc::size_t outputs):
60  parent_type(__func__, {inputs}, {outputs}),
61  w(outputs, inputs),
62  b(outputs),
63  w_gradients(outputs, inputs),
64  b_gradients(outputs),
65  w_opt(w.get_shape()),
66  b_opt(b.get_shape())
67  {
68  w.randomize(-2, 2);
69  b.randomize(-2, 2);
70  w_gradients.zero();
71  b_gradients.zero();
72  }
73 
74  template<class Matrix>
75  auto forward_propagation(const Matrix& x)
76  {
77  return w * x + b;
78  }
79 
80  template<class X, class Delta>
81  auto back_propagation(const X& x, const Delta& dy)
82  {
83  w_gradients -= dy * x.t();
84  b_gradients -= dy;
85  return w.t() * dy;
86  }
87 
88  virtual void set_learning_rate_hook(value_type lr) override
89  {
90  w_opt.set_learning_rate(this->get_batched_learning_rate());
91  b_opt.set_learning_rate(this->get_batched_learning_rate());
92  }
93 
95  {
96  w_opt.update(w, w_gradients);
97  w_gradients.zero();
98 
99  b_opt.update(b, b_gradients);
100  b_gradients.zero();
101  }
102 
103  virtual void save(Layer_Loader& loader) const override
104  {
105  loader.save_variable(w, "w");
106  loader.save_variable(b, "b");
107  w_opt.save(loader, "w_opt");
108  b_opt.save(loader, "b_opt");
109  }
110 
111  virtual void load(Layer_Loader& loader) override
112  {
113  loader.load_variable(w, "w");
114  loader.load_variable(b, "b");
115  w_opt.load(loader, "w_opt");
116  b_opt.save(loader, "b_opt");
117  }
118 };
119 
120 template<class SystemTag, class Optimizer=nn_default_optimizer_type>
121 auto feedforward(SystemTag system_tag, int inputs, int outputs, Optimizer=Optimizer()) {
122  using value_type = typename SystemTag::default_floating_point_type;
123  return FeedForward<SystemTag, value_type, Optimizer>(inputs, outputs);
124 }
125 
126 template<class Optimizer=nn_default_optimizer_type>
127 auto feedforward(int inputs, int outputs, Optimizer=Optimizer()) {
128  using system_tag = BLACKCAT_DEFAULT_SYSTEM_T;
129  using value_type = typename system_tag::default_floating_point_type;
130  return FeedForward<system_tag, value_type, Optimizer>(inputs, outputs);
131 }
132 
133 
134 }
135 }
136 
137 #endif /* FEEDFORWARD_CU_ */
void randomize(value_type lb=0, value_type ub=1)
Definition: tensor_base.h:36
std::true_type greedy_evaluate_delta
Definition: feedforward.h:36
self_type & zero()
Definition: tensor_base.h:13
Definition: layer_base.h:86
#define BLACKCAT_DEFAULT_SYSTEM_T
Definition: common.h:49
Definition: layer_loader.h:19
virtual void set_learning_rate_hook(value_type lr) override
Definition: feedforward.h:88
void save_variable(const T &tensor, string variable_name)
Definition: layer_loader.h:44
auto forward_propagation(const Matrix &x)
Definition: feedforward.h:75
Definition: feedforward.h:20
virtual void save(Layer_Loader &loader) const override
Definition: feedforward.h:103
FeedForward(bc::size_t inputs, bc::size_t outputs)
Definition: feedforward.h:59
auto back_propagation(const X &x, const Delta &dy)
Definition: feedforward.h:81
int size_t
Definition: common.h:283
const auto t() const
Definition: expression_base.h:94
void load_variable(T &tensor, string variable_name)
Definition: layer_loader.h:50
Definition: cmath.h:17
SystemTag system_tag
Definition: feedforward.h:25
Optimizer optimizer_type
Definition: feedforward.h:34
ValueType value_type
Definition: feedforward.h:26
void update_weights()
Definition: feedforward.h:94
auto feedforward(SystemTag system_tag, int inputs, int outputs, Optimizer=Optimizer())
Definition: feedforward.h:121
virtual void load(Layer_Loader &loader) override
Definition: feedforward.h:111
The Evaluator determines if an expression needs to be greedily optimized.
Definition: algorithms.h:22
Definition: recycle_allocator.h:57