67 #ifndef KERNEL_MATRIX_HH 68 #define KERNEL_MATRIX_HH 92 template<
typename KERNEL>
103 _cacheIsUpToDate( false)
111 _cacheIsUpToDate( false)
118 #ifdef ALLOW_KERNEL_MATRIX_COPY 134 _cacheIsUpToDate = orig._cacheIsUpToDate;
136 if( orig._width != 0)
138 unsigned int size = _width*_width;
139 std::copy( orig._data, orig._data + size, _data);
155 if( width == _width)
return;
162 _data =
new double[width*width];
163 _rowStarts =
new double*[width];
165 for(
long i = 0; i < width; ++i)
167 _rowStarts[i] = _data + i*width;
192 _cacheIsUpToDate =
false;
198 _kernel.clearCache();
203 template<
typename ForwardIter,
typename Accessor>
205 const ForwardIter& fvEnd,
213 _kernel.updateCache( fvBegin, fvEnd, accessor, pr);
219 unsigned int maxUID = 0;
221 for( ForwardIter p = fvBegin; p != fvEnd; ++p)
223 if( accessor(p).uniqueID() > maxUID)
225 maxUID = accessor(p).uniqueID();
234 size_t nFeatureVectors = fvEnd - fvBegin;
235 ForwardIter p = fvBegin;
236 int nKernelEvaluations =
237 static_cast<int>(nFeatureVectors * (nFeatureVectors + 1) / 2);
238 int reportProgressEveryNthEval = nKernelEvaluations / 100;
239 if( reportProgressEveryNthEval == 0) reportProgressEveryNthEval = 1;
240 int nKernelEvaluationsFinished = 0;
241 int silentProgressCounter = reportProgressEveryNthEval;
243 for(
unsigned int i = 0; i < nFeatureVectors; ++i, ++p)
245 unsigned int row = accessor(p).uniqueID();
246 ForwardIter q = fvBegin + i;
247 for(
unsigned int j = i; j < nFeatureVectors; ++j, ++q)
249 unsigned int col = accessor(q).uniqueID();
250 double val = _kernel.k_function( accessor(p), accessor(q));
251 _rowStarts[row][col] = val;
252 _rowStarts[col][row] = val;
253 ++nKernelEvaluationsFinished;
254 --silentProgressCounter;
255 if( silentProgressCounter <= 0)
257 silentProgressCounter = reportProgressEveryNthEval;
262 "calculating full kernel matrix",
263 static_cast<float>(nKernelEvaluationsFinished) /
264 static_cast<float>(nKernelEvaluations),
"");
273 "full kernel matrix calculated", 1.0,
276 _cacheIsUpToDate =
true;
281 template<
typename ForwardIter1,
typename Accessor1,
282 typename ForwardIter2,
typename Accessor2 >
294 template<
typename FV>
298 if( _cacheIsUpToDate ==
false)
300 return _kernel.k_function( x, y);
303 long row =
static_cast<long>(x.uniqueID());
304 long col =
static_cast<long>(y.uniqueID());
309 return _rowStarts[row][col];
312 template<
typename STDATA>
316 _kernel.loadParameters( stData);
319 template<
typename STDATA>
323 _kernel.saveParameters( stData);
324 stData.setValue(
"kernel_type",
name());
329 return std::string(
"kmatrix_") + KERNEL::name();
334 return std::string(
"cached kernel matrix for ") + KERNEL::description();
349 KERNEL::getParamInfos( p);
354 mutable double* _data;
355 mutable double** _rowStarts;
357 mutable bool _cacheIsUpToDate;
static std::string description()
double k_function(const FV &x, const FV &y) const
#define CHECK_MEMBER_TEMPLATE(c)
void resizeCache(long width) const
#define SVM_ASSERT(condition)
const unsigned int MAX_BELIEVABLE_UNIQUE_ID
#define CHECK_CLASS_TEMPLATE1(c)
Kernel_MATRIX(const KERNEL &kernel)
void saveParameters(STDATA &stData) const
#define CHECK_CLASS_TEMPLATE2(c)
Ensure that TESTCLASS provides a loadParameters() and saveParamters() method.
void loadParameters(STDATA &stData)
void updateCache(const ForwardIter &fvBegin, const ForwardIter &fvEnd, Accessor accessor, ProgressReporter *pr=0) const
static void getParamInfos(std::vector< ParamInfo > &p)
get information about the parameters, that are used in loadParameters() and saveParameters().
const int TASK_LEVEL_CROSS_VAL
static std::string name()
void updateCache(const ForwardIter1 &, const ForwardIter1 &, Accessor1, const ForwardIter2 &, const ForwardIter2 &, Accessor2, ProgressReporter *=0) const