Shark machine learning library
Installation
Tutorials
Benchmarks
Documentation
Quick references
Class list
Global functions
include
shark
Models
Kernels
AbstractMetric.h
Go to the documentation of this file.
1
//===========================================================================
2
/*!
3
*
4
*
5
* \brief abstract super class of all metrics
6
*
7
*
8
*
9
* \author O. Krause
10
* \date 2015
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_ABSTRACTMETRIC_H
36
#define SHARK_MODELS_KERNELS_ABSTRACTMETRIC_H
37
38
#include <
shark/Data/BatchInterface.h
>
39
#include <
shark/Core/IParameterizable.h
>
40
#include <
shark/Core/ISerializable.h
>
41
#include <
shark/Core/INameable.h
>
42
#include <
shark/Core/Traits/ProxyReferenceTraits.h
>
43
namespace
shark
{
44
45
/// \brief Base-class for metrics.
46
///
47
/// A metric is a distance measure between objects. All kernels in shark define metrics.
48
/// \ingroup kernels
49
template
<
class
InputTypeT>
50
class
AbstractMetric
:
public
INameable
,
public
IParameterizable
<>,
public
ISerializable
{
51
public
:
52
/// \brief Input type of the Kernel.
53
typedef
InputTypeT
InputType
;
54
/// \brief batch input type of the kernel
55
typedef
typename
Batch<InputTypeT>::type
BatchInputType
;
56
/// \brief Const references to InputType
57
typedef
typename
ConstProxyReference<InputType const>::type
ConstInputReference
;
58
/// \brief Const references to BatchInputType
59
typedef
typename
ConstProxyReference<BatchInputType const>::type
ConstBatchInputReference
;
60
61
AbstractMetric
() { }
62
virtual
~AbstractMetric
() { }
63
64
/// \brief From ISerializable, reads a metric from an archive.
65
virtual
void
read
(
InArchive
& archive ){
66
RealVector p;
67
archive & p;
68
setParameterVector
(p);
69
}
70
71
/// \brief From ISerializable, writes a metric to an archive.
72
///
73
/// The default implementation just saves the parameters.
74
virtual
void
write
(
OutArchive
& archive )
const
{
75
RealVector p =
parameterVector
();
76
archive & p;
77
}
78
79
/// Computes the squared distance in the kernel induced feature space.
80
virtual
double
featureDistanceSqr
(
ConstInputReference
x1,
ConstInputReference
x2)
const
=0;
81
82
virtual
RealMatrix
featureDistanceSqr
(
83
ConstBatchInputReference
batchX1,
84
ConstBatchInputReference
batchX2
85
)
const
= 0;
86
87
88
/// \brief Computes the distance in the kernel induced feature space.
89
double
featureDistance
(
ConstInputReference
x1,
ConstInputReference
x2)
const
{
90
return
std::sqrt(
featureDistanceSqr
(x1, x2));
91
}
92
};
93
}
94
#endif