mlpack 3.4.2
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
27namespace mlpack {
28namespace ann {
29
52template <
53 typename InputDataType = arma::mat,
54 typename OutputDataType = arma::mat
55>
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 unsigned int /* 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
SearchModeVisitor executes the Gradient() method of the given module using the input and delta parame...
This class implements the Recurrent Model for Visual Attention, using a variety of possible layer imp...
OutputDataType const & Delta() const
Get the delta.
OutputDataType const & Parameters() const
Get the parameters.
RecurrentAttention()
Default constructor: this will not give a usable RecurrentAttention object, so be sure to set all the...
size_t const & Rho() const
Get the number of steps to backpropagate through time.
size_t OutSize() const
Get the module output size.
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...
OutputDataType const & OutputParameter() const
Get the output parameter.
RecurrentAttention(const size_t outSize, const RNNModuleType &rnn, const ActionModuleType &action, const size_t rho)
Create the RecurrentAttention object using the specified modules.
bool & Deterministic()
Modify the value of the deterministic parameter.
bool Deterministic() const
The value of the deterministic parameter.
OutputDataType const & Gradient() const
Get the gradient.
OutputDataType & Gradient()
Modify the gradient.
void Gradient(const arma::Mat< eT > &, const arma::Mat< eT > &, arma::Mat< eT > &)
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...
OutputDataType & OutputParameter()
Modify the output parameter.
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
OutputDataType & Parameters()
Modify the parameters.
OutputDataType & Delta()
Modify the delta.
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
The core includes that mlpack expects; standard C++ includes and Armadillo.