recurrent.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_ANN_LAYER_RECURRENT_HPP
14 #define MLPACK_METHODS_ANN_LAYER_RECURRENT_HPP
15 
16 #include <mlpack/core.hpp>
17 
18 #include "../visitor/delete_visitor.hpp"
19 #include "../visitor/delta_visitor.hpp"
20 #include "../visitor/output_parameter_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 
39 template <
40  typename InputDataType = arma::mat,
41  typename OutputDataType = arma::mat,
42  typename... CustomLayers
43 >
44 class Recurrent
45 {
46  public:
51  Recurrent();
52 
54  ~Recurrent();
55 
65  template<typename StartModuleType,
66  typename InputModuleType,
67  typename FeedbackModuleType,
68  typename TransferModuleType>
69  Recurrent(const StartModuleType& start,
70  const InputModuleType& input,
71  const FeedbackModuleType& feedback,
72  const TransferModuleType& transfer,
73  const size_t rho);
74 
82  template<typename eT>
83  void Forward(arma::Mat<eT>&& input, arma::Mat<eT>&& output);
84 
94  template<typename eT>
95  void Backward(const arma::Mat<eT>&& /* input */,
96  arma::Mat<eT>&& gy,
97  arma::Mat<eT>&& g);
98 
99  /*
100  * Calculate the gradient using the output delta and the input activation.
101  *
102  * @param input The input parameter used for calculating the gradient.
103  * @param error The calculated error.
104  * @param gradient The calculated gradient.
105  */
106  template<typename eT>
107  void Gradient(arma::Mat<eT>&& input,
108  arma::Mat<eT>&& error,
109  arma::Mat<eT>&& /* gradient */);
110 
112  std::vector<LayerTypes<CustomLayers...> >& Model() { return network; }
113 
115  bool Deterministic() const { return deterministic; }
117  bool& Deterministic() { return deterministic; }
118 
120  OutputDataType const& Parameters() const { return parameters; }
122  OutputDataType& Parameters() { return parameters; }
123 
125  InputDataType const& InputParameter() const { return inputParameter; }
127  InputDataType& InputParameter() { return inputParameter; }
128 
130  OutputDataType const& OutputParameter() const { return outputParameter; }
132  OutputDataType& OutputParameter() { return outputParameter; }
133 
135  OutputDataType const& Delta() const { return delta; }
137  OutputDataType& Delta() { return delta; }
138 
140  OutputDataType const& Gradient() const { return gradient; }
142  OutputDataType& Gradient() { return gradient; }
143 
147  template<typename Archive>
148  void serialize(Archive& ar, const unsigned int /* version */);
149 
150  private:
152  DeleteVisitor deleteVisitor;
153 
155  LayerTypes<CustomLayers...> startModule;
156 
158  LayerTypes<CustomLayers...> inputModule;
159 
161  LayerTypes<CustomLayers...> feedbackModule;
162 
164  LayerTypes<CustomLayers...> transferModule;
165 
167  size_t rho;
168 
170  size_t forwardStep;
171 
173  size_t backwardStep;
174 
176  size_t gradientStep;
177 
179  bool deterministic;
180 
183  bool ownsLayer;
184 
186  OutputDataType parameters;
187 
189  LayerTypes<CustomLayers...> initialModule;
190 
192  LayerTypes<CustomLayers...> recurrentModule;
193 
195  std::vector<LayerTypes<CustomLayers...> > network;
196 
198  LayerTypes<CustomLayers...> mergeModule;
199 
201  WeightSizeVisitor weightSizeVisitor;
202 
204  DeltaVisitor deltaVisitor;
205 
207  OutputParameterVisitor outputParameterVisitor;
208 
210  std::vector<arma::mat> feedbackOutputParameter;
211 
213  OutputDataType delta;
214 
216  OutputDataType gradient;
217 
219  InputDataType inputParameter;
220 
222  OutputDataType outputParameter;
223 
225  arma::mat recurrentError;
226 }; // class Recurrent
227 
228 } // namespace ann
229 } // namespace mlpack
230 
231 // Include implementation.
232 #include "recurrent_impl.hpp"
233 
234 #endif
DeleteVisitor executes the destructor of the instantiated object.
OutputDataType const & Delta() const
Get the delta.
Definition: recurrent.hpp:135
.hpp
Definition: add_to_po.hpp:21
std::vector< LayerTypes< CustomLayers... > > & Model()
Get the model modules.
Definition: recurrent.hpp:112
bool & Deterministic()
Modify the value of the deterministic parameter.
Definition: recurrent.hpp:117
OutputDataType const & Parameters() const
Get the parameters.
Definition: recurrent.hpp:120
WeightSizeVisitor returns the number of weights of the given module.
OutputDataType & Delta()
Modify the delta.
Definition: recurrent.hpp:137
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...
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
OutputDataType const & Gradient() const
Get the gradient.
Definition: recurrent.hpp:140
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
InputDataType & InputParameter()
Modify the input parameter.
Definition: recurrent.hpp:127
OutputDataType & Gradient()
Modify the gradient.
Definition: recurrent.hpp:142
OutputParameterVisitor exposes the output parameter of the given module.
OutputDataType & Parameters()
Modify the parameters.
Definition: recurrent.hpp:122
Recurrent()
Default constructor—this will create a Recurrent object that can&#39;t be used, so be careful! Make sure...
Include all of the base components required to write mlpack methods, and the main mlpack Doxygen docu...
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...
InputDataType const & InputParameter() const
Get the input parameter.
Definition: recurrent.hpp:125
DeltaVisitor exposes the delta parameter of the given module.
bool Deterministic() const
The value of the deterministic parameter.
Definition: recurrent.hpp:115
~Recurrent()
Destructor to release allocated memory.
OutputDataType const & OutputParameter() const
Get the output parameter.
Definition: recurrent.hpp:130
OutputDataType & OutputParameter()
Modify the output parameter.
Definition: recurrent.hpp:132