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 * <https://shark-ml.github.io/Shark/>
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
41namespace shark{
42
43
44/// \brief Linear Kernel, parameter free
45/// \ingroup kernels
46template<class InputType=RealVector>
47class LinearKernel : public AbstractKernelFunction<InputType>
48{
49private:
51public:
55
61
62 /// \brief From INameable: return the class name.
63 std::string name() const
64 { return "LinearKernel"; }
65
66 RealVector parameterVector() const{
67 return RealVector();
68 }
69 void setParameterVector(RealVector const& newParameters){
70 SIZE_CHECK(newParameters.size() == 0);
71 }
72
73 boost::shared_ptr<State> createState()const{
74 return boost::shared_ptr<State>(new EmptyState());
75 }
76
78 SIZE_CHECK(x1.size() == x2.size());
79 return inner_prod(x1, x2);
80 }
81
82 void eval(ConstBatchInputReference x1, ConstBatchInputReference x2, RealMatrix& result, State& state) const{
83 eval(x1,x2,result);
84 }
85
86 void eval(ConstBatchInputReference x1, ConstBatchInputReference x2, RealMatrix& result) const{
87 SIZE_CHECK(x1.size2() == x2.size2());
88 result.resize(x1.size1(),x2.size1());
89 noalias(result) = prod(x1,trans(x2));
90 }
91
95 RealMatrix const& coefficients,
96 State const& state,
97 RealVector& gradient
98 ) const{
99 SIZE_CHECK(batchX1.size2() == batchX2.size2());
100 gradient.resize(0);
101 }
105 RealMatrix const& coefficientsX2,
106 State const& state,
107 BatchInputType& gradient
108 ) const{
109 SIZE_CHECK(batchX1.size2() == batchX2.size2());
110 //~ SIZE_CHECK(cofficientsX2.size1() == batchX1.size1());
111 //~ SIZE_CHECK(cofficientsX2.size2() == batchX2.size1());
112 gradient.resize(batchX1.size1(),batchX1.size2());
113
114 noalias(gradient) = prod(coefficientsX2,batchX2);
115 }
116
118 return distanceSqr(x1,x2);
119 }
120
122 return distanceSqr(x1,x2);
123 }
124
125 /// \brief The kernel does not serialize anything
126 void read(InArchive& ar){}
127
128 /// \brief The kernel does not serialize anything
129 void write(OutArchive& ar) const{}
130
131};
132
135
136
137}
138#endif