SigmoidModel.h
Go to the documentation of this file.
1 /*!
2  * \brief Implements a simple sigmoidal model for sigmoidal fitting in a 2-d problem
3  *
4  * \author
5  * \date
6  *
7  *
8  * \par Copyright 1995-2017 Shark Development Team
9  *
10  * <BR><HR>
11  * This file is part of Shark.
12  * <http://shark-ml.org/>
13  *
14  * Shark is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU Lesser General Public License as published
16  * by the Free Software Foundation, either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * Shark is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
26  *
27  */
28 #ifndef SHARK_MODEL_ML_SIGMOIDMODEL_H
29 #define SHARK_MODEL_ML_SIGMOIDMODEL_H
30 
31 #include <shark/Core/DLLSupport.h>
33 namespace shark {
34 
35 
36 //! \brief Standard sigmoid function.
37 //!
38 //! \par
39 //! This model maps a real-valued input to the unit interval by the sigmoid function
40 //! \f$ f(x) = \frac{1}{1 + \exp(-Ax+B))} \f$,
41 //! where the real-valued model parameter A controls the slope, and the real-valued
42 //! offset model parameter B controls the position of the symmetry point.
43 //! This is a special case of a feed forward neural network consisting of
44 //! a single sigmoid layer.
45 //! Note that the parameter A is expected to be non-negative
46 //! (and hence does not incorporate the minus sign in the sigmoid's equation).
47 //! Also, the offset parameter can be disabled using the setOffsetActivity()
48 //! member function.
49 //!
50 //! \sa TanhSigmoidModel SimpleSigmoidModel
51 class SigmoidModel : public AbstractModel<RealVector,RealVector>
52 {
53 private:
54  struct InternalState:public State{
55  RealVector result;
56 
57  void resize(std::size_t patterns){
58  result.resize(patterns);
59  }
60  };
61 public:
62 
63  //! default ctor
64  //! \param transform_for_unconstrained when a new paramVector is set, should the exponent of the first parameter be used as the sigmoid's slope?
65  SHARK_EXPORT_SYMBOL SigmoidModel( bool transform_for_unconstrained = true );
66 
67  /// \brief From INameable: return the class name.
68  std::string name() const
69  { return "SigmoidModel"; }
70 
71  SHARK_EXPORT_SYMBOL RealVector parameterVector() const;
72 
73  //! note that the parameters are not expected to incorporate the minus sign in the sigmoid's equation
74  //! \param newParameters the new parameter vector A and offset B concatenated
75  SHARK_EXPORT_SYMBOL void setParameterVector(RealVector const& newParameters);
76 
77  std::size_t numberOfParameters() const {
78  return 2; //we always return 2, even if the offset is hard-clamped to zero.
79  }
80  // \brief whether to use the offset, or clamp it to zero. offset is active by default.
81  SHARK_EXPORT_SYMBOL void setOffsetActivity( bool enable_offset );
82 
83  bool hasOffset()const{
84  return m_useOffset;
85  }
86 
87  bool slopeIsExpEncoded()const{
89  }
90 
91  /*!
92  * \brief activation function \f$g_{output}(x)\f$
93  */
94  SHARK_EXPORT_SYMBOL virtual double sigmoid(double x)const;
95  /*!
96  * \brief Computes the derivative of the activation function
97  * \f$g_{output}(x)\f$ for the output given the
98  * last response of the model gx=g(x)
99  */
100  virtual double sigmoidDerivative(double gx)const;
101 
102  boost::shared_ptr<State> createState()const{
103  return boost::shared_ptr<State>(new InternalState());
104  }
105 
106  SHARK_EXPORT_SYMBOL void eval(BatchInputType const&pattern, BatchOutputType& output, State& state)const;
107  SHARK_EXPORT_SYMBOL void eval(BatchInputType const&pattern, BatchOutputType& output)const;
109 
111  BatchInputType const& pattern, BatchOutputType const& coefficients, State const& state, RealVector& gradient
112  )const;
113 
115  BatchInputType const& pattern, BatchOutputType const& coefficients, State const& state, BatchInputType& derivative
116  )const;
117 
118  std::size_t inputSize()const{
119  return 1;
120  }
121  std::size_t outputSize()const{
122  return 1;
123  }
124 
125  //! set the minimum log value that should be returned as log-encoded slope if the true slope is actually zero. default in ctor sets -230.
126  //! param logvalue the new minimum log value
127  void setMinLogValue( double logvalue = -230.0 );
128 
129  /// From ISerializable, reads a model from an archive
130  void read( InArchive & archive );
131 
132  /// From ISerializable, writes a model to an archive
133  void write( OutArchive & archive ) const;
134 
135 protected:
136  RealVector m_parameters; ///< the parameter vector
137  bool m_useOffset; ///< whether or not to allow non-zero offset values
138 
139  bool m_transformForUnconstrained; ///< flag for encoding variant
140  double m_minLogValue; ///< what value should be returned as log-encoded slope if the true slope is actually zero
141 
142 };
143 
144 
145 //! \brief Simple sigmoid function
146 //!
147 //! \par
148 //! This model maps the reals to the unit interval by the sigmoid function
149 //! \f$ f(x) = \frac{1}{2} \frac{st}{1+|<A,x>+b|} + \frac{1}{2} \f$.
151 {
152 public:
153  SHARK_EXPORT_SYMBOL SimpleSigmoidModel( bool transform_for_unconstrained = true );
154  SHARK_EXPORT_SYMBOL double sigmoid(double a)const;
155  SHARK_EXPORT_SYMBOL double sigmoidDerivative(double ga)const;
156 
157  /// \brief From INameable: return the class name.
158  std::string name() const
159  { return "SimpleSigmoidModel"; }
160 };
161 
162 //! \brief scaled Tanh sigmoid function
163 //!
164 //! \par
165 //! This model maps the reals to the unit interval by the sigmoid function
166 //! \f$ f(x) = \frac{1}{2} \tanh(<A,x>+b) + \frac{1}{2} \f$.
168 {
169 public:
170  SHARK_EXPORT_SYMBOL TanhSigmoidModel( bool transform_for_unconstrained = true );
171  SHARK_EXPORT_SYMBOL double sigmoid(double a)const;
172  SHARK_EXPORT_SYMBOL double sigmoidDerivative(double ga)const;
173 
174  /// \brief From INameable: return the class name.
175  std::string name() const
176  { return "TanhSigmoidModel"; }
177 };
178 }
179 #endif
180