lstm.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_ANN_LAYER_LSTM_HPP
13 #define MLPACK_METHODS_ANN_LAYER_LSTM_HPP
14 
15 #include <mlpack/prereqs.hpp>
16 #include <limits>
17 
18 namespace mlpack {
19 namespace ann {
20 
58 template <
59  typename InputDataType = arma::mat,
60  typename OutputDataType = arma::mat
61 >
62 class LSTM
63 {
64  public:
66  LSTM();
67 
75  LSTM(const size_t inSize,
76  const size_t outSize,
77  const size_t rho = std::numeric_limits<size_t>::max());
78 
86  template<typename InputType, typename OutputType>
87  void Forward(InputType&& input, OutputType&& output);
88 
98  template<typename InputType, typename OutputType>
99  void Forward(InputType&& input,
100  OutputType&& output,
101  OutputType&& cellState,
102  bool useCellState = false);
103 
113  template<typename InputType, typename ErrorType, typename GradientType>
114  void Backward(const InputType&& input,
115  ErrorType&& gy,
116  GradientType&& g);
117 
118  /*
119  * Reset the layer parameter.
120  */
121  void Reset();
122 
123  /*
124  * Resets the cell to accept a new input. This breaks the BPTT chain starts a
125  * new one.
126  *
127  * @param size The current maximum number of steps through time.
128  */
129  void ResetCell(const size_t size);
130 
131  /*
132  * Calculate the gradient using the output delta and the input activation.
133  *
134  * @param input The input parameter used for calculating the gradient.
135  * @param error The calculated error.
136  * @param gradient The calculated gradient.
137  */
138  template<typename InputType, typename ErrorType, typename GradientType>
139  void Gradient(InputType&& input,
140  ErrorType&& error,
141  GradientType&& gradient);
142 
144  size_t Rho() const { return rho; }
146  size_t& Rho() { return rho; }
147 
149  OutputDataType const& Parameters() const { return weights; }
151  OutputDataType& Parameters() { return weights; }
152 
154  OutputDataType const& OutputParameter() const { return outputParameter; }
156  OutputDataType& OutputParameter() { return outputParameter; }
157 
159  OutputDataType const& Delta() const { return delta; }
161  OutputDataType& Delta() { return delta; }
162 
164  OutputDataType const& Gradient() const { return grad; }
166  OutputDataType& Gradient() { return grad; }
167 
171  template<typename Archive>
172  void serialize(Archive& ar, const unsigned int /* version */);
173 
174  private:
176  size_t inSize;
177 
179  size_t outSize;
180 
182  size_t rho;
183 
185  size_t forwardStep;
186 
188  size_t backwardStep;
189 
191  size_t gradientStep;
192 
194  OutputDataType weights;
195 
197  OutputDataType prevOutput;
198 
200  size_t batchSize;
201 
203  size_t batchStep;
204 
207  size_t gradientStepIdx;
208 
210  OutputDataType cellActivationError;
211 
213  OutputDataType delta;
214 
216  OutputDataType grad;
217 
219  OutputDataType outputParameter;
220 
222  OutputDataType output2GateInputWeight;
223 
225  OutputDataType input2GateInputWeight;
226 
228  OutputDataType input2GateInputBias;
229 
231  OutputDataType cell2GateInputWeight;
232 
234  OutputDataType output2GateForgetWeight;
235 
237  OutputDataType input2GateForgetWeight;
238 
240  OutputDataType input2GateForgetBias;
241 
243  OutputDataType cell2GateForgetWeight;
244 
246  OutputDataType output2GateOutputWeight;
247 
249  OutputDataType input2GateOutputWeight;
250 
252  OutputDataType input2GateOutputBias;
253 
255  OutputDataType cell2GateOutputWeight;
256 
258  OutputDataType inputGate;
259 
261  OutputDataType forgetGate;
262 
264  OutputDataType hiddenLayer;
265 
267  OutputDataType outputGate;
268 
270  OutputDataType inputGateActivation;
271 
273  OutputDataType forgetGateActivation;
274 
276  OutputDataType outputGateActivation;
277 
279  OutputDataType hiddenLayerActivation;
280 
282  OutputDataType input2HiddenWeight;
283 
285  OutputDataType input2HiddenBias;
286 
288  OutputDataType output2HiddenWeight;
289 
291  OutputDataType cell;
292 
294  OutputDataType cellActivation;
295 
297  OutputDataType forgetGateError;
298 
300  OutputDataType outputGateError;
301 
303  OutputDataType prevError;
304 
306  OutputDataType outParameter;
307 
309  OutputDataType inputCellError;
310 
312  OutputDataType inputGateError;
313 
315  OutputDataType hiddenError;
316 
318  size_t rhoSize;
319 
321  size_t bpttSteps;
322 }; // class LSTM
323 
324 } // namespace ann
325 } // namespace mlpack
326 
327 // Include implementation.
328 #include "lstm_impl.hpp"
329 
330 #endif
OutputDataType const & OutputParameter() const
Get the output parameter.
Definition: lstm.hpp:154
strip_type.hpp
Definition: add_to_po.hpp:21
The core includes that mlpack expects; standard C++ includes and Armadillo.
OutputDataType & Gradient()
Modify the gradient.
Definition: lstm.hpp:166
OutputDataType & OutputParameter()
Modify the output parameter.
Definition: lstm.hpp:156
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
size_t Rho() const
Get the maximum number of steps to backpropagate through time (BPTT).
Definition: lstm.hpp:144
OutputDataType const & Parameters() const
Get the parameters.
Definition: lstm.hpp:149
void Forward(InputType &&input, OutputType &&output)
Ordinary feed-forward pass of a neural network, evaluating the function f(x) by propagating the activ...
OutputDataType const & Delta() const
Get the delta.
Definition: lstm.hpp:159
void Backward(const InputType &&input, ErrorType &&gy, GradientType &&g)
Ordinary feed backward pass of a neural network, calculating the function f(x) by propagating x backw...
LSTM()
Create the LSTM object.
OutputDataType const & Gradient() const
Get the gradient.
Definition: lstm.hpp:164
void ResetCell(const size_t size)
OutputDataType & Delta()
Modify the delta.
Definition: lstm.hpp:161
OutputDataType & Parameters()
Modify the parameters.
Definition: lstm.hpp:151
size_t & Rho()
Modify the maximum number of steps to backpropagate through time (BPTT).
Definition: lstm.hpp:146