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 
52 template <
53  typename InputDataType = arma::mat,
54  typename OutputDataType = arma::mat
55 >
56 class RecurrentAttention
57 {
58  public:
64 
73  template<typename RNNModuleType, typename ActionModuleType>
74  RecurrentAttention(const size_t outSize,
75  const RNNModuleType& rnn,
76  const ActionModuleType& action,
77  const size_t rho);
78 
86  template<typename eT>
87  void Forward(const arma::Mat<eT>& input, arma::Mat<eT>& output);
88 
98  template<typename eT>
99  void Backward(const arma::Mat<eT>& /* input */,
100  const arma::Mat<eT>& gy,
101  arma::Mat<eT>& g);
102 
103  /*
104  * Calculate the gradient using the output delta and the input activation.
105  *
106  * @param * (input) The input parameter used for calculating the gradient.
107  * @param * (error) The calculated error.
108  * @param * (gradient) The calculated gradient.
109  */
110  template<typename eT>
111  void Gradient(const arma::Mat<eT>& /* input */,
112  const arma::Mat<eT>& /* error */,
113  arma::Mat<eT>& /* gradient */);
114 
116  std::vector<LayerTypes<>>& Model() { return network; }
117 
119  bool Deterministic() const { return deterministic; }
121  bool& Deterministic() { return deterministic; }
122 
124  OutputDataType const& Parameters() const { return parameters; }
126  OutputDataType& Parameters() { return parameters; }
127 
129  OutputDataType const& OutputParameter() const { return outputParameter; }
131  OutputDataType& OutputParameter() { return outputParameter; }
132 
134  OutputDataType const& Delta() const { return delta; }
136  OutputDataType& Delta() { return delta; }
137 
139  OutputDataType const& Gradient() const { return gradient; }
141  OutputDataType& Gradient() { return gradient; }
142 
144  size_t OutSize() const { return outSize; }
145 
147  size_t const& Rho() const { return rho; }
148 
152  template<typename Archive>
153  void serialize(Archive& ar, const uint32_t /* version */);
154 
155  private:
157  void IntermediateGradient()
158  {
159  intermediateGradient.zeros();
160 
161  // Gradient of the action module.
162  if (backwardStep == (rho - 1))
163  {
164  boost::apply_visitor(GradientVisitor(initialInput, actionError),
165  actionModule);
166  }
167  else
168  {
169  boost::apply_visitor(GradientVisitor(boost::apply_visitor(
170  outputParameterVisitor, actionModule), actionError),
171  actionModule);
172  }
173 
174  // Gradient of the recurrent module.
175  boost::apply_visitor(GradientVisitor(boost::apply_visitor(
176  outputParameterVisitor, rnnModule), recurrentError),
177  rnnModule);
178 
179  attentionGradient += intermediateGradient;
180  }
181 
183  size_t outSize;
184 
186  LayerTypes<> rnnModule;
187 
189  LayerTypes<> actionModule;
190 
192  size_t rho;
193 
195  size_t forwardStep;
196 
198  size_t backwardStep;
199 
201  bool deterministic;
202 
204  OutputDataType parameters;
205 
207  std::vector<LayerTypes<>> network;
208 
210  WeightSizeVisitor weightSizeVisitor;
211 
213  DeltaVisitor deltaVisitor;
214 
216  OutputParameterVisitor outputParameterVisitor;
217 
219  std::vector<arma::mat> feedbackOutputParameter;
220 
222  std::vector<arma::mat> moduleOutputParameter;
223 
225  OutputDataType delta;
226 
228  OutputDataType gradient;
229 
231  OutputDataType outputParameter;
232 
234  arma::mat recurrentError;
235 
237  arma::mat actionError;
238 
240  arma::mat actionDelta;
241 
243  arma::mat rnnDelta;
244 
246  arma::mat initialInput;
247 
249  ResetVisitor resetVisitor;
250 
252  arma::mat attentionGradient;
253 
255  arma::mat intermediateGradient;
256 }; // class RecurrentAttention
257 
258 } // namespace ann
259 } // namespace mlpack
260 
261 // Include implementation.
262 #include "recurrent_attention_impl.hpp"
263 
264 #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.
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 > *, FlexibleReLU< 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 > *, Softmax< arma::mat, arma::mat > *, SpatialDropout< arma::mat, arma::mat > *, TransposedConvolution< NaiveConvolution< ValidConvolution >, NaiveConvolution< ValidConvolution >, NaiveConvolution< ValidConvolution >, arma::mat, arma::mat > *, WeightNorm< arma::mat, arma::mat > *, MoreTypes, CustomLayers *... > LayerTypes
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.
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.