/usr/include/shark/LinAlg/PartlyPrecomputedMatrix.h is in libshark-dev 3.1.3+ds1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | //===========================================================================
/*!
*
*
* \brief Partly Precomputed version of a matrix for quadratic programming
*
*
* \par
*
*
*
* \author T. Glasmachers, A. Demircioglu
* \date 2007-2014
*
*
* \par Copyright 1995-2015 Shark Development Team
*
* <BR><HR>
* This file is part of Shark.
* <http://image.diku.dk/shark/>
*
* Shark is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Shark is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Shark. If not, see <http://www.gnu.org/licenses/>.
*
*/
//===========================================================================
#ifndef SHARK_LINALG_PARTLYPRECOMPUTEDMATRIX_H
#define SHARK_LINALG_PARTLYPRECOMPUTEDMATRIX_H
#include <shark/Data/Dataset.h>
#include <shark/LinAlg/Base.h>
#include <vector>
#include <cmath>
namespace shark
{
///
/// \brief Partly Precomputed version of a matrix for quadratic programming
///
/// \par
/// The PartlyPrecomputedMatrix class computes all pairs of kernel
/// evaluations that fits the given cachesize in its constructor and
/// stores them im memory.
///
/// \par
/// Use of this class may be beneficial for certain model
/// selection strategies, where the whole matrix does not fit into
/// memory, and the LRU cache will produce too much hit rates,
/// so that at least partially caching the kernel matrix will help.
/// In particular this will help the KernelSGD/Pegasos algorithm.
///
template <class Matrix>
class PartlyPrecomputedMatrix
{
public:
typedef typename Matrix::QpFloatType QpFloatType;
/// Constructor
/// \param[in] base matrix to be cached. it is assumed that this matrix is not precomputed,
/// but the (costy) computation takes place every time an entry is queried.
/// \param[in] cachesize size of the cache to use in bytes. the size of the cached matrix will
// depend on this value.
PartlyPrecomputedMatrix(Matrix* base, std::size_t cachesize = 0x4000000)
: m_cacheSize(cachesize)
, m_baseMatrix(base)
{
if((m_baseMatrix == NULL) || (m_baseMatrix ->size() == 0))
throw SHARKEXCEPTION("Cannot cache a NULL matrix!");
// remember the original size of the matrix
m_originalNumberOfRows = m_baseMatrix -> size();
// determine how many bytes we need for a single row
size_t rowSizeBytes = m_originalNumberOfRows * sizeof(QpFloatType);
// how many rows fit into our cache?
size_t m_nRows = (size_t) m_cacheSize / rowSizeBytes;
if(m_nRows < 1)
throw SHARKEXCEPTION("Cache size is smaller than the size of a row!");
// if we have more space than needed, well, we do not need it.
if(m_nRows > m_originalNumberOfRows)
m_nRows = m_originalNumberOfRows ;
// resize matrix
m_cachedMatrix.resize(m_nRows, m_baseMatrix ->size());
// copy the rows
for(std::size_t r = 0; r < m_cachedMatrix.size1(); r++)
{
for(std::size_t j = 0; j < m_baseMatrix->size(); j++)
{
m_cachedMatrix(r, j) = (*m_baseMatrix)(r, j);
}
}
}
/// return, if a given row is cached
/// \param[in] k row to check
/// \return is given row in cached matrix or not?
bool isCached(std::size_t k) const
{
if(k < m_cachedMatrix.size1())
return true;
return false;
}
/// return a complete row of the matrix.
/// if the row is cached, it will be returned from there, if not, it will
/// be recomputed on-the-fly and not stored.
/// param[in] k row to compute
/// param[in] storage vector to store the row. must be the same size as a row!
void row(std::size_t k, blas::vector<QpFloatType> &storage) const
{
RANGE_CHECK(k < m_originalNumberOfRows);
RANGE_CHECK(0 <= k);
SIZE_CHECK(storage.size() == m_cachedMatrix.size2());
if(isCached(k) == true)
{
for(std::size_t j = 0; j < m_cachedMatrix.size2(); j++)
{
storage[j] = m_cachedMatrix(k, j);
}
}
else
{
for(std::size_t j = 0; j < m_cachedMatrix.size2(); j++)
{
storage[j] = (*m_baseMatrix)(k, j);
}
}
}
/// return a single matrix entry
/// param[in] i row of entry
/// param[in] j column entry
/// @return value of matrix at given position
QpFloatType operator()(std::size_t i, std::size_t j) const
{
return entry(i, j);
}
/// return a single matrix entry
/// param[in] i row of entry
/// param[in] j column entry
/// @return value of matrix at given position
QpFloatType entry(std::size_t i, std::size_t j) const
{
// check if we have to compute that or not
if(isCached(i))
return m_cachedMatrix(i, j);
// ok, need to compute that element
return (*m_baseMatrix)(i, j);
}
/// return the number of cached rows
/// @return number of rows that are cached
std::size_t size() const
{
return m_cachedMatrix.size();
}
/// return size of cached matrix in QpFloatType units
/// @return the capacity of the cached matrix in QpFloatType units
std::size_t getMaxCacheSize()
{
return m_cachedMatrix.size() * m_cachedMatrix.size2();
}
/// return the dimension of a row in the cache (as we do not shorten our
/// rows, this must be the same as the dimension of a row in the original kernel matrix).
/// @return dimension of any cached row
std::size_t getCacheRowSize() const
{
return m_cachedMatrix.size2();
}
protected:
/// container for precomputed values
blas::matrix<QpFloatType> m_cachedMatrix;
// maximal size of cache
size_t m_cacheSize;
// original kernel matrix, will be accessed if entries outsied the cache are requested
Matrix* m_baseMatrix;
// remember how big the original matrix was to prevent access errors
size_t m_originalNumberOfRows;
};
}
#endif
|