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.
261 #include "recurrent_attention_impl.hpp"
262 
263 #endif
bool & Deterministic()
Modify the value of the deterministic parameter.
OutputDataType & Parameters()
Modify the parameters.
Linear algebra utility functions, generally performed on matrices or vectors.
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
The core includes that mlpack expects; standard C++ includes and Armadillo.
size_t OutSize() const
Get the module output size.
WeightSizeVisitor returns the number of weights of the given module.
OutputDataType & Gradient()
Modify the gradient.
size_t const & Rho() const
Get the number of steps to backpropagate through time.
OutputDataType const & Delta() const
Get the delta.
OutputDataType const & Gradient() const
Get the gradient.
ResetVisitor executes the Reset() function.
OutputParameterVisitor exposes the output parameter of the given module.
OutputDataType & OutputParameter()
Modify the output parameter.
RecurrentAttention()
Default constructor: this will not give a usable RecurrentAttention object, so be sure to set all the...
OutputDataType & Delta()
Modify the delta.
SearchModeVisitor executes the Gradient() method of the given module using the input and delta parame...
OutputDataType const & OutputParameter() const
Get the output parameter.
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...
DeltaVisitor exposes the delta parameter of the given module.
boost::variant< AdaptiveMaxPooling< arma::mat, arma::mat > *, AdaptiveMeanPooling< arma::mat, arma::mat > *, Add< arma::mat, arma::mat > *, AddMerge< arma::mat, arma::mat > *, AlphaDropout< arma::mat, arma::mat > *, AtrousConvolution< NaiveConvolution< ValidConvolution >, NaiveConvolution< FullConvolution >, NaiveConvolution< ValidConvolution >, arma::mat, arma::mat > *, BaseLayer< LogisticFunction, arma::mat, arma::mat > *, BaseLayer< IdentityFunction, arma::mat, arma::mat > *, BaseLayer< TanhFunction, arma::mat, arma::mat > *, BaseLayer< SoftplusFunction, arma::mat, arma::mat > *, BaseLayer< RectifierFunction, arma::mat, arma::mat > *, BatchNorm< arma::mat, arma::mat > *, BilinearInterpolation< arma::mat, arma::mat > *, CELU< arma::mat, arma::mat > *, Concat< arma::mat, arma::mat > *, Concatenate< arma::mat, arma::mat > *, ConcatPerformance< NegativeLogLikelihood< arma::mat, arma::mat >, arma::mat, arma::mat > *, Constant< arma::mat, arma::mat > *, Convolution< NaiveConvolution< ValidConvolution >, NaiveConvolution< FullConvolution >, NaiveConvolution< ValidConvolution >, arma::mat, arma::mat > *, CReLU< arma::mat, arma::mat > *, DropConnect< arma::mat, arma::mat > *, Dropout< arma::mat, arma::mat > *, ELU< arma::mat, arma::mat > *, FastLSTM< arma::mat, arma::mat > *, GRU< arma::mat, arma::mat > *, HardTanH< arma::mat, arma::mat > *, Join< arma::mat, arma::mat > *, LayerNorm< arma::mat, arma::mat > *, LeakyReLU< arma::mat, arma::mat > *, Linear< arma::mat, arma::mat, NoRegularizer > *, LinearNoBias< arma::mat, arma::mat, NoRegularizer > *, LogSoftMax< arma::mat, arma::mat > *, Lookup< arma::mat, arma::mat > *, LSTM< arma::mat, arma::mat > *, MaxPooling< arma::mat, arma::mat > *, MeanPooling< arma::mat, arma::mat > *, MiniBatchDiscrimination< arma::mat, arma::mat > *, MultiplyConstant< arma::mat, arma::mat > *, MultiplyMerge< arma::mat, arma::mat > *, NegativeLogLikelihood< arma::mat, arma::mat > *, NoisyLinear< arma::mat, arma::mat > *, Padding< arma::mat, arma::mat > *, PReLU< arma::mat, arma::mat > *, Sequential< arma::mat, arma::mat, false > *, Sequential< arma::mat, arma::mat, true > *, Softmax< arma::mat, arma::mat > *, TransposedConvolution< NaiveConvolution< ValidConvolution >, NaiveConvolution< ValidConvolution >, NaiveConvolution< ValidConvolution >, arma::mat, arma::mat > *, WeightNorm< arma::mat, arma::mat > *, MoreTypes, CustomLayers *... > LayerTypes
OutputDataType const & Parameters() const
Get the parameters.
std::vector< LayerTypes<> > & Model()
Get the model modules.
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...
bool Deterministic() const
The value of the deterministic parameter.