BlackCat_Tensors
A GPU-supported autograd and linear algebra library, designed for neural network construction
recurrent.h
Go to the documentation of this file.
1 /*
2  * Recurrent.cu
3  *
4  * Created on: Jan 28, 2018
5  * Author: joseph
6  */
7 
8 #ifndef RECURRENT_FEEDFORWARD_CU_
9 #define RECURRENT_FEEDFORWARD_CU_
10 
11 #include "layer_base.h"
12 
13 namespace bc {
14 namespace nn {
15 
16 template<class SystemTag, class ValueType, class RecurrentNonLinearity=bc::Tanh>
17 struct Recurrent:
18  public Layer_Base<
19  Recurrent<SystemTag, ValueType, RecurrentNonLinearity>,
20  Tensor_Descriptor<ValueType, SystemTag, Integer<1>>> {
21 
22  using system_tag = SystemTag;
23  using value_type = ValueType;
28 
29  using forward_requires_outputs = std::true_type;
30  using backward_requires_outputs = std::true_type;
31  using greedy_evaluate_delta = std::true_type;
32 
33 private:
34 
37 
38  RecurrentNonLinearity g;
40 
41  mat dc; //delta cell_state
42  mat w, w_gradients; //weights
43  mat r, r_gradients;
44  vec b, b_gradients; //biases
45 
46 public:
47 
48  Recurrent(int inputs, int outputs) :
49  parent_type(__func__, {inputs}, {outputs}),
50  w(outputs, inputs),
51  w_gradients(outputs, inputs),
52  r(outputs, outputs),
53  r_gradients(outputs, outputs),
54  b(outputs),
55  b_gradients(outputs)
56  {
57  w.randomize(-2, 2);
58  b.randomize(-2, 2);
59  r.randomize(-2, 2);
61  }
62 
63  template<class X>
64  auto forward_propagation(const X& x) {
65  return w * x + b;
66  }
67 
68  template<class X, class Y>
69  auto forward_propagation(const X& x, const Y& y) {
70  return w * x + r * g(y) + b;
71  }
72 
73  template<class X, class Y, class Delta>
74  auto back_propagation(const X& x, const Y& y, const Delta& dy) {
75  r_gradients -= dc * g.dx(y).t();
76 
77  dc.alias() = dy + r.t() * dc;
78  w_gradients -= dy * x.t();
79  b_gradients -= dy;
80  return w.t() * dy;
81  }
82 
83  void update_weights() {
84  auto lr = this->lr / this->batch_size();
85 
86  w += w_gradients * lr;
87  b += b_gradients * lr;
88  r += r_gradients * lr;
89 
90  zero_deltas();
92  }
93 
95  dc = mat(this->output_size(), bs);
96  zero_deltas();
97  }
98 
99  void zero_deltas() {
100  dc.zero();
101  }
102 
103  void zero_gradients() {
104  w_gradients.zero();
105  b_gradients.zero();
106  r_gradients.zero();
107  }
108 
109  virtual void save(Layer_Loader& loader) const override {
110  loader.save_variable(w, "w");
111  loader.save_variable(r, "r");
112  loader.save_variable(b, "b");
113  }
114 
115  virtual void load(Layer_Loader& loader) {
116  loader.load_variable(w, "w");
117  loader.load_variable(r, "r");
118  loader.load_variable(b, "b");
119  }
120 };
121 
122 #ifndef BC_CLING_JIT
123 template<class ValueType, class SystemTag>
124 Recurrent<SystemTag, ValueType> recurrent(SystemTag system_tag, int inputs, int outputs) {
125  return Recurrent<SystemTag, ValueType>(inputs, outputs);
126 }
127 #endif
128 
129 template<class SystemTag>
130 auto recurrent(SystemTag system_tag, int inputs, int outputs) {
132 }
133 
134 auto recurrent(int inputs, int outputs) {
136  typename BLACKCAT_DEFAULT_SYSTEM_T::default_floating_point_type>(inputs, outputs);
137 }
138 
139 
140 }
141 }
142 
143 #endif /* FEEDFORWARD_CU_ */
void update_weights()
Definition: recurrent.h:83
void randomize(value_type lb=0, value_type ub=1)
Definition: tensor_base.h:36
std::true_type greedy_evaluate_delta
Definition: recurrent.h:31
void set_batch_size_hook(bc::size_t bs)
Definition: recurrent.h:94
Recurrent< SystemTag, ValueType > recurrent(SystemTag system_tag, int inputs, int outputs)
Definition: recurrent.h:124
void zero_deltas()
Definition: recurrent.h:99
virtual void save(Layer_Loader &loader) const override
Definition: recurrent.h:109
virtual void load(Layer_Loader &loader)
Definition: recurrent.h:115
self_type & zero()
Definition: tensor_base.h:13
Recurrent(int inputs, int outputs)
Definition: recurrent.h:48
ValueType value_type
Definition: recurrent.h:23
std::true_type forward_requires_outputs
Definition: recurrent.h:29
Definition: layer_base.h:86
#define BLACKCAT_DEFAULT_SYSTEM_T
Definition: common.h:49
Definition: layer_loader.h:19
Definition: recurrent.h:17
void save_variable(const T &tensor, string variable_name)
Definition: layer_loader.h:44
std::true_type backward_requires_outputs
Definition: recurrent.h:30
auto forward_propagation(const X &x, const Y &y)
Definition: recurrent.h:69
Alias alias()
Definition: tensor_base.h:104
int size_t
Definition: common.h:283
Definition: allocators.h:20
const auto t() const
Definition: expression_base.h:94
auto forward_propagation(const X &x)
Definition: recurrent.h:64
void load_variable(T &tensor, string variable_name)
Definition: layer_loader.h:50
Definition: cmath.h:17
void zero_gradients()
Definition: recurrent.h:103
SystemTag system_tag
Definition: recurrent.h:22
auto back_propagation(const X &x, const Y &y, const Delta &dy)
Definition: recurrent.h:74
static constexpr value_type default_learning_rate
Definition: layer_base.h:107
The Evaluator determines if an expression needs to be greedily optimized.
Definition: algorithms.h:22