Shark machine learning library
Installation
Tutorials
Benchmarks
Documentation
Quick references
Class list
Global functions
include
shark
Models
Kernels
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
39
#include <
shark/Models/Kernels/AbstractKernelFunction.h
>
40
41
namespace
shark
{
42
43
44
/// \brief Linear Kernel, parameter free
45
/// \ingroup kernels
46
template
<
class
InputType=RealVector>
47
class
LinearKernel
:
public
AbstractKernelFunction
<InputType>
48
{
49
private
:
50
typedef
AbstractKernelFunction<InputType>
base_type
;
51
public
:
52
typedef
typename
base_type::BatchInputType
BatchInputType
;
53
typedef
typename
base_type::ConstInputReference
ConstInputReference
;
54
typedef
typename
base_type::ConstBatchInputReference
ConstBatchInputReference
;
55
56
LinearKernel
(){
57
this->
m_features
|=
base_type::HAS_FIRST_PARAMETER_DERIVATIVE
;
58
this->
m_features
|=
base_type::HAS_FIRST_INPUT_DERIVATIVE
;
59
this->
m_features
|=
base_type::SUPPORTS_VARIABLE_INPUT_SIZE
;
60
}
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
77
double
eval
(
ConstInputReference
x1,
ConstInputReference
x2)
const
{
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
92
void
weightedParameterDerivative
(
93
ConstBatchInputReference
batchX1,
94
ConstBatchInputReference
batchX2,
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
}
102
void
weightedInputDerivative
(
103
ConstBatchInputReference
batchX1,
104
ConstBatchInputReference
batchX2,
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
117
virtual
double
featureDistanceSqr
(
ConstInputReference
x1,
ConstInputReference
x2)
const
{
118
return
distanceSqr(x1,x2);
119
}
120
121
virtual
RealMatrix
featureDistanceSqr
(
ConstBatchInputReference
x1,
ConstBatchInputReference
x2)
const
{
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
133
typedef
LinearKernel<>
DenseLinearKernel
;
134
typedef
LinearKernel<CompressedRealVector>
CompressedLinearKernel
;
135
136
137
}
138
#endif