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  InputDataType const& InputParameter() const { return inputParameter; }
130  InputDataType& InputParameter() { return inputParameter; }
131 
133  OutputDataType const& OutputParameter() const { return outputParameter; }
135  OutputDataType& OutputParameter() { return outputParameter; }
136 
138  OutputDataType const& Delta() const { return delta; }
140  OutputDataType& Delta() { return delta; }
141 
143  OutputDataType const& Gradient() const { return gradient; }
145  OutputDataType& Gradient() { return gradient; }
146 
150  template<typename Archive>
151  void serialize(Archive& ar, const unsigned int /* version */);
152 
153  private:
155  void IntermediateGradient()
156  {
157  intermediateGradient.zeros();
158 
159  // Gradient of the action module.
160  if (backwardStep == (rho - 1))
161  {
162  boost::apply_visitor(GradientVisitor(std::move(initialInput),
163  std::move(actionError)), actionModule);
164  }
165  else
166  {
167  boost::apply_visitor(GradientVisitor(std::move(boost::apply_visitor(
168  outputParameterVisitor, actionModule)), std::move(actionError)),
169  actionModule);
170  }
171 
172  // Gradient of the recurrent module.
173  boost::apply_visitor(GradientVisitor(std::move(boost::apply_visitor(
174  outputParameterVisitor, rnnModule)), std::move(recurrentError)),
175  rnnModule);
176 
177  attentionGradient += intermediateGradient;
178  }
179 
181  size_t outSize;
182 
184  LayerTypes<> rnnModule;
185 
187  LayerTypes<> actionModule;
188 
190  size_t rho;
191 
193  size_t forwardStep;
194 
196  size_t backwardStep;
197 
199  bool deterministic;
200 
202  OutputDataType parameters;
203 
205  std::vector<LayerTypes<>> network;
206 
208  WeightSizeVisitor weightSizeVisitor;
209 
211  DeltaVisitor deltaVisitor;
212 
214  OutputParameterVisitor outputParameterVisitor;
215 
217  std::vector<arma::mat> feedbackOutputParameter;
218 
220  std::vector<arma::mat> moduleOutputParameter;
221 
223  OutputDataType delta;
224 
226  OutputDataType gradient;
227 
229  InputDataType inputParameter;
230 
232  OutputDataType outputParameter;
233 
235  arma::mat recurrentError;
236 
238  arma::mat actionError;
239 
241  arma::mat actionDelta;
242 
244  arma::mat rnnDelta;
245 
247  arma::mat initialInput;
248 
250  ResetVisitor resetVisitor;
251 
253  arma::mat attentionGradient;
254 
256  arma::mat intermediateGradient;
257 }; // class RecurrentAttention
258 
259 } // namespace ann
260 } // namespace mlpack
261 
262 // Include implementation.
263 #include "recurrent_attention_impl.hpp"
264 
265 #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.
InputDataType & InputParameter()
Modify the input parameter.
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.
InputDataType const & InputParameter() const
Get the input parameter.
OutputDataType const & Delta() const
Get the delta.
boost::variant< Add< arma::mat, arma::mat > *, AddMerge< 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 > *, BatchNorm< arma::mat, arma::mat > *, BilinearInterpolation< arma::mat, arma::mat > *, Concat< 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 > *, 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 > *, Join< arma::mat, arma::mat > *, LeakyReLU< arma::mat, arma::mat > *, Linear< arma::mat, arma::mat > *, LinearNoBias< arma::mat, arma::mat > *, 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 > *, MultiplyConstant< arma::mat, arma::mat > *, NegativeLogLikelihood< arma::mat, arma::mat > *, PReLU< arma::mat, arma::mat > *, Recurrent< arma::mat, arma::mat > *, RecurrentAttention< arma::mat, arma::mat > *, ReinforceNormal< arma::mat, arma::mat > *, Select< arma::mat, arma::mat > *, Sequential< arma::mat, arma::mat > *, VRClassReward< arma::mat, arma::mat > *, CustomLayers *... > LayerTypes
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.