MonomialKernel.h
Go to the documentation of this file.
1 //===========================================================================
2 /*!
3  *
4  *
5  * \brief monomial (polynomial) kernel
6  *
7  *
8  *
9  * \author T.Glasmachers, O. Krause, M. Tuma
10  * \date 2012
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 #ifndef SHARK_MODELS_KERNELS_MONOMIAL_KERNEL_H
36 #define SHARK_MODELS_KERNELS_MONOMIAL_KERNEL_H
37 
38 
40 namespace shark {
41 
42 
43 /// \brief Monomial kernel. Calculates \f$ \left\langle x_1, x_2 \right\rangle^m_exponent \f$
44 ///
45 /// \par
46 /// The degree \f$ m_exponent \f$ is a non-trainable but configurable parameter.
47 /// The default value is one - exactly the same as a LinearKernel.
48 template<class InputType=RealVector>
49 class MonomialKernel : public AbstractKernelFunction<InputType>
50 {
51 private:
53 
54  struct InternalState: public State{
55  RealMatrix base;//stores the inner product of vectors x_1,x_j which is the base the late rused pow
56  RealMatrix exponentedProd;//pow(base,m_exponent)
57 
58  void resize(std::size_t sizeX1, std::size_t sizeX2){
59  base.resize(sizeX1, sizeX2);
60  exponentedProd.resize(sizeX1, sizeX2);
61  }
62  };
63 
64 public:
72  }
73  MonomialKernel(unsigned int n):m_exponent(n){
77  }
78 
79  /// \brief From INameable: return the class name.
80  std::string name() const
81  { return "MonomialKernel"; }
82 
83  RealVector parameterVector() const{
84  return RealVector(0);
85  }
86  void setParameterVector(RealVector const& newParameters){
87  SIZE_CHECK(newParameters.size() == 0);
88  }
89  std::size_t numberOfParameters() const{
90  return 0;
91  }
92 
93  ///\brief creates the internal state of the kernel
94  boost::shared_ptr<State> createState()const{
95  return boost::shared_ptr<State>(new InternalState());
96  }
97 
98  /////////////////////////EVALUATION//////////////////////////////
99  double eval(ConstInputReference x1, ConstInputReference x2) const{
100  SIZE_CHECK(x1.size() == x2.size());
101  double prod=inner_prod(x1, x2);
102  return std::pow(prod,m_exponent);
103  }
104 
105  void eval(ConstBatchInputReference batchX1, ConstBatchInputReference batchX2, RealMatrix& result) const{
106  SIZE_CHECK(batchX1.size2() == batchX2.size2());
107  std::size_t sizeX1 = batchX1.size1();
108  std::size_t sizeX2 = batchX2.size1();
109  result.resize(sizeX1,sizeX2);
110  noalias(result) = prod(batchX1,trans(batchX2));
111  if(m_exponent != 1)
112  noalias(result) = pow(result,m_exponent);
113  }
114 
115  void eval(ConstBatchInputReference batchX1, ConstBatchInputReference batchX2, RealMatrix& result, State& state) const{
116  SIZE_CHECK(batchX1.size2() == batchX2.size2());
117  std::size_t sizeX1 = batchX1.size1();
118  std::size_t sizeX2 = batchX2.size1();
119  result.resize(sizeX1,sizeX2);
120 
121 
122  InternalState& s = state.toState<InternalState>();
123  s.resize(sizeX1,sizeX2);
124 
125  //calculate the inner product
126  noalias(s.base) = prod(batchX1,trans(batchX2));
127  //now do exponentiation
128  if(m_exponent != 1)
129  noalias(result) = pow(s.base,m_exponent);
130  else
131  noalias(result) = s.base;
132  //store also in state
133  noalias(s.exponentedProd) = result;
134 
135  }
136 
137  ////////////////////////DERIVATIVES////////////////////////////
138 
140  ConstBatchInputReference batchX1,
141  ConstBatchInputReference batchX2,
142  RealMatrix const& coefficients,
143  State const& state,
144  RealVector& gradient
145  ) const{
146  SIZE_CHECK(batchX1.size2() == batchX2.size2());
147  gradient.resize(0);
148  }
149 
151  ConstBatchInputReference batchX1,
152  ConstBatchInputReference batchX2,
153  RealMatrix const& coefficientsX2,
154  State const& state,
155  BatchInputType& gradient
156  ) const{
157 
158  std::size_t sizeX1 = batchX1.size1();
159  std::size_t sizeX2 = batchX2.size1();
160  gradient.resize(sizeX1,batchX1.size2());
161  InternalState const& s = state.toState<InternalState>();
162 
163  //internal checks
164  SIZE_CHECK(batchX1.size2() == batchX2.size2());
165  SIZE_CHECK(s.base.size1() == sizeX1);
166  SIZE_CHECK(s.base.size2() == sizeX2);
167  SIZE_CHECK(s.exponentedProd.size1() == sizeX1);
168  SIZE_CHECK(s.exponentedProd.size2() == sizeX2);
169 
170  //first calculate weights(i,j) = coeff(i)*exp(i,j)/prod(i,j)
171  //we have to take the usual division by 0 into account
172  RealMatrix weights = coefficientsX2 * safe_div(s.exponentedProd,s.base,0.0);
173  //The derivative of input i of batch x1 is
174  //g = sum_j m_exponent*weights(i,j)*x2_j
175  //we now sum over j which is a matrix-matrix product
176  noalias(gradient) = m_exponent * prod(weights,batchX2);
177  }
178 
179  void read(InArchive& ar){
180  ar >> m_exponent;
181  }
182 
183  void write(OutArchive& ar) const{
184  ar << m_exponent;
185  }
186 
187 protected:
188  ///the exponent of the monomials
190 };
191 
194 
195 
196 }
197 #endif