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 
55 template <
56  typename InputDataType = arma::mat,
57  typename OutputDataType = arma::mat,
58  typename... CustomLayers
59 >
60 class WeightNorm
61 {
62  public:
68  WeightNorm(LayerTypes<CustomLayers...> layer = LayerTypes<CustomLayers...>());
69 
71  ~WeightNorm();
72 
76  void Reset();
77 
87  template<typename eT>
88  void Forward(arma::Mat<eT>&& input, arma::Mat<eT>&& output);
89 
98  template<typename eT>
99  void Backward(const arma::Mat<eT>&& input,
100  arma::Mat<eT>&& gy,
101  arma::Mat<eT>&& g);
102 
111  template<typename eT>
112  void Gradient(arma::Mat<eT>&& input,
113  arma::Mat<eT>&& error,
114  arma::Mat<eT>&& gradient);
115 
117  OutputDataType const& Delta() const { return delta; }
119  OutputDataType& Delta() { return delta; }
120 
122  OutputDataType const& Gradient() const { return gradient; }
124  OutputDataType& Gradient() { return gradient; }
125 
127  OutputDataType const& OutputParameter() const { return outputParameter; }
129  OutputDataType& OutputParameter() { return outputParameter; }
130 
132  OutputDataType const& Parameters() const { return weights; }
134  OutputDataType& Parameters() { return weights; }
135 
137  LayerTypes<CustomLayers...> const& Layer() { return wrappedLayer; }
138 
142  template<typename Archive>
143  void serialize(Archive& ar, const unsigned int /* version */);
144 
145  private:
147  size_t biasWeightSize;
148 
150  DeleteVisitor deleteVisitor;
151 
153  OutputDataType delta;
154 
156  DeltaVisitor deltaVisitor;
157 
159  OutputDataType gradient;
160 
162  LayerTypes<CustomLayers...> wrappedLayer;
163 
165  size_t layerWeightSize;
166 
168  OutputDataType outputParameter;
169 
171  OutputParameterVisitor outputParameterVisitor;
172 
174  void ResetGradients(arma::mat& gradient);
175 
177  ResetVisitor resetVisitor;
178 
180  OutputDataType scalarParameter;
181 
183  OutputDataType vectorParameter;
184 
186  OutputDataType weights;
187 
189  WeightSizeVisitor weightSizeVisitor;
190 
192  OutputDataType layerGradients;
193 
195  OutputDataType layerWeights;
196 }; // class WeightNorm
197 
198 } // namespace ann
199 } // namespace mlpack
200 
201 // Include the implementation.
202 #include "weight_norm_impl.hpp"
203 
204 #endif
DeleteVisitor executes the destructor of the instantiated object.
LayerTypes< CustomLayers... > const & Layer()
Get the wrapped layer.
OutputDataType & Gradient()
Modify the gradient.
strip_type.hpp
Definition: add_to_po.hpp:21
void Backward(const arma::Mat< eT > &&input, arma::Mat< eT > &&gy, arma::Mat< eT > &&g)
Backward pass through the layer.
void Reset()
Reset the layer parameters.
The core includes that mlpack expects; standard C++ includes and Armadillo.
OutputDataType & Parameters()
Modify the parameters.
WeightSizeVisitor returns the number of weights of the given module.
~WeightNorm()
Destructor to release allocated memory.
OutputDataType const & Gradient() const
Get the gradient.
ResetVisitor executes the Reset() function.
OutputParameterVisitor exposes the output parameter of the given module.
void Forward(arma::Mat< eT > &&input, arma::Mat< eT > &&output)
Forward pass of the WeightNorm layer.
OutputDataType & Delta()
Modify the delta.
OutputDataType const & OutputParameter() const
Get the output parameter.
boost::variant< Add< arma::mat, arma::mat > *, AddMerge< arma::mat, arma::mat > *, AtrousConvolution< NaiveConvolution< ValidConvolution >, NaiveConvolution< FullConvolution >, NaiveConvolution< ValidConvolution >, 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 > *, BaseLayer< SoftplusFunction, arma::mat, arma::mat > *, BatchNorm< arma::mat, arma::mat > *, BilinearInterpolation< arma::mat, arma::mat > *, Concat< arma::mat, arma::mat > *, Concatenate< 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 > *, TransposedConvolution< NaiveConvolution< ValidConvolution >, NaiveConvolution< ValidConvolution >, NaiveConvolution< ValidConvolution >, arma::mat, arma::mat > *, DropConnect< arma::mat, arma::mat > *, Dropout< arma::mat, arma::mat > *, AlphaDropout< arma::mat, arma::mat > *, ELU< arma::mat, arma::mat > *, FlexibleReLU< arma::mat, arma::mat > *, Glimpse< arma::mat, arma::mat > *, HardTanH< arma::mat, arma::mat > *, Highway< arma::mat, arma::mat > *, Join< arma::mat, arma::mat > *, LayerNorm< arma::mat, arma::mat > *, LeakyReLU< arma::mat, arma::mat > *, CReLU< arma::mat, arma::mat > *, Linear< arma::mat, arma::mat, NoRegularizer > *, LinearNoBias< arma::mat, arma::mat, NoRegularizer > *, LogSoftMax< arma::mat, arma::mat > *, Lookup< arma::mat, arma::mat > *, LSTM< arma::mat, arma::mat > *, GRU< arma::mat, arma::mat > *, FastLSTM< arma::mat, arma::mat > *, MaxPooling< arma::mat, arma::mat > *, MeanPooling< arma::mat, arma::mat > *, MiniBatchDiscrimination< arma::mat, arma::mat > *, MultiplyConstant< arma::mat, arma::mat > *, MultiplyMerge< arma::mat, arma::mat > *, NegativeLogLikelihood< arma::mat, arma::mat > *, Padding< arma::mat, arma::mat > *, PReLU< arma::mat, arma::mat > *, WeightNorm< arma::mat, arma::mat > *, MoreTypes, CustomLayers *... > LayerTypes
OutputDataType & OutputParameter()
Modify the output parameter.
DeltaVisitor exposes the delta parameter of the given module.
OutputDataType const & Parameters() const
Get the parameters.
OutputDataType const & Delta() const
Get the delta.
WeightNorm(LayerTypes< CustomLayers... > layer=LayerTypes< CustomLayers... >())
Create the WeightNorm layer object.
void serialize(Archive &ar, const unsigned int)
Serialize the layer.