mlpack
weight_norm.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_ANN_LAYER_WEIGHTNORM_HPP
13 #define MLPACK_METHODS_ANN_LAYER_WEIGHTNORM_HPP
14 
15 #include <mlpack/prereqs.hpp>
16 #include "layer_types.hpp"
17 
18 #include "../visitor/delete_visitor.hpp"
19 #include "../visitor/delta_visitor.hpp"
20 #include "../visitor/output_parameter_visitor.hpp"
21 #include "../visitor/reset_visitor.hpp"
22 #include "../visitor/weight_size_visitor.hpp"
23 #include "../visitor/weight_set_visitor.hpp"
24 
25 namespace mlpack {
26 namespace ann {
27 
56 template <
57  typename InputDataType = arma::mat,
58  typename OutputDataType = arma::mat,
59  typename... CustomLayers
60 >
61 class WeightNorm
62 {
63  public:
69  WeightNorm(LayerTypes<CustomLayers...> layer = LayerTypes<CustomLayers...>());
70 
72  ~WeightNorm();
73 
77  void Reset();
78 
88  template<typename eT>
89  void Forward(const arma::Mat<eT>& input, arma::Mat<eT>& output);
90 
99  template<typename eT>
100  void Backward(const arma::Mat<eT>& input,
101  const arma::Mat<eT>& gy,
102  arma::Mat<eT>& g);
103 
112  template<typename eT>
113  void Gradient(const arma::Mat<eT>& input,
114  const arma::Mat<eT>& error,
115  arma::Mat<eT>& gradient);
116 
118  OutputDataType const& Delta() const { return delta; }
120  OutputDataType& Delta() { return delta; }
121 
123  OutputDataType const& Gradient() const { return gradient; }
125  OutputDataType& Gradient() { return gradient; }
126 
128  OutputDataType const& OutputParameter() const { return outputParameter; }
130  OutputDataType& OutputParameter() { return outputParameter; }
131 
133  OutputDataType const& Parameters() const { return weights; }
135  OutputDataType& Parameters() { return weights; }
136 
138  LayerTypes<CustomLayers...> const& Layer() { return wrappedLayer; }
139 
143  template<typename Archive>
144  void serialize(Archive& ar, const uint32_t /* version */);
145 
146  private:
148  size_t biasWeightSize;
149 
151  DeleteVisitor deleteVisitor;
152 
154  OutputDataType delta;
155 
157  DeltaVisitor deltaVisitor;
158 
160  OutputDataType gradient;
161 
163  LayerTypes<CustomLayers...> wrappedLayer;
164 
166  size_t layerWeightSize;
167 
169  OutputDataType outputParameter;
170 
172  OutputParameterVisitor outputParameterVisitor;
173 
175  void ResetGradients(arma::mat& gradient);
176 
178  ResetVisitor resetVisitor;
179 
181  OutputDataType scalarParameter;
182 
184  OutputDataType vectorParameter;
185 
187  OutputDataType weights;
188 
190  WeightSizeVisitor weightSizeVisitor;
191 
193  OutputDataType layerGradients;
194 
196  OutputDataType layerWeights;
197 }; // class WeightNorm
198 
199 } // namespace ann
200 } // namespace mlpack
201 
202 // Include the implementation.
203 #include "weight_norm_impl.hpp"
204 
205 #endif
DeleteVisitor executes the destructor of the instantiated object.
Definition: delete_visitor.hpp:27
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
void Reset()
Reset the layer parameters.
Definition: weight_norm_impl.hpp:49
~WeightNorm()
Destructor to release allocated memory.
Definition: weight_norm_impl.hpp:42
void Backward(const arma::Mat< eT > &input, const arma::Mat< eT > &gy, arma::Mat< eT > &g)
Backward pass through the layer.
Definition: weight_norm_impl.hpp:88
The core includes that mlpack expects; standard C++ includes and Armadillo.
OutputDataType const & OutputParameter() const
Get the output parameter.
Definition: weight_norm.hpp:128
WeightSizeVisitor returns the number of weights of the given module.
Definition: weight_size_visitor.hpp:27
void Forward(const arma::Mat< eT > &input, arma::Mat< eT > &output)
Forward pass of the WeightNorm layer.
Definition: weight_norm_impl.hpp:70
LayerTypes< CustomLayers... > const & Layer()
Get the wrapped layer.
Definition: weight_norm.hpp:138
WeightNorm(LayerTypes< CustomLayers... > layer=LayerTypes< CustomLayers... >())
Create the WeightNorm layer object.
Definition: weight_norm_impl.hpp:29
OutputDataType & Gradient()
Modify the gradient.
Definition: weight_norm.hpp:125
OutputDataType & Delta()
Modify the delta.
Definition: weight_norm.hpp:120
ResetVisitor executes the Reset() function.
Definition: reset_visitor.hpp:26
OutputParameterVisitor exposes the output parameter of the given module.
Definition: output_parameter_visitor.hpp:27
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
Definition: weight_norm_impl.hpp:144
OutputDataType const & Delta() const
Get the delta.
Definition: weight_norm.hpp:118
OutputDataType const & Parameters() const
Get the parameters.
Definition: weight_norm.hpp:133
OutputDataType & Parameters()
Modify the parameters.
Definition: weight_norm.hpp:135
DeltaVisitor exposes the delta parameter of the given module.
Definition: delta_visitor.hpp:27
OutputDataType const & Gradient() const
Get the gradient.
Definition: weight_norm.hpp:123
OutputDataType & OutputParameter()
Modify the output parameter.
Definition: weight_norm.hpp:130