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.
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...
void ResetCell(const size_t size)
OutputDataType & Parameters()
Modify the parameters.
Definition: gru.hpp:132
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
Linear algebra utility functions, generally performed on matrices or vectors.
OutputDataType const & Delta() const
Get the delta.
Definition: gru.hpp:140
size_t InputShape() const
Get the shape of the input.
Definition: gru.hpp:159
OutputDataType & Delta()
Modify the delta.
Definition: gru.hpp:142
The core includes that mlpack expects; standard C++ includes and Armadillo.
boost::variant< AdaptiveMaxPooling< arma::mat, arma::mat > *, AdaptiveMeanPooling< arma::mat, arma::mat > *, Add< arma::mat, arma::mat > *, AddMerge< arma::mat, arma::mat > *, AlphaDropout< 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< SoftplusFunction, arma::mat, arma::mat > *, BaseLayer< RectifierFunction, arma::mat, arma::mat > *, BatchNorm< arma::mat, arma::mat > *, BilinearInterpolation< arma::mat, arma::mat > *, CELU< 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 > *, CReLU< arma::mat, arma::mat > *, DropConnect< arma::mat, arma::mat > *, Dropout< arma::mat, arma::mat > *, ELU< arma::mat, arma::mat > *, FastLSTM< arma::mat, arma::mat > *, FlexibleReLU< arma::mat, arma::mat > *, GRU< arma::mat, arma::mat > *, HardTanH< arma::mat, arma::mat > *, Join< arma::mat, arma::mat > *, LayerNorm< arma::mat, arma::mat > *, LeakyReLU< 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 > *, 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 > *, NoisyLinear< arma::mat, arma::mat > *, Padding< arma::mat, arma::mat > *, PReLU< arma::mat, arma::mat > *, Softmax< arma::mat, arma::mat > *, SpatialDropout< arma::mat, arma::mat > *, TransposedConvolution< NaiveConvolution< ValidConvolution >, NaiveConvolution< ValidConvolution >, NaiveConvolution< ValidConvolution >, arma::mat, arma::mat > *, WeightNorm< arma::mat, arma::mat > *, MoreTypes, CustomLayers *... > LayerTypes
bool & Deterministic()
Modify the value of the deterministic parameter.
Definition: gru.hpp:122
std::vector< LayerTypes<> > & Model()
Get the model modules.
Definition: gru.hpp:150
size_t OutSize() const
Get the number of output units.
Definition: gru.hpp:156
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...
size_t Rho() const
Get the maximum number of steps to backpropagate through time (BPTT).
Definition: gru.hpp:125
GRU()
Create the GRU object.
OutputParameterVisitor exposes the output parameter of the given module.
OutputDataType const & Parameters() const
Get the parameters.
Definition: gru.hpp:130
An implementation of a gru network layer.
Definition: gru.hpp:58
size_t InSize() const
Get the number of input units.
Definition: gru.hpp:153
DeltaVisitor exposes the delta parameter of the given module.
OutputDataType const & OutputParameter() const
Get the output parameter.
Definition: gru.hpp:135
bool Deterministic() const
The value of the deterministic parameter.
Definition: gru.hpp:120
OutputDataType & OutputParameter()
Modify the output parameter.
Definition: gru.hpp:137
OutputDataType & Gradient()
Modify the gradient.
Definition: gru.hpp:147
OutputDataType const & Gradient() const
Get the gradient.
Definition: gru.hpp:145
size_t & Rho()
Modify the maximum number of steps to backpropagate through time (BPTT).
Definition: gru.hpp:127