Softmax.h
Go to the documentation of this file.
1 //===========================================================================
2 /*!
3  *
4  *
5  * \brief Soft-max transformation.
6  *
7  *
8  *
9  * \author O. Krause, T. Glasmachers
10  * \date 2010-2011
11  *
12  *
13  * \par Copyright 1995-2017 Shark Development Team
14  *
15  * <BR><HR>
16  * This file is part of Shark.
17  * <http://shark-ml.org/>
18  *
19  * Shark is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU Lesser General Public License as published
21  * by the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * Shark is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU Lesser General Public License for more details.
28  *
29  * You should have received a copy of the GNU Lesser General Public License
30  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
33 //===========================================================================
34 
35 
36 #ifndef SHARK_MODELS_SOFTMAX_H
37 #define SHARK_MODELS_SOFTMAX_H
38 
39 #include <shark/Core/DLLSupport.h>
41 namespace shark {
42 
43 
44 ///
45 /// \brief Softmax function
46 ///
47 /// \par
48 /// Squash an n-dimensional real vector space
49 /// to the (n-1)-dimensional probability simplex:
50 /// \f[
51 /// f_i(x) = \frac{\exp(x_i)}{\sum_j \exp(x_j)}
52 /// \f]
53 /// This also corresponds to the exponential norm of the input.
54 ///
55 /// in the case of n=1, the output is
56 /// \f[
57 /// f_i(x) = \frac{\exp((2i-1)x)}{\exp(x_j)+\exp(-x_j)}
58 /// \f]
59 /// and the output dimension is 2.
60 ///
61 /// This convention ensures that all models that are trained via CrossEntropy
62 /// can be used as input to this model and the output will be the probability
63 /// of the labels.
64 
65 class Softmax : public AbstractModel<RealVector,RealVector>
66 {
67 private:
68  struct InternalState : public State{
69  RealMatrix results;
70 
71  void resize(std::size_t numPatterns,std::size_t inputs){
72  results.resize(numPatterns,inputs);
73  }
74  };
75 
76 public:
77  /// Constructor
78  SHARK_EXPORT_SYMBOL Softmax(size_t inputs);
79  /// Constructor
81 
82  /// \brief From INameable: return the class name.
83  std::string name() const
84  { return "Softmax"; }
85 
86  RealVector parameterVector()const{
87  return RealVector();
88  }
89  void setParameterVector(RealVector const& newParameters){
90  SIZE_CHECK(newParameters.size()==0);
91  }
92 
93  size_t inputSize()const{
94  return m_inputSize;
95  }
96  size_t outputSize()const{
97  return m_inputSize==1?2:m_inputSize;
98  }
99  size_t numberOfParameters()const{
100  return 0;
101  }
102 
103  boost::shared_ptr<State> createState()const{
104  return boost::shared_ptr<State>(new InternalState());
105  }
106 
107  SHARK_EXPORT_SYMBOL void eval(BatchInputType const& patterns,BatchOutputType& output)const;
108  SHARK_EXPORT_SYMBOL void eval(BatchInputType const& patterns,BatchOutputType& output, State & state)const;
110 
112  BatchInputType const& patterns, BatchOutputType const& coefficients, State const& state, RealVector& gradient
113  )const;
115  BatchInputType const& patterns, RealMatrix const& coefficients, State const& state, BatchOutputType& gradient
116  )const;
117 
118  void setStructure(std::size_t inputSize){
119  m_inputSize = inputSize;
120  }
121 
122  /// From ISerializable, reads a model from an archive
123  SHARK_EXPORT_SYMBOL void read( InArchive & archive );
124 
125  /// From ISerializable, writes a model to an archive
126  SHARK_EXPORT_SYMBOL void write( OutArchive & archive ) const;
127 
128 private:
129  std::size_t m_inputSize;
130 };
131 
132 }
133 #endif