mlpack
lregularizer_impl.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_ANN_LREGULARIZER_IMPL_HPP
13 #define MLPACK_METHODS_ANN_LREGULARIZER_IMPL_HPP
14 
15 // In case it hasn't been included.
16 #include "lregularizer.hpp"
17 
18 namespace mlpack {
19 namespace ann {
20 
21 template<int Power>
23  factor(factor)
24 {
25  // Nothing to do here
26 }
27 
28 // Unspecialized implementation. This should almost never be used...
29 template<int Power>
30 template<typename MatType>
31 void LRegularizer<Power>::Evaluate(const MatType& weight, MatType& gradient)
32 {
33  gradient += arma::vectorise(arma::pow(weight, Power - 1) * Power * factor);
34 }
35 
36 // L1-Regularizer specializations.
37 template<>
38 template<typename MatType>
39 void LRegularizer<1>::Evaluate(const MatType& weight, MatType& gradient)
40 {
41  gradient += arma::vectorise(factor * weight / arma::abs(weight));
42 }
43 
44 // L2-Regularizer specializations.
45 template<>
46 template<typename MatType>
47 void LRegularizer<2>::Evaluate(const MatType& weight, MatType& gradient)
48 {
49  gradient += arma::vectorise(2 * factor * weight);
50 }
51 
52 template<int Power>
53 template<typename Archive>
55  Archive& ar, const uint32_t /* version */)
56 {
57  ar(CEREAL_NVP(factor));
58 }
59 
60 } // namespace ann
61 } // namespace mlpack
62 
63 #endif
double factor
The constant for the regularization.
Definition: lregularizer.hpp:55
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
LRegularizer(double factor=1.0)
Create the regularizer object.
Definition: lregularizer_impl.hpp:22
static const int Power
The power of the regularizer.
Definition: lregularizer.hpp:52
void Evaluate(const MatType &weight, MatType &gradient)
Calculate the gradient for regularization.
Definition: lregularizer_impl.hpp:31
void serialize(Archive &ar, const uint32_t)
Serialize the regularizer (nothing to do).
Definition: lregularizer_impl.hpp:54