SparseAutoencoderError.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief Specific error function for Feed-Forward-Networks which enforces it to have sparse hidden neuron activation
5  *
6  *
7  *
8  * \author O.Krause
9  * \date 2012
10  *
11  *
12  * \par Copyright 1995-2017 Shark Development Team
13  *
14  * <BR><HR>
15  * This file is part of Shark.
16  * <http://shark-ml.org/>
17  *
18  * Shark is free software: you can redistribute it and/or modify
19  * it under the terms of the GNU Lesser General Public License as published
20  * by the Free Software Foundation, either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * Shark is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU Lesser General Public License for more details.
27  *
28  * You should have received a copy of the GNU Lesser General Public License
29  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
30  *
31  */
32 #ifndef SHARK_OBJECTIVEFUNCTIONS_SparseAutoencoderError_H
33 #define SHARK_OBJECTIVEFUNCTIONS_SparseAutoencoderError_H
34 
35 
40 #include "Impl/FunctionWrapperBase.h"
41 
42 #include <boost/scoped_ptr.hpp>
43 namespace shark{
44 
45 ///
46 /// \brief Error Function for Autoencoders and TiedAutoencoders which should be trained with sparse activation of the hidden neurons
47 ///
48 /// This error function optimizes a Network with respect to some loss function similar to the standard
49 /// ErrorFunction. Additionally another penalty term is added which enforces a sparse activation pattern of
50 /// the hidden neurons.
51 /// Given a target mean activation \f$ \rho \f$ the mean activation of hidden neuron j over the whole dataset
52 /// \f$ \rho_j\f$ is interpreted as the activation propability and penalized using the KL-divergence:
53 /// \f$ KL(\rho||\rho_j) = \rho log(\frac{\rho}{\rho_j})+(1-\rho) log(\frac{1-\rho}{1-\rho_j}) \f$
54 ///
55 /// This Error Function has two meta-parameters: rho governs the desired mean activation and
56 /// beta the strength of regularization. Another regularizer can be added using setRegularizer as in typical ErrorFunctions.
58 {
59 public:
61  template<class HiddenNeuron, class OutputNeuron>
63  DatasetType const& dataset,
66  double rho = 0.5, double beta = 0.1
67  );
68  template<class HiddenNeuron, class OutputNeuron>
70  DatasetType const& dataset,
73  double rho = 0.5, double beta = 0.1
74  );
75 
77  mp_wrapper.reset(op.mp_wrapper->clone());
78  return *this;
79  }
80 
81  /// \brief From INameable: return the class name.
82  std::string name() const
83  { return "SparseAutoencoderError"; }
84 
85  std::size_t numberOfVariables()const{
86  return mp_wrapper->numberOfVariables();
87  }
88 
90  return mp_wrapper->proposeStartingPoint();
91  }
92 
93  void setRegularizer(double factor, SingleObjectiveFunction* regularizer){
94  m_regularizer = regularizer;
95  m_regularizationStrength = factor;
96  }
97 
98  double eval(RealVector const& input) const{
100  double value = mp_wrapper -> eval(input);
101  if(m_regularizer)
102  value += m_regularizationStrength * m_regularizer->eval(input);
103  return value;
104  }
105  ResultType evalDerivative( SearchPointType const& input, FirstOrderDerivative & derivative ) const{
107  double value = mp_wrapper -> evalDerivative(input,derivative);
108  if(m_regularizer){
109  FirstOrderDerivative regularizerDerivative;
110  value += m_regularizationStrength * m_regularizer->evalDerivative(input,regularizerDerivative);
111  noalias(derivative) += m_regularizationStrength*regularizerDerivative;
112  }
113  return value;
114  }
115 
117  swap(op1.mp_wrapper,op2.mp_wrapper);
118  }
119 
120 private:
121  boost::scoped_ptr<detail::FunctionWrapperBase > mp_wrapper;
122 
123  SingleObjectiveFunction* m_regularizer;
124  double m_regularizationStrength;
125 };
126 
127 }
128 #include "Impl/SparseAutoencoderError.inl"
129 #endif