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  InputDataType const& InputParameter() const { return inputParameter; }
141  InputDataType& InputParameter() { return inputParameter; }
142 
144  OutputDataType const& OutputParameter() const { return outputParameter; }
146  OutputDataType& OutputParameter() { return outputParameter; }
147 
149  OutputDataType const& Delta() const { return delta; }
151  OutputDataType& Delta() { return delta; }
152 
154  OutputDataType const& Gradient() const { return gradient; }
156  OutputDataType& Gradient() { return gradient; }
157 
159  std::vector<LayerTypes<> >& Model() { return network; }
160 
164  template<typename Archive>
165  void serialize(Archive& ar, const unsigned int /* version */);
166 
167  private:
169  size_t inSize;
170 
172  size_t outSize;
173 
175  size_t rho;
176 
178  size_t batchSize;
179 
181  OutputDataType weights;
182 
184  LayerTypes<> input2GateModule;
185 
187  LayerTypes<> output2GateModule;
188 
190  LayerTypes<> outputHidden2GateModule;
191 
193  LayerTypes<> inputGateModule;
194 
196  LayerTypes<> hiddenStateModule;
197 
199  LayerTypes<> forgetGateModule;
200 
202  OutputParameterVisitor outputParameterVisitor;
203 
205  DeltaVisitor deltaVisitor;
206 
208  DeleteVisitor deleteVisitor;
209 
211  std::vector<LayerTypes<> > network;
212 
214  size_t forwardStep;
215 
217  size_t backwardStep;
218 
220  size_t gradientStep;
221 
223  std::list<arma::mat> outParameter;
224 
226  arma::mat allZeros;
227 
229  std::list<arma::mat>::iterator prevOutput;
230 
232  std::list<arma::mat>::iterator backIterator;
233 
235  std::list<arma::mat>::iterator gradIterator;
236 
238  arma::mat prevError;
239 
241  bool deterministic;
242 
244  OutputDataType delta;
245 
247  OutputDataType gradient;
248 
250  InputDataType inputParameter;
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
InputDataType const & InputParameter() const
Get the input parameter.
Definition: gru.hpp:139
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:149
InputDataType & InputParameter()
Modify the input parameter.
Definition: gru.hpp:141
OutputDataType & Delta()
Modify the delta.
Definition: gru.hpp:151
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:159
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
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:144
bool Deterministic() const
The value of the deterministic parameter.
Definition: gru.hpp:124
OutputDataType & OutputParameter()
Modify the output parameter.
Definition: gru.hpp:146
OutputDataType & Gradient()
Modify the gradient.
Definition: gru.hpp:156
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
OutputDataType const & Gradient() const
Get the gradient.
Definition: gru.hpp:154
size_t & Rho()
Modify the maximum number of steps to backpropagate through time (BPTT).
Definition: gru.hpp:131