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.hpp"
17 
18 namespace mlpack {
19 namespace ann {
20 
48 template <
49  typename InputType = arma::mat,
50  typename OutputType = arma::mat
51 >
52 class WeightNormType : public Layer<InputType, OutputType>
53 {
54  public:
59 
66 
69 
71  WeightNormType(const WeightNormType& other);
78 
80  WeightNormType* Clone() const { return new WeightNormType(*this); }
81 
85  void SetWeights(typename OutputType::elem_type* weightsPtr);
86 
96  void Forward(const InputType& input, OutputType& output);
97 
106  void Backward(const InputType& input,
107  const OutputType& gy,
108  OutputType& g);
109 
118  void Gradient(const InputType& input,
119  const OutputType& error,
120  OutputType& gradient);
121 
123  OutputType const& Parameters() const { return weights; }
125  OutputType& Parameters() { return weights; }
126 
128  Layer<InputType, OutputType>* const& WrappedLayer() { return wrappedLayer; }
129 
130  const size_t WeightSize() const { return wrappedLayer->WeightSize(); }
131 
132  const std::vector<size_t> OutputDimensions() const
133  {
134  wrappedLayer->InputDimensions() = inputDimensions;
135  return wrappedLayer->OutputDimensions();
136  }
137 
141  template<typename Archive>
142  void serialize(Archive& ar, const uint32_t /* version */);
143 
144  private:
146  size_t biasWeightSize;
147 
149  Layer<InputType, OutputType>* wrappedLayer;
150 
152  size_t layerWeightSize;
153 
155  void ResetGradients(OutputType& gradient);
156 
158  OutputType scalarParameter;
159 
161  OutputType vectorParameter;
162 
164  OutputType weights;
165 
167  OutputType layerGradients;
168 
170  OutputType layerWeights;
171 }; // class WeightNormType.
172 
173 // Standard WeightNorm layer.
175 
176 } // namespace ann
177 } // namespace mlpack
178 
179 // Include the implementation.
180 #include "weight_norm_impl.hpp"
181 
182 #endif
WeightNormType()
Create an empty WeightNorm layer.
std::vector< size_t > inputDimensions
Logical input dimensions of each point.
Definition: layer.hpp:302
~WeightNormType()
Destructor to release allocated memory.
WeightNormType & operator=(const WeightNormType &other)
Copy the given layer.
Linear algebra utility functions, generally performed on matrices or vectors.
void Backward(const InputType &input, const OutputType &gy, OutputType &g)
Backward pass through the layer.
const size_t WeightSize() const
Get the total number of trainable weights in the layer.
The core includes that mlpack expects; standard C++ includes and Armadillo.
const std::vector< size_t > & OutputDimensions()
Get the output dimensions.
Definition: layer.hpp:231
void SetWeights(typename OutputType::elem_type *weightsPtr)
Reset the layer parameters.
void Gradient(const InputType &input, const OutputType &error, OutputType &gradient)
Calculate the gradient using the output delta, input activations and the weights of the wrapped layer...
void Forward(const InputType &input, OutputType &output)
Forward pass of the WeightNorm layer.
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
const std::vector< size_t > & InputDimensions() const
Get the input dimensions.
Definition: layer.hpp:222
const std::vector< size_t > OutputDimensions() const
OutputType const & Parameters() const
Get the parameters.
Declaration of the WeightNorm layer class.
Definition: weight_norm.hpp:52
OutputType & Parameters()
Modify the parameters.
WeightNormType< arma::mat, arma::mat > WeightNorm
A layer is an abstract class implementing common neural networks operations, such as convolution...
Definition: layer.hpp:52
Layer< InputType, OutputType > *const & WrappedLayer()
Get the wrapped layer.
WeightNormType * Clone() const
Clone the WeightNormType object. This handles polymorphism correctly.
Definition: weight_norm.hpp:80
virtual size_t WeightSize() const
Get the total number of trainable weights in the layer.
Definition: layer.hpp:195