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/copy_visitor.hpp"
21 #include "../visitor/output_parameter_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 
57  Recurrent(const Recurrent&);
58 
68  template<typename StartModuleType,
69  typename InputModuleType,
70  typename FeedbackModuleType,
71  typename TransferModuleType>
72  Recurrent(const StartModuleType& start,
73  const InputModuleType& input,
74  const FeedbackModuleType& feedback,
75  const TransferModuleType& transfer,
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<CustomLayers...> >& 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  OutputDataType const& OutputParameter() const { return outputParameter; }
130  OutputDataType& OutputParameter() { return outputParameter; }
131 
133  OutputDataType const& Delta() const { return delta; }
135  OutputDataType& Delta() { return delta; }
136 
138  OutputDataType const& Gradient() const { return gradient; }
140  OutputDataType& Gradient() { return gradient; }
141 
145  template<typename Archive>
146  void serialize(Archive& ar, const unsigned int /* version */);
147 
148  private:
150  DeleteVisitor deleteVisitor;
151 
153  CopyVisitor<CustomLayers...> copyVisitor;
154 
156  LayerTypes<CustomLayers...> startModule;
157 
159  LayerTypes<CustomLayers...> inputModule;
160 
162  LayerTypes<CustomLayers...> feedbackModule;
163 
165  LayerTypes<CustomLayers...> transferModule;
166 
168  size_t rho;
169 
171  size_t forwardStep;
172 
174  size_t backwardStep;
175 
177  size_t gradientStep;
178 
180  bool deterministic;
181 
184  bool ownsLayer;
185 
187  OutputDataType parameters;
188 
190  LayerTypes<CustomLayers...> initialModule;
191 
193  LayerTypes<CustomLayers...> recurrentModule;
194 
196  std::vector<LayerTypes<CustomLayers...> > network;
197 
199  LayerTypes<CustomLayers...> mergeModule;
200 
202  DeltaVisitor deltaVisitor;
203 
205  OutputParameterVisitor outputParameterVisitor;
206 
208  std::vector<arma::mat> feedbackOutputParameter;
209 
211  OutputDataType delta;
212 
214  OutputDataType gradient;
215 
217  OutputDataType outputParameter;
218 
220  arma::mat recurrentError;
221 }; // class Recurrent
222 
223 } // namespace ann
224 } // namespace mlpack
225 
226 // Include implementation.
227 #include "recurrent_impl.hpp"
228 
229 #endif
DeleteVisitor executes the destructor of the instantiated object.
OutputDataType const & Delta() const
Get the delta.
Definition: recurrent.hpp:133
strip_type.hpp
Definition: add_to_po.hpp:21
std::vector< LayerTypes< CustomLayers... > > & Model()
Get the model modules.
Definition: recurrent.hpp:115
bool & Deterministic()
Modify the value of the deterministic parameter.
Definition: recurrent.hpp:120
This visitor is to support copy constructor for neural network module.
OutputDataType const & Parameters() const
Get the parameters.
Definition: recurrent.hpp:123
OutputDataType & Delta()
Modify the delta.
Definition: recurrent.hpp:135
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:138
OutputDataType & Gradient()
Modify the gradient.
Definition: recurrent.hpp:140
OutputParameterVisitor exposes the output parameter of the given module.
OutputDataType & Parameters()
Modify the parameters.
Definition: recurrent.hpp:125
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...
boost::variant< Add< arma::mat, arma::mat > *, AddMerge< 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< RectifierFunction, arma::mat, arma::mat > *, BaseLayer< SoftplusFunction, arma::mat, arma::mat > *, BatchNorm< arma::mat, arma::mat > *, BilinearInterpolation< 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 > *, TransposedConvolution< NaiveConvolution< ValidConvolution >, NaiveConvolution< ValidConvolution >, 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 > *, Highway< arma::mat, arma::mat > *, Join< arma::mat, arma::mat > *, LayerNorm< arma::mat, arma::mat > *, LeakyReLU< arma::mat, arma::mat > *, CReLU< 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 > *, GRU< arma::mat, arma::mat > *, FastLSTM< 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 > *, Padding< arma::mat, arma::mat > *, PReLU< arma::mat, arma::mat > *, WeightNorm< arma::mat, arma::mat > *, MoreTypes, CustomLayers *... > LayerTypes
DeltaVisitor exposes the delta parameter of the given module.
bool Deterministic() const
The value of the deterministic parameter.
Definition: recurrent.hpp:118
~Recurrent()
Destructor to release allocated memory.
OutputDataType const & OutputParameter() const
Get the output parameter.
Definition: recurrent.hpp:128
OutputDataType & OutputParameter()
Modify the output parameter.
Definition: recurrent.hpp:130