mlpack  master
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:
66  template<typename RNNModuleType, typename ActionModuleType>
67  RecurrentAttention(const size_t outSize,
68  const RNNModuleType& rnn,
69  const ActionModuleType& action,
70  const size_t rho);
71 
79  template<typename eT>
80  void Forward(arma::Mat<eT>&& input, arma::Mat<eT>&& output);
81 
91  template<typename eT>
92  void Backward(const arma::Mat<eT>&& /* input */,
93  arma::Mat<eT>&& gy,
94  arma::Mat<eT>&& g);
95 
96  /*
97  * Calculate the gradient using the output delta and the input activation.
98  *
99  * @param input The input parameter used for calculating the gradient.
100  * @param error The calculated error.
101  * @param gradient The calculated gradient.
102  */
103  template<typename eT>
104  void Gradient(arma::Mat<eT>&& /* input */,
105  arma::Mat<eT>&& /* error */,
106  arma::Mat<eT>&& /* gradient */);
107 
109  std::vector<LayerTypes>& Model() { return network; }
110 
112  bool Deterministic() const { return deterministic; }
114  bool& Deterministic() { return deterministic; }
115 
117  OutputDataType const& Parameters() const { return parameters; }
119  OutputDataType& Parameters() { return parameters; }
120 
122  InputDataType const& InputParameter() const { return inputParameter; }
124  InputDataType& InputParameter() { return inputParameter; }
125 
127  OutputDataType const& OutputParameter() const { return outputParameter; }
129  OutputDataType& OutputParameter() { return outputParameter; }
130 
132  OutputDataType const& Delta() const { return delta; }
134  OutputDataType& Delta() { return delta; }
135 
137  OutputDataType const& Gradient() const { return gradient; }
139  OutputDataType& Gradient() { return gradient; }
140 
144  template<typename Archive>
145  void Serialize(Archive& ar, const unsigned int /* version */);
146 
147  private:
150  {
151  intermediateGradient.zeros();
152 
153  // Gradient of the action module.
154  if (backwardStep == (rho - 1))
155  {
156  boost::apply_visitor(GradientVisitor(std::move(initialInput),
157  std::move(actionError)), actionModule);
158  }
159  else
160  {
161  boost::apply_visitor(GradientVisitor(std::move(boost::apply_visitor(
163  actionModule);
164  }
165 
166  // Gradient of the recurrent module.
167  boost::apply_visitor(GradientVisitor(std::move(boost::apply_visitor(
169  rnnModule);
170 
172  }
173 
175  size_t outSize;
176 
179 
182 
184  size_t rho;
185 
187  size_t forwardStep;
188 
190  size_t backwardStep;
191 
194 
196  OutputDataType parameters;
197 
200 
203 
205  std::vector<LayerTypes> network;
206 
209 
212 
215 
218 
220  std::vector<arma::mat> feedbackOutputParameter;
221 
223  std::vector<arma::mat> moduleOutputParameter;
224 
226  OutputDataType delta;
227 
229  OutputDataType gradient;
230 
232  InputDataType inputParameter;
233 
235  OutputDataType outputParameter;
236 
238  arma::mat recurrentError;
239 
241  arma::mat actionError;
242 
244  arma::mat actionDelta;
245 
247  arma::mat rnnDelta;
248 
250  arma::mat initialInput;
251 
254 
256  arma::mat attentionGradient;
257 
260 }; // class RecurrentAttention
261 
262 } // namespace ann
263 } // namespace mlpack
264 
265 // Include implementation.
266 #include "recurrent_attention_impl.hpp"
267 
268 #endif
LayerTypes rnnModule
Locally-stored start module.
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...
size_t forwardStep
Locally-stored number of forward steps.
void Serialize(Archive &ar, const unsigned int)
Serialize the layer.
arma::mat attentionGradient
Locally-stored attention gradient.
OutputDataType & Parameters()
Modify the parameters.
size_t backwardStep
Locally-stored number of backward steps.
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
RecurrentAttention(const size_t outSize, const RNNModuleType &rnn, const ActionModuleType &action, const size_t rho)
Create the RecurrentAttention object using the specified modules.
LayerTypes actionModule
Locally-stored input module.
LayerTypes recurrentModule
Locally-stored recurrent module.
size_t outSize
Locally-stored module output size.
The core includes that mlpack expects; standard C++ includes and Armadillo.
OutputParameterVisitor outputParameterVisitor
Locally-stored output parameter visitor.
LayerTypes mergeModule
Locally-stored merge module.
arma::mat recurrentError
Locally-stored recurrent error parameter.
WeightSizeVisitor returns the number of weights of the given module.
arma::mat actionError
Locally-stored action error parameter.
OutputDataType & OutputParameter()
Modify the output parameter.
OutputDataType delta
Locally-stored delta object.
DeltaVisitor deltaVisitor
Locally-stored delta visitor.
std::vector< arma::mat > moduleOutputParameter
List of all module parameters for the backward pass (BBTT).
OutputDataType parameters
Locally-stored weight object.
arma::mat initialInput
Locally-stored initial action input.
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...
size_t rho
Number of steps to backpropagate through time (BPTT).
OutputDataType const & Gradient() const
Get the gradient.
arma::mat rnnDelta
Locally-stored recurrent delta.
OutputDataType const & Delta() const
Get the delta.
ResetVisitor executes the Reset() function.
OutputParameterVisitor exposes the output parameter of the given module.
OutputDataType & Gradient()
Modify the gradient.
OutputDataType const & OutputParameter() const
Get the output parameter.
OutputDataType outputParameter
Locally-stored output parameter object.
void IntermediateGradient()
Calculate the gradient of the attention module.
InputDataType const & InputParameter() const
Get the input parameter.
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 > *, 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 > *, 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 > *, MaxPooling< arma::mat, arma::mat > *, MeanPooling< arma::mat, arma::mat > *, MeanSquaredError< 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 > * > LayerTypes
LayerTypes initialModule
Locally-stored initial module.
OutputDataType const & Parameters() const
Get the parameters.
bool & Deterministic()
Modify the value of the deterministic parameter.
SearchModeVisitor executes the Gradient() method of the given module using the input and delta parame...
ResetVisitor resetVisitor
Locally-stored reset visitor.
InputDataType & InputParameter()
Modify the input parameter.
InputDataType inputParameter
Locally-stored input parameter object.
WeightSizeVisitor weightSizeVisitor
Locally-stored weight size visitor.
OutputDataType & Delta()
Modify the delta.
DeltaVisitor exposes the delta parameter of the given module.
bool Deterministic() const
The value of the deterministic parameter.
arma::mat intermediateGradient
Locally-stored intermediate gradient for the attention module.
OutputDataType gradient
Locally-stored gradient object.
arma::mat actionDelta
Locally-stored action delta.
std::vector< arma::mat > feedbackOutputParameter
Locally-stored feedback output parameters.
bool deterministic
If true dropout and scaling is disabled, see notes above.
std::vector< LayerTypes > & Model()
Get the model modules.
std::vector< LayerTypes > network
Locally-stored model modules.