mlpack  master
lstm.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_ANN_LAYER_LSTM_HPP
14 #define MLPACK_METHODS_ANN_LAYER_LSTM_HPP
15 
16 #include <mlpack/prereqs.hpp>
17 
18 #include <boost/ptr_container/ptr_vector.hpp>
19 
20 #include "../visitor/delta_visitor.hpp"
21 #include "../visitor/output_parameter_visitor.hpp"
22 
23 #include "layer_types.hpp"
24 #include "add_merge.hpp"
25 #include "sequential.hpp"
26 
27 namespace mlpack {
28 namespace ann {
29 
42 template <
43  typename InputDataType = arma::mat,
44  typename OutputDataType = arma::mat
45 >
46 class LSTM
47 {
48  public:
50  LSTM();
51 
59  LSTM(const size_t inSize, const size_t outSize, const size_t rho);
60 
68  template<typename eT>
69  void Forward(arma::Mat<eT>&& input, arma::Mat<eT>&& output);
70 
80  template<typename eT>
81  void Backward(const arma::Mat<eT>&& /* input */,
82  arma::Mat<eT>&& gy,
83  arma::Mat<eT>&& g);
84 
85  /*
86  * Calculate the gradient using the output delta and the input activation.
87  *
88  * @param input The input parameter used for calculating the gradient.
89  * @param error The calculated error.
90  * @param gradient The calculated gradient.
91  */
92  template<typename eT>
93  void Gradient(arma::Mat<eT>&& input,
94  arma::Mat<eT>&& /* error */,
95  arma::Mat<eT>&& /* gradient */);
96 
98  bool Deterministic() const { return deterministic; }
100  bool& Deterministic() { return deterministic; }
101 
103  size_t Rho() const { return rho; }
105  size_t& Rho() { return rho; }
106 
108  OutputDataType const& Parameters() const { return weights; }
110  OutputDataType& Parameters() { return weights; }
111 
113  InputDataType const& InputParameter() const { return inputParameter; }
115  InputDataType& InputParameter() { return inputParameter; }
116 
118  OutputDataType const& OutputParameter() const { return outputParameter; }
120  OutputDataType& OutputParameter() { return outputParameter; }
121 
123  OutputDataType const& Delta() const { return delta; }
125  OutputDataType& Delta() { return delta; }
126 
128  OutputDataType const& Gradient() const { return gradient; }
130  OutputDataType& Gradient() { return gradient; }
131 
133  std::vector<LayerTypes>& Model() { return network; }
134 
138  template<typename Archive>
139  void Serialize(Archive& ar, const unsigned int /* version */);
140 
141  private:
142 
144  size_t inSize;
145 
147  size_t outSize;
148 
150  size_t rho;
151 
153  OutputDataType weights;
154 
156  arma::mat prevOutput;
157 
159  arma::mat prevCell;
160 
163 
166 
169 
172 
175 
178 
181 
184 
187 
190 
192  std::vector<LayerTypes> network;
193 
195  size_t forwardStep;
196 
198  size_t backwardStep;
199 
201  size_t gradientStep;
202 
204  std::vector<arma::mat> cellParameter;
205 
207  std::vector<arma::mat> outParameter;
208 
210  arma::mat prevError;
211 
214 
216  arma::mat forgetGateError;
217 
220 
222  OutputDataType delta;
223 
225  OutputDataType gradient;
226 
228  InputDataType inputParameter;
229 
231  OutputDataType outputParameter;
232 }; // class LSTM
233 
234 } // namespace ann
235 } // namespace mlpack
236 
237 // Include implementation.
238 #include "lstm_impl.hpp"
239 
240 #endif
LayerTypes output2GateModule
Locally-stored output 2 gate module.
Definition: lstm.hpp:165
arma::mat prevCell
Locally-stored previous cell state.
Definition: lstm.hpp:159
arma::mat prevOutput
Locally-stored previous output.
Definition: lstm.hpp:156
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
OutputParameterVisitor outputParameterVisitor
Locally-stored output parameter visitor.
Definition: lstm.hpp:186
InputDataType const & InputParameter() const
Get the input parameter.
Definition: lstm.hpp:113
size_t outSize
Locally-stored number of output units.
Definition: lstm.hpp:147
OutputDataType & OutputParameter()
Modify the output parameter.
Definition: lstm.hpp:120
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...
The core includes that mlpack expects; standard C++ includes and Armadillo.
size_t inSize
Locally-stored number of input units.
Definition: lstm.hpp:144
std::vector< LayerTypes > & Model()
Get the model modules.
Definition: lstm.hpp:133
std::vector< LayerTypes > network
Locally-stored list of network modules.
Definition: lstm.hpp:192
OutputDataType & Parameters()
Modify the parameters.
Definition: lstm.hpp:110
size_t gradientStep
Locally-stored number of gradient steps.
Definition: lstm.hpp:201
LayerTypes cellActivationModule
Locally-stored cell activation module.
Definition: lstm.hpp:183
size_t & Rho()
Modify the maximum number of steps to backpropagate through time (BPTT).
Definition: lstm.hpp:105
OutputDataType delta
Locally-stored delta object.
Definition: lstm.hpp:222
LayerTypes forgetGateModule
Locally-stored forget gate module.
Definition: lstm.hpp:174
DeltaVisitor deltaVisitor
Locally-stored delta visitor.
Definition: lstm.hpp:189
LayerTypes inputGateModule
Locally-stored input gate module.
Definition: lstm.hpp:168
OutputDataType const & Delta() const
Get the delta.
Definition: lstm.hpp:123
OutputDataType const & Parameters() const
Get the parameters.
Definition: lstm.hpp:108
OutputDataType gradient
Locally-stored gradient object.
Definition: lstm.hpp:225
OutputParameterVisitor exposes the output parameter of the given module.
size_t Rho() const
Get the maximum number of steps to backpropagate through time (BPTT).
Definition: lstm.hpp:103
InputDataType inputParameter
Locally-stored input parameter object.
Definition: lstm.hpp:228
size_t rho
Number of steps to backpropagate through time (BPTT).
Definition: lstm.hpp:150
void Serialize(Archive &ar, const unsigned int)
Serialize the layer.
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 > *, 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 > *, 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 > *, MaxPooling< arma::mat, arma::mat > *, MeanPooling< arma::mat, arma::mat > *, MeanSquaredError< 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 > * > LayerTypes
OutputDataType & Gradient()
Modify the gradient.
Definition: lstm.hpp:130
OutputDataType outputParameter
Locally-stored output parameter object.
Definition: lstm.hpp:231
size_t forwardStep
Locally-stored number of forward steps.
Definition: lstm.hpp:195
LayerTypes outputGateModule
Locally-stored output gate module.
Definition: lstm.hpp:177
LayerTypes input2GateModule
Locally-stored input 2 gate module.
Definition: lstm.hpp:162
bool & Deterministic()
Modify the value of the deterministic parameter.
Definition: lstm.hpp:100
OutputDataType const & OutputParameter() const
Get the output parameter.
Definition: lstm.hpp:118
arma::mat forgetGateError
Locally-stored foget gate error.
Definition: lstm.hpp:216
LayerTypes cellModule
Locally-stored cell module.
Definition: lstm.hpp:180
bool deterministic
If true dropout and scaling is disabled, see notes above.
Definition: lstm.hpp:219
DeltaVisitor exposes the delta parameter of the given module.
OutputDataType & Delta()
Modify the delta.
Definition: lstm.hpp:125
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< arma::mat > cellParameter
Locally-stored cell parameters.
Definition: lstm.hpp:204
LSTM()
Create the LSTM object.
OutputDataType const & Gradient() const
Get the gradient.
Definition: lstm.hpp:128
LayerTypes hiddenStateModule
Locally-stored hidden state module.
Definition: lstm.hpp:171
InputDataType & InputParameter()
Modify the input parameter.
Definition: lstm.hpp:115
std::vector< arma::mat > outParameter
Locally-stored output parameters.
Definition: lstm.hpp:207
bool Deterministic() const
The value of the deterministic parameter.
Definition: lstm.hpp:98
size_t backwardStep
Locally-stored number of backward steps.
Definition: lstm.hpp:198
arma::mat prevError
Locally-stored previous error.
Definition: lstm.hpp:210
arma::mat cellActivationError
Locally-stored cell activation error.
Definition: lstm.hpp:213
OutputDataType weights
Locally-stored weight object.
Definition: lstm.hpp:153