LinearKernel.h
Go to the documentation of this file.
1 //===========================================================================
2 /*!
3  *
4  *
5  * \brief linear kernel (standard inner product)
6  *
7  *
8  *
9  * \author T.Glasmachers, O. Krause, M. Tuma
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 #ifndef SHARK_MODELS_KERNELS_LINEAR_KERNEL_H
36 #define SHARK_MODELS_KERNELS_LINEAR_KERNEL_H
37 
38 
40 
41 namespace shark{
42 
43 
44 /// \brief Linear Kernel, parameter free
45 template<class InputType=RealVector>
46 class LinearKernel : public AbstractKernelFunction<InputType>
47 {
48 private:
50 public:
54 
59  }
60 
61  /// \brief From INameable: return the class name.
62  std::string name() const
63  { return "LinearKernel"; }
64 
65  RealVector parameterVector() const{
66  return RealVector();
67  }
68  void setParameterVector(RealVector const& newParameters){
69  SIZE_CHECK(newParameters.size() == 0);
70  }
71 
72  boost::shared_ptr<State> createState()const{
73  return boost::shared_ptr<State>(new EmptyState());
74  }
75 
76  double eval(ConstInputReference x1, ConstInputReference x2) const{
77  SIZE_CHECK(x1.size() == x2.size());
78  return inner_prod(x1, x2);
79  }
80 
81  void eval(ConstBatchInputReference x1, ConstBatchInputReference x2, RealMatrix& result, State& state) const{
82  eval(x1,x2,result);
83  }
84 
85  void eval(ConstBatchInputReference x1, ConstBatchInputReference x2, RealMatrix& result) const{
86  SIZE_CHECK(x1.size2() == x2.size2());
87  result.resize(x1.size1(),x2.size1());
88  noalias(result) = prod(x1,trans(x2));
89  }
90 
92  ConstBatchInputReference batchX1,
93  ConstBatchInputReference batchX2,
94  RealMatrix const& coefficients,
95  State const& state,
96  RealVector& gradient
97  ) const{
98  SIZE_CHECK(batchX1.size2() == batchX2.size2());
99  gradient.resize(0);
100  }
102  ConstBatchInputReference batchX1,
103  ConstBatchInputReference batchX2,
104  RealMatrix const& coefficientsX2,
105  State const& state,
106  BatchInputType& gradient
107  ) const{
108  SIZE_CHECK(batchX1.size2() == batchX2.size2());
109  //~ SIZE_CHECK(cofficientsX2.size1() == batchX1.size1());
110  //~ SIZE_CHECK(cofficientsX2.size2() == batchX2.size1());
111  gradient.resize(batchX1.size1(),batchX1.size2());
112 
113  noalias(gradient) = prod(coefficientsX2,batchX2);
114  }
115 
116  virtual double featureDistanceSqr(ConstInputReference x1, ConstInputReference x2) const{
117  return distanceSqr(x1,x2);
118  }
119 
120  virtual RealMatrix featureDistanceSqr(ConstBatchInputReference x1, ConstBatchInputReference x2) const{
121  return distanceSqr(x1,x2);
122  }
123 
124  /// \brief The kernel does not serialize anything
125  void read(InArchive& ar){}
126 
127  /// \brief The kernel does not serialize anything
128  void write(OutArchive& ar) const{}
129 
130 };
131 
134 
135 
136 }
137 #endif