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 #include <boost/ptr_container/ptr_vector.hpp>
17 
18 #include "../visitor/delta_visitor.hpp"
19 #include "../visitor/output_parameter_visitor.hpp"
20 #include "../visitor/reset_visitor.hpp"
21 #include "../visitor/weight_size_visitor.hpp"
22 
23 #include "layer_types.hpp"
24 #include "add_merge.hpp"
25 #include "sequential.hpp"
26 
27 namespace mlpack {
28 namespace ann {
29 
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(arma::Mat<eT>&& input, arma::Mat<eT>&& output);
87 
97  template<typename eT>
98  void Backward(const arma::Mat<eT>&& /* input */,
99  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(arma::Mat<eT>&& /* input */,
111  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 
145  template<typename Archive>
146  void serialize(Archive& ar, const unsigned int /* version */);
147 
148  private:
150  void IntermediateGradient()
151  {
152  intermediateGradient.zeros();
153 
154  // Gradient of the action module.
155  if (backwardStep == (rho - 1))
156  {
157  boost::apply_visitor(GradientVisitor(std::move(initialInput),
158  std::move(actionError)), actionModule);
159  }
160  else
161  {
162  boost::apply_visitor(GradientVisitor(std::move(boost::apply_visitor(
163  outputParameterVisitor, actionModule)), std::move(actionError)),
164  actionModule);
165  }
166 
167  // Gradient of the recurrent module.
168  boost::apply_visitor(GradientVisitor(std::move(boost::apply_visitor(
169  outputParameterVisitor, rnnModule)), std::move(recurrentError)),
170  rnnModule);
171 
172  attentionGradient += intermediateGradient;
173  }
174 
176  size_t outSize;
177 
179  LayerTypes<> rnnModule;
180 
182  LayerTypes<> actionModule;
183 
185  size_t rho;
186 
188  size_t forwardStep;
189 
191  size_t backwardStep;
192 
194  bool deterministic;
195 
197  OutputDataType parameters;
198 
200  std::vector<LayerTypes<>> network;
201 
203  WeightSizeVisitor weightSizeVisitor;
204 
206  DeltaVisitor deltaVisitor;
207 
209  OutputParameterVisitor outputParameterVisitor;
210 
212  std::vector<arma::mat> feedbackOutputParameter;
213 
215  std::vector<arma::mat> moduleOutputParameter;
216 
218  OutputDataType delta;
219 
221  OutputDataType gradient;
222 
224  OutputDataType outputParameter;
225 
227  arma::mat recurrentError;
228 
230  arma::mat actionError;
231 
233  arma::mat actionDelta;
234 
236  arma::mat rnnDelta;
237 
239  arma::mat initialInput;
240 
242  ResetVisitor resetVisitor;
243 
245  arma::mat attentionGradient;
246 
248  arma::mat intermediateGradient;
249 }; // class RecurrentAttention
250 
251 } // namespace ann
252 } // namespace mlpack
253 
254 // Include implementation.
255 #include "recurrent_attention_impl.hpp"
256 
257 #endif
bool & Deterministic()
Modify the value of the deterministic parameter.
OutputDataType & Parameters()
Modify the parameters.
.hpp
Definition: add_to_po.hpp:21
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
The core includes that mlpack expects; standard C++ includes and Armadillo.
WeightSizeVisitor returns the number of weights of the given module.
OutputDataType & Gradient()
Modify the gradient.
boost::variant< Add< arma::mat, arma::mat > *, AddMerge< 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< RectifierFunction, arma::mat, arma::mat > *, BaseLayer< SoftplusFunction, arma::mat, arma::mat > *, BatchNorm< arma::mat, arma::mat > *, BilinearInterpolation< 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 > *, TransposedConvolution< NaiveConvolution< ValidConvolution >, NaiveConvolution< FullConvolution >, NaiveConvolution< ValidConvolution >, arma::mat, arma::mat > *, DropConnect< arma::mat, arma::mat > *, Dropout< arma::mat, arma::mat > *, AlphaDropout< arma::mat, arma::mat > *, ELU< arma::mat, arma::mat > *, FlexibleReLU< arma::mat, arma::mat > *, Glimpse< arma::mat, arma::mat > *, HardTanH< arma::mat, arma::mat > *, Highway< arma::mat, arma::mat > *, Join< arma::mat, arma::mat > *, LayerNorm< arma::mat, arma::mat > *, LeakyReLU< arma::mat, arma::mat > *, CReLU< 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 > *, GRU< arma::mat, arma::mat > *, FastLSTM< 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 > *, Padding< arma::mat, arma::mat > *, PReLU< arma::mat, arma::mat > *, MoreTypes, CustomLayers *... > LayerTypes
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.
DeltaVisitor exposes the delta parameter of the given module.
OutputDataType const & Parameters() const
Get the parameters.
std::vector< LayerTypes<> > & Model()
Get the model modules.
void Backward(const arma::Mat< eT > &&, 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...
void Forward(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.