gru.hpp
Go to the documentation of this file.
1 
25 #ifndef MLPACK_METHODS_ANN_LAYER_GRU_HPP
26 #define MLPACK_METHODS_ANN_LAYER_GRU_HPP
27 
28 #include <list>
29 #include <limits>
30 
31 #include <mlpack/prereqs.hpp>
32 
33 #include "../visitor/delta_visitor.hpp"
34 #include "../visitor/output_parameter_visitor.hpp"
35 
36 #include "layer_types.hpp"
37 #include "add_merge.hpp"
38 #include "sequential.hpp"
39 
40 namespace mlpack {
41 namespace ann {
42 
53 template <
54  typename InputDataType = arma::mat,
55  typename OutputDataType = arma::mat
56 >
57 class GRU
58 {
59  public:
61  GRU();
62 
70  GRU(const size_t inSize,
71  const size_t outSize,
72  const size_t rho = std::numeric_limits<size_t>::max());
73 
77  ~GRU();
78 
86  template<typename eT>
87  void Forward(arma::Mat<eT>&& input, arma::Mat<eT>&& output);
88 
98  template<typename eT>
99  void Backward(const arma::Mat<eT>&& /* input */,
100  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(arma::Mat<eT>&& input,
112  arma::Mat<eT>&& /* error */,
113  arma::Mat<eT>&& /* gradient */);
114 
115  /*
116  * Resets the cell to accept a new input. This breaks the BPTT chain starts a
117  * new one.
118  *
119  * @param size The current maximum number of steps through time.
120  */
121  void ResetCell(const size_t size);
122 
124  bool Deterministic() const { return deterministic; }
126  bool& Deterministic() { return deterministic; }
127 
129  size_t Rho() const { return rho; }
131  size_t& Rho() { return rho; }
132 
134  OutputDataType const& Parameters() const { return weights; }
136  OutputDataType& Parameters() { return weights; }
137 
139  OutputDataType const& OutputParameter() const { return outputParameter; }
141  OutputDataType& OutputParameter() { return outputParameter; }
142 
144  OutputDataType const& Delta() const { return delta; }
146  OutputDataType& Delta() { return delta; }
147 
149  OutputDataType const& Gradient() const { return gradient; }
151  OutputDataType& Gradient() { return gradient; }
152 
154  std::vector<LayerTypes<> >& Model() { return network; }
155 
159  template<typename Archive>
160  void serialize(Archive& ar, const unsigned int /* version */);
161 
162  private:
164  size_t inSize;
165 
167  size_t outSize;
168 
170  size_t rho;
171 
173  size_t batchSize;
174 
176  OutputDataType weights;
177 
179  LayerTypes<> input2GateModule;
180 
182  LayerTypes<> output2GateModule;
183 
185  LayerTypes<> outputHidden2GateModule;
186 
188  LayerTypes<> inputGateModule;
189 
191  LayerTypes<> hiddenStateModule;
192 
194  LayerTypes<> forgetGateModule;
195 
197  OutputParameterVisitor outputParameterVisitor;
198 
200  DeltaVisitor deltaVisitor;
201 
203  DeleteVisitor deleteVisitor;
204 
206  std::vector<LayerTypes<> > network;
207 
209  size_t forwardStep;
210 
212  size_t backwardStep;
213 
215  size_t gradientStep;
216 
218  std::list<arma::mat> outParameter;
219 
221  arma::mat allZeros;
222 
224  std::list<arma::mat>::iterator prevOutput;
225 
227  std::list<arma::mat>::iterator backIterator;
228 
230  std::list<arma::mat>::iterator gradIterator;
231 
233  arma::mat prevError;
234 
236  bool deterministic;
237 
239  OutputDataType delta;
240 
242  OutputDataType gradient;
243 
245  OutputDataType outputParameter;
246 }; // class GRU
247 
248 } // namespace ann
249 } // namespace mlpack
250 
251 // Include implementation.
252 #include "gru_impl.hpp"
253 
254 #endif
DeleteVisitor executes the destructor of the instantiated object.
void ResetCell(const size_t size)
OutputDataType & Parameters()
Modify the parameters.
Definition: gru.hpp:136
.hpp
Definition: add_to_po.hpp:21
OutputDataType const & Delta() const
Get the delta.
Definition: gru.hpp:144
OutputDataType & Delta()
Modify the delta.
Definition: gru.hpp:146
The core includes that mlpack expects; standard C++ includes and Armadillo.
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()
Modify the value of the deterministic parameter.
Definition: gru.hpp:126
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...
std::vector< LayerTypes<> > & Model()
Get the model modules.
Definition: gru.hpp:154
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< 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 > *, 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 > *, MoreTypes, CustomLayers *... > LayerTypes
size_t Rho() const
Get the maximum number of steps to backpropagate through time (BPTT).
Definition: gru.hpp:129
GRU()
Create the GRU object.
OutputParameterVisitor exposes the output parameter of the given module.
OutputDataType const & Parameters() const
Get the parameters.
Definition: gru.hpp:134
An implementation of a gru network layer.
Definition: gru.hpp:57
DeltaVisitor exposes the delta parameter of the given module.
~GRU()
Delete the GRU and the layers it holds.
OutputDataType const & OutputParameter() const
Get the output parameter.
Definition: gru.hpp:139
bool Deterministic() const
The value of the deterministic parameter.
Definition: gru.hpp:124
OutputDataType & OutputParameter()
Modify the output parameter.
Definition: gru.hpp:141
OutputDataType & Gradient()
Modify the gradient.
Definition: gru.hpp:151
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
OutputDataType const & Gradient() const
Get the gradient.
Definition: gru.hpp:149
size_t & Rho()
Modify the maximum number of steps to backpropagate through time (BPTT).
Definition: gru.hpp:131