gru.hpp
Go to the documentation of this file.
1 // Temporarily drop.
27 #ifndef MLPACK_METHODS_ANN_LAYER_GRU_HPP
28 #define MLPACK_METHODS_ANN_LAYER_GRU_HPP
29 
30 #include <list>
31 #include <limits>
32 
33 #include <mlpack/prereqs.hpp>
34 
35 #include "layer_types.hpp"
36 #include "add_merge.hpp"
37 #include "sequential.hpp"
38 
39 namespace mlpack {
40 namespace ann {
41 
52 template <
53  typename InputType = arma::mat,
54  typename OutputType = arma::mat
55 >
56 class GRU : public Layer<InputType, OutputType>
57 {
58  public:
60  GRU();
61 
69  GRU(const size_t inSize,
70  const size_t outSize,
71  const size_t rho = std::numeric_limits<size_t>::max());
72 
80  void Forward(const InputType& input, OutputType& output);
81 
91  void Backward(const InputType& /* input */,
92  const OutputType& gy,
93  OutputType& g);
94 
95  /*
96  * Calculate the gradient using the output delta and the input activation.
97  *
98  * @param input The input parameter used for calculating the gradient.
99  * @param error The calculated error.
100  * @param gradient The calculated gradient.
101  */
102  void Gradient(const InputType& input,
103  const OutputType& /* error */,
104  OutputType& /* gradient */);
105 
106  /*
107  * Resets the cell to accept a new input. This breaks the BPTT chain starts a
108  * new one.
109  *
110  * @param size The current maximum number of steps through time.
111  */
112  void ResetCell(const size_t size);
113 
115  size_t Rho() const { return rho; }
117  size_t& Rho() { return rho; }
118 
120  OutputType const& Parameters() const { return weights; }
122  OutputType& Parameters() { return weights; }
123 
125  std::vector<Layer<InputType, OutputType>*>& Model() { return network; }
126 
128  size_t InSize() const { return inSize; }
129 
131  size_t OutSize() const { return outSize; }
132 
134  size_t InputShape() const
135  {
136  return inSize;
137  }
138 
142  template<typename Archive>
143  void serialize(Archive& ar, const uint32_t /* version */);
144 
145  private:
147  size_t inSize;
148 
150  size_t outSize;
151 
153  size_t rho;
154 
156  size_t batchSize;
157 
159  OutputType weights;
160 
162  Layer<InputType, OutputType>* input2GateModule;
163 
165  Layer<InputType, OutputType>* output2GateModule;
166 
168  Layer<InputType, OutputType>* outputHidden2GateModule;
169 
171  Layer<InputType, OutputType>* inputGateModule;
172 
174  Layer<InputType, OutputType>* hiddenStateModule;
175 
177  Layer<InputType, OutputType>* forgetGateModule;
178 
180  std::vector<Layer<InputType, OutputType>*> network;
181 
183  size_t forwardStep;
184 
186  size_t backwardStep;
187 
189  size_t gradientStep;
190 
192  std::list<OutputType> outParameter;
193 
195  OutputType allZeros;
196 
198  typename std::list<OutputType>::iterator prevOutput;
199 
201  typename std::list<OutputType>::iterator backIterator;
202 
204  typename std::list<OutputType>::iterator gradIterator;
205 
207  OutputType prevError;
208 
210  bool deterministic;
211 
213  OutputType delta;
214 
216  OutputType gradient;
217 
219  OutputType outputParameter;
220 }; // class GRU
221 
222 } // namespace ann
223 } // namespace mlpack
224 
225 // Include implementation.
226 #include "gru_impl.hpp"
227 
228 #endif
OutputType const & Parameters() const
Get the parameters.
Definition: gru.hpp:120
void ResetCell(const size_t size)
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
constexpr auto size(Container const &container) noexcept -> decltype(container.size())
Definition: iterator.hpp:29
Linear algebra utility functions, generally performed on matrices or vectors.
size_t InputShape() const
Get the shape of the input.
Definition: gru.hpp:134
The core includes that mlpack expects; standard C++ includes and Armadillo.
std::vector< Layer< InputType, OutputType > * > & Model()
Get the model modules.
Definition: gru.hpp:125
void Backward(const InputType &, const OutputType &gy, OutputType &g)
Ordinary feed backward pass of a neural network, calculating the function f(x) by propagating x backw...
OutputType & Parameters()
Modify the parameters.
Definition: gru.hpp:122
size_t OutSize() const
Get the number of output units.
Definition: gru.hpp:131
constexpr T const & max(T const &lhs, T const &rhs)
Definition: algorithm.hpp:79
size_t Rho() const
Get the maximum number of steps to backpropagate through time (BPTT).
Definition: gru.hpp:115
void Gradient(const InputType &input, const OutputType &, OutputType &)
GRU()
Create the GRU object.
An implementation of a gru network layer.
Definition: gru.hpp:56
size_t InSize() const
Get the number of input units.
Definition: gru.hpp:128
A layer is an abstract class implementing common neural networks operations, such as convolution...
Definition: layer.hpp:52
void Forward(const InputType &input, OutputType &output)
Ordinary feed forward pass of a neural network, evaluating the function f(x) by propagating the activ...
size_t & Rho()
Modify the maximum number of steps to backpropagate through time (BPTT).
Definition: gru.hpp:117