KernelMatrix.h
Go to the documentation of this file.
1//===========================================================================
2/*!
3 *
4 *
5 * \brief Kernel Gram matrix
6 *
7 *
8 * \par
9 *
10 *
11 *
12 * \author T. Glasmachers
13 * \date 2007-2012
14 *
15 *
16 * \par Copyright 1995-2017 Shark Development Team
17 *
18 * <BR><HR>
19 * This file is part of Shark.
20 * <https://shark-ml.github.io/Shark/>
21 *
22 * Shark is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU Lesser General Public License as published
24 * by the Free Software Foundation, either version 3 of the License, or
25 * (at your option) any later version.
26 *
27 * Shark is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU Lesser General Public License for more details.
31 *
32 * You should have received a copy of the GNU Lesser General Public License
33 * along with Shark. If not, see <http://www.gnu.org/licenses/>.
34 *
35 */
36//===========================================================================
37
38
39#ifndef SHARK_LINALG_KERNELMATRIX_H
40#define SHARK_LINALG_KERNELMATRIX_H
41
42
43#include <shark/Data/Dataset.h>
44#include <shark/LinAlg/Base.h>
46
47#include <vector>
48#include <cmath>
49
50
51namespace shark {
52
53///
54/// \brief Kernel Gram matrix
55///
56/// \par
57/// The KernelMatrix is the most prominent type of matrix
58/// for quadratic programming. It provides the Gram matrix
59/// of a fixed data set with respect to an inner product
60/// implicitly defined by a kernel function.
61///
62/// \par
63/// NOTE: The KernelMatrix class stores pointers to the
64/// data, instead of maintaining a copy of the data. Thus,
65/// it implicitly assumes that the dataset is not altered
66/// during the lifetime of the KernelMatrix object. This
67/// condition is ensured as long as the class is used via
68/// the various SVM-trainers.
69///
70template <class InputType, class CacheType>
72{
73public:
74 typedef CacheType QpFloatType;
75
76 /// Constructor
77 /// \param kernelfunction kernel function defining the Gram matrix
78 /// \param data data to evaluate the kernel function
80 Data<InputType> const& data)
81 : kernel(kernelfunction)
82 , m_data(data)
83 , m_accessCounter( 0 )
84 {
85 std::size_t elements = m_data.numberOfElements();
86 x.resize(elements);
88 for(std::size_t i = 0; i != elements; ++i,++iter){
89 x[i]=iter.getInnerIterator();
90 }
91 }
92
93 /// return a single matrix entry
94 QpFloatType operator () (std::size_t i, std::size_t j) const
95 { return entry(i, j); }
96
97 /// return a single matrix entry
98 QpFloatType entry(std::size_t i, std::size_t j) const
99 {
101 return (QpFloatType)kernel.eval(*x[i], *x[j]);
102 }
103
104 /// \brief Computes the i-th row of the kernel matrix.
105 ///
106 ///The entries start,...,end of the i-th row are computed and stored in storage.
107 ///There must be enough room for this operation preallocated.
108 void row(std::size_t i, std::size_t start,std::size_t end, QpFloatType* storage) const{
109 m_accessCounter += end-start;
110
112 SHARK_PARALLEL_FOR(int j = (int)start; j < (int) end; j++)
113 {
114 storage[j-start] = QpFloatType(kernel.eval(xi, *x[j]));
115 }
116 }
117
118 /// \brief Computes the kernel-matrix
119 template<class M>
120 void matrix(
121 blas::matrix_expression<M, blas::cpu_tag> & storage
122 ) const{
124 }
125
126 /// swap two variables
127 void flipColumnsAndRows(std::size_t i, std::size_t j){
128 using std::swap;
129 swap(x[i],x[j]);
130 }
131
132 /// return the size of the quadratic matrix
133 std::size_t size() const
134 { return x.size(); }
135
136 /// query the kernel access counter
137 unsigned long long getAccessCount() const
138 { return m_accessCounter; }
139
140 /// reset the kernel access counter
143
144protected:
145 /// Kernel function defining the kernel Gram matrix
147
149
151 /// Array of data pointers for kernel evaluations
152 std::vector<PointerType> x;
153
154 /// counter for the kernel accesses
155 mutable unsigned long long m_accessCounter;
156};
157
158}
159#endif