layer_norm.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_ANN_LAYER_LAYERNORM_HPP
13 #define MLPACK_METHODS_ANN_LAYER_LAYERNORM_HPP
14 
15 #include <mlpack/prereqs.hpp>
16 
17 namespace mlpack {
18 namespace ann {
19 
61 template <
62  typename InputDataType = arma::mat,
63  typename OutputDataType = arma::mat
64 >
65 class LayerNorm
66 {
67  public:
69  LayerNorm();
70 
77  LayerNorm(const size_t size, const double eps = 1e-8);
78 
82  void Reset();
83 
92  template<typename eT>
93  void Forward(const arma::Mat<eT>&& input, arma::Mat<eT>&& output);
94 
102  template<typename eT>
103  void Backward(const arma::Mat<eT>&& input,
104  arma::Mat<eT>&& gy,
105  arma::Mat<eT>&& g);
106 
114  template<typename eT>
115  void Gradient(const arma::Mat<eT>&& input,
116  arma::Mat<eT>&& error,
117  arma::Mat<eT>&& gradient);
118 
120  OutputDataType const& Parameters() const { return weights; }
122  OutputDataType& Parameters() { return weights; }
123 
125  InputDataType const& InputParameter() const { return inputParameter; }
127  InputDataType& InputParameter() { return inputParameter; }
128 
130  OutputDataType const& OutputParameter() const { return outputParameter; }
132  OutputDataType& OutputParameter() { return outputParameter; }
133 
135  OutputDataType const& Delta() const { return delta; }
137  OutputDataType& Delta() { return delta; }
138 
140  OutputDataType const& Gradient() const { return gradient; }
142  OutputDataType& Gradient() { return gradient; }
143 
145  OutputDataType Mean() { return mean; }
146 
148  OutputDataType Variance() { return variance; }
149 
153  template<typename Archive>
154  void serialize(Archive& ar, const unsigned int /* version */);
155 
156  private:
158  size_t size;
159 
161  double eps;
162 
164  OutputDataType gamma;
165 
167  OutputDataType beta;
168 
170  OutputDataType weights;
171 
173  OutputDataType mean;
174 
176  OutputDataType variance;
177 
179  OutputDataType gradient;
180 
182  OutputDataType delta;
183 
185  InputDataType inputParameter;
186 
188  OutputDataType outputParameter;
189 
191  OutputDataType normalized;
192 }; // class LayerNorm
193 
194 } // namespace ann
195 } // namespace mlpack
196 
197 // Include the implementation.
198 #include "layer_norm_impl.hpp"
199 
200 #endif
void Backward(const arma::Mat< eT > &&input, arma::Mat< eT > &&gy, arma::Mat< eT > &&g)
Backward pass through the layer.
OutputDataType Mean()
Get the mean across single training data.
Definition: layer_norm.hpp:145
OutputDataType const & OutputParameter() const
Get the output parameter.
Definition: layer_norm.hpp:130
.hpp
Definition: add_to_po.hpp:21
The core includes that mlpack expects; standard C++ includes and Armadillo.
OutputDataType const & Delta() const
Get the delta.
Definition: layer_norm.hpp:135
OutputDataType & Parameters()
Modify the parameters.
Definition: layer_norm.hpp:122
LayerNorm()
Create the LayerNorm object.
OutputDataType & OutputParameter()
Modify the output parameter.
Definition: layer_norm.hpp:132
OutputDataType & Delta()
Modify the delta.
Definition: layer_norm.hpp:137
Declaration of the Layer Normalization class.
Definition: layer_norm.hpp:65
OutputDataType & Gradient()
Modify the gradient.
Definition: layer_norm.hpp:142
InputDataType & InputParameter()
Modify the input parameter.
Definition: layer_norm.hpp:127
OutputDataType const & Parameters() const
Get the parameters.
Definition: layer_norm.hpp:120
OutputDataType Variance()
Get the variance across single training data.
Definition: layer_norm.hpp:148
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
OutputDataType const & Gradient() const
Get the gradient.
Definition: layer_norm.hpp:140
void Forward(const arma::Mat< eT > &&input, arma::Mat< eT > &&output)
Forward pass of Layer Normalization.
void Reset()
Reset the layer parameters.
InputDataType const & InputParameter() const
Get the input parameter.
Definition: layer_norm.hpp:125