mlpack
recurrent_attention.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_ANN_LAYER_RECURRENT_ATTENTION_HPP
13 #define MLPACK_METHODS_ANN_LAYER_RECURRENT_ATTENTION_HPP
14 
15 #include <mlpack/prereqs.hpp>
16 
17 #include "../visitor/delta_visitor.hpp"
18 #include "../visitor/output_parameter_visitor.hpp"
19 #include "../visitor/reset_visitor.hpp"
20 #include "../visitor/weight_size_visitor.hpp"
21 
22 #include "layer_types.hpp"
23 #include "add_merge.hpp"
24 #include "sequential.hpp"
25 
26 namespace mlpack {
27 namespace ann {
28 
51 template <
52  typename InputDataType = arma::mat,
53  typename OutputDataType = arma::mat
54 >
55 class RecurrentAttention
56 {
57  public:
63 
72  template<typename RNNModuleType, typename ActionModuleType>
73  RecurrentAttention(const size_t outSize,
74  const RNNModuleType& rnn,
75  const ActionModuleType& action,
76  const size_t rho);
77 
85  template<typename eT>
86  void Forward(const arma::Mat<eT>& input, arma::Mat<eT>& output);
87 
97  template<typename eT>
98  void Backward(const arma::Mat<eT>& /* input */,
99  const arma::Mat<eT>& gy,
100  arma::Mat<eT>& g);
101 
102  /*
103  * Calculate the gradient using the output delta and the input activation.
104  *
105  * @param * (input) The input parameter used for calculating the gradient.
106  * @param * (error) The calculated error.
107  * @param * (gradient) The calculated gradient.
108  */
109  template<typename eT>
110  void Gradient(const arma::Mat<eT>& /* input */,
111  const arma::Mat<eT>& /* error */,
112  arma::Mat<eT>& /* gradient */);
113 
115  std::vector<LayerTypes<>>& Model() { return network; }
116 
118  bool Deterministic() const { return deterministic; }
120  bool& Deterministic() { return deterministic; }
121 
123  OutputDataType const& Parameters() const { return parameters; }
125  OutputDataType& Parameters() { return parameters; }
126 
128  OutputDataType const& OutputParameter() const { return outputParameter; }
130  OutputDataType& OutputParameter() { return outputParameter; }
131 
133  OutputDataType const& Delta() const { return delta; }
135  OutputDataType& Delta() { return delta; }
136 
138  OutputDataType const& Gradient() const { return gradient; }
140  OutputDataType& Gradient() { return gradient; }
141 
143  size_t OutSize() const { return outSize; }
144 
146  size_t const& Rho() const { return rho; }
147 
151  template<typename Archive>
152  void serialize(Archive& ar, const uint32_t /* version */);
153 
154  private:
156  void IntermediateGradient()
157  {
158  intermediateGradient.zeros();
159 
160  // Gradient of the action module.
161  if (backwardStep == (rho - 1))
162  {
163  boost::apply_visitor(GradientVisitor(initialInput, actionError),
164  actionModule);
165  }
166  else
167  {
168  boost::apply_visitor(GradientVisitor(boost::apply_visitor(
169  outputParameterVisitor, actionModule), actionError),
170  actionModule);
171  }
172 
173  // Gradient of the recurrent module.
174  boost::apply_visitor(GradientVisitor(boost::apply_visitor(
175  outputParameterVisitor, rnnModule), recurrentError),
176  rnnModule);
177 
178  attentionGradient += intermediateGradient;
179  }
180 
182  size_t outSize;
183 
185  LayerTypes<> rnnModule;
186 
188  LayerTypes<> actionModule;
189 
191  size_t rho;
192 
194  size_t forwardStep;
195 
197  size_t backwardStep;
198 
200  bool deterministic;
201 
203  OutputDataType parameters;
204 
206  std::vector<LayerTypes<>> network;
207 
209  WeightSizeVisitor weightSizeVisitor;
210 
212  DeltaVisitor deltaVisitor;
213 
215  OutputParameterVisitor outputParameterVisitor;
216 
218  std::vector<arma::mat> feedbackOutputParameter;
219 
221  std::vector<arma::mat> moduleOutputParameter;
222 
224  OutputDataType delta;
225 
227  OutputDataType gradient;
228 
230  OutputDataType outputParameter;
231 
233  arma::mat recurrentError;
234 
236  arma::mat actionError;
237 
239  arma::mat actionDelta;
240 
242  arma::mat rnnDelta;
243 
245  arma::mat initialInput;
246 
248  ResetVisitor resetVisitor;
249 
251  arma::mat attentionGradient;
252 
254  arma::mat intermediateGradient;
255 }; // class RecurrentAttention
256 
257 } // namespace ann
258 } // namespace mlpack
259 
260 // Include implementation.
262 
263 #endif
RecurrentAttention()
Default constructor: this will not give a usable RecurrentAttention object, so be sure to set all the...
Definition: recurrent_attention_impl.hpp:30
OutputDataType const & OutputParameter() const
Get the output parameter.
Definition: recurrent_attention.hpp:128
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
Definition: recurrent_attention_impl.hpp:211
OutputDataType const & Parameters() const
Get the parameters.
Definition: recurrent_attention.hpp:123
OutputDataType const & Delta() const
Get the delta.
Definition: recurrent_attention.hpp:133
OutputDataType const & Gradient() const
Get the gradient.
Definition: recurrent_attention.hpp:138
OutputDataType & Parameters()
Modify the parameters.
Definition: recurrent_attention.hpp:125
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
The core includes that mlpack expects; standard C++ includes and Armadillo.
WeightSizeVisitor returns the number of weights of the given module.
Definition: weight_size_visitor.hpp:27
OutputDataType & OutputParameter()
Modify the output parameter.
Definition: recurrent_attention.hpp:130
size_t const & Rho() const
Get the number of steps to backpropagate through time.
Definition: recurrent_attention.hpp:146
ResetVisitor executes the Reset() function.
Definition: reset_visitor.hpp:26
OutputParameterVisitor exposes the output parameter of the given module.
Definition: output_parameter_visitor.hpp:27
OutputDataType & Gradient()
Modify the gradient.
Definition: recurrent_attention.hpp:140
void Backward(const arma::Mat< eT > &, const arma::Mat< eT > &gy, arma::Mat< eT > &g)
Ordinary feed backward pass of a neural network, calculating the function f(x) by propagating x backw...
Definition: recurrent_attention_impl.hpp:116
bool & Deterministic()
Modify the value of the deterministic parameter.
Definition: recurrent_attention.hpp:120
std::vector< LayerTypes<> > & Model()
Get the model modules.
Definition: recurrent_attention.hpp:115
SearchModeVisitor executes the Gradient() method of the given module using the input and delta parame...
Definition: gradient_visitor.hpp:28
OutputDataType & Delta()
Modify the delta.
Definition: recurrent_attention.hpp:135
DeltaVisitor exposes the delta parameter of the given module.
Definition: delta_visitor.hpp:27
void Forward(const arma::Mat< eT > &input, arma::Mat< eT > &output)
Ordinary feed forward pass of a neural network, evaluating the function f(x) by propagating the activ...
Definition: recurrent_attention_impl.hpp:61
bool Deterministic() const
The value of the deterministic parameter.
Definition: recurrent_attention.hpp:118
size_t OutSize() const
Get the module output size.
Definition: recurrent_attention.hpp:143