mlpack
gru.hpp
Go to the documentation of this file.
1 
26 #ifndef MLPACK_METHODS_ANN_LAYER_GRU_HPP
27 #define MLPACK_METHODS_ANN_LAYER_GRU_HPP
28 
29 #include <list>
30 #include <limits>
31 
32 #include <mlpack/prereqs.hpp>
33 
34 #include "../visitor/delta_visitor.hpp"
35 #include "../visitor/output_parameter_visitor.hpp"
36 
37 #include "layer_types.hpp"
38 #include "add_merge.hpp"
39 #include "sequential.hpp"
40 
41 namespace mlpack {
42 namespace ann {
43 
54 template <
55  typename InputDataType = arma::mat,
56  typename OutputDataType = arma::mat
57 >
58 class GRU
59 {
60  public:
62  GRU();
63 
71  GRU(const size_t inSize,
72  const size_t outSize,
73  const size_t rho = std::numeric_limits<size_t>::max());
74 
82  template<typename eT>
83  void Forward(const arma::Mat<eT>& input, arma::Mat<eT>& output);
84 
94  template<typename eT>
95  void Backward(const arma::Mat<eT>& /* input */,
96  const 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(const arma::Mat<eT>& input,
108  const arma::Mat<eT>& /* error */,
109  arma::Mat<eT>& /* gradient */);
110 
111  /*
112  * Resets the cell to accept a new input. This breaks the BPTT chain starts a
113  * new one.
114  *
115  * @param size The current maximum number of steps through time.
116  */
117  void ResetCell(const size_t size);
118 
120  bool Deterministic() const { return deterministic; }
122  bool& Deterministic() { return deterministic; }
123 
125  size_t Rho() const { return rho; }
127  size_t& Rho() { return rho; }
128 
130  OutputDataType const& Parameters() const { return weights; }
132  OutputDataType& Parameters() { return weights; }
133 
135  OutputDataType const& OutputParameter() const { return outputParameter; }
137  OutputDataType& OutputParameter() { return outputParameter; }
138 
140  OutputDataType const& Delta() const { return delta; }
142  OutputDataType& Delta() { return delta; }
143 
145  OutputDataType const& Gradient() const { return gradient; }
147  OutputDataType& Gradient() { return gradient; }
148 
150  std::vector<LayerTypes<> >& Model() { return network; }
151 
153  size_t InSize() const { return inSize; }
154 
156  size_t OutSize() const { return outSize; }
157 
159  size_t InputShape() const
160  {
161  return inSize;
162  }
163 
167  template<typename Archive>
168  void serialize(Archive& ar, const uint32_t /* version */);
169 
170  private:
172  size_t inSize;
173 
175  size_t outSize;
176 
178  size_t rho;
179 
181  size_t batchSize;
182 
184  OutputDataType weights;
185 
187  LayerTypes<> input2GateModule;
188 
190  LayerTypes<> output2GateModule;
191 
193  LayerTypes<> outputHidden2GateModule;
194 
196  LayerTypes<> inputGateModule;
197 
199  LayerTypes<> hiddenStateModule;
200 
202  LayerTypes<> forgetGateModule;
203 
205  OutputParameterVisitor outputParameterVisitor;
206 
208  DeltaVisitor deltaVisitor;
209 
211  DeleteVisitor deleteVisitor;
212 
214  std::vector<LayerTypes<> > network;
215 
217  size_t forwardStep;
218 
220  size_t backwardStep;
221 
223  size_t gradientStep;
224 
226  std::list<arma::mat> outParameter;
227 
229  arma::mat allZeros;
230 
232  std::list<arma::mat>::iterator prevOutput;
233 
235  std::list<arma::mat>::iterator backIterator;
236 
238  std::list<arma::mat>::iterator gradIterator;
239 
241  arma::mat prevError;
242 
244  bool deterministic;
245 
247  OutputDataType delta;
248 
250  OutputDataType gradient;
251 
253  OutputDataType outputParameter;
254 }; // class GRU
255 
256 } // namespace ann
257 } // namespace mlpack
258 
259 // Include implementation.
260 #include "gru_impl.hpp"
261 
262 #endif
DeleteVisitor executes the destructor of the instantiated object.
Definition: delete_visitor.hpp:27
OutputDataType & Parameters()
Modify the parameters.
Definition: gru.hpp:132
size_t & Rho()
Modify the maximum number of steps to backpropagate through time (BPTT).
Definition: gru.hpp:127
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...
Definition: gru_impl.hpp:81
OutputDataType const & OutputParameter() const
Get the output parameter.
Definition: gru.hpp:135
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
OutputDataType & Gradient()
Modify the gradient.
Definition: gru.hpp:147
bool & Deterministic()
Modify the value of the deterministic parameter.
Definition: gru.hpp:122
GRU()
Create the GRU object.
Definition: gru_impl.hpp:27
The core includes that mlpack expects; standard C++ includes and Armadillo.
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
Definition: gru_impl.hpp:382
bool Deterministic() const
The value of the deterministic parameter.
Definition: gru.hpp:120
OutputDataType const & Parameters() const
Get the parameters.
Definition: gru.hpp:130
size_t Rho() const
Get the maximum number of steps to backpropagate through time (BPTT).
Definition: gru.hpp:125
size_t InputShape() const
Get the shape of the input.
Definition: gru.hpp:159
OutputParameterVisitor exposes the output parameter of the given module.
Definition: output_parameter_visitor.hpp:27
std::vector< LayerTypes<> > & Model()
Get the model modules.
Definition: gru.hpp:150
An implementation of a gru network layer.
Definition: gru.hpp:58
OutputDataType & OutputParameter()
Modify the output parameter.
Definition: gru.hpp:137
size_t OutSize() const
Get the number of output units.
Definition: gru.hpp:156
DeltaVisitor exposes the delta parameter of the given module.
Definition: delta_visitor.hpp:27
OutputDataType & Delta()
Modify the delta.
Definition: gru.hpp:142
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...
Definition: gru_impl.hpp:196
OutputDataType const & Gradient() const
Get the gradient.
Definition: gru.hpp:145
size_t InSize() const
Get the number of input units.
Definition: gru.hpp:153
OutputDataType const & Delta() const
Get the delta.
Definition: gru.hpp:140