72 #ifndef KERNEL_SCALE_HH 73 #define KERNEL_SCALE_HH 131 template<
typename KERNEL>
139 :_cacheIsUpToDate(false)
145 _cacheIsUpToDate(false)
158 _algorithm = algorithm;
191 template<
typename ForwardIter,
typename Accessor>
192 void calcScaleAndOffset(
const ForwardIter& fvBegin,
193 const ForwardIter& fvEnd,
194 Accessor accessor)
const;
208 template<
typename FV_IN,
typename FV_OUT>
211 for(
unsigned int i = 0; i < fvIn.size(); ++i)
213 fvOut[i] = (fvIn[i] + _offset[i]) * _scaleFactor[i];
240 template<
typename ForwardIter,
typename Accessor>
241 void updateCache(
const ForwardIter& fvBegin,
242 const ForwardIter& fvEnd,
247 template<
typename ForwardIter1,
typename Accessor1,
248 typename ForwardIter2,
typename Accessor2 >
250 const ForwardIter1& ,
252 const ForwardIter2& ,
253 const ForwardIter2& ,
262 _scaledFVsByUID.clear();
263 _cacheIsUpToDate =
false;
268 _kernel.clearCache();
288 template<
typename FV>
293 if( _cacheIsUpToDate ==
false)
295 if( x.size() != nComponents() || y.size() != nComponents())
298 err <<
"number of scale factors (" << nComponents()
299 <<
") and number of feature vectors components (" 300 << x.size() <<
") don't match. " 301 "Maybe you forgot to specify the scale_algorithm?";
308 xScaled.
resize( x.size());
309 yScaled.
resize( y.size());
310 scaleSingleFV( x, xScaled);
311 scaleSingleFV( y, yScaled);
314 return _kernel.k_function( xScaled, yScaled);
317 unsigned int xUid = x.uniqueID();
318 unsigned int yUid = y.uniqueID();
323 return _kernel.k_function( _scaledFVsByUID[xUid],
324 _scaledFVsByUID[yUid]);
337 return static_cast<unsigned int>(_scaleFactor.size());
351 return _scaleFactor[ index];
364 return _offset[ index];
367 template<
typename STDATA>
371 if( stData.valueExists(
"scale_factor"))
373 _scaleFactor.resize( stData.getArraySize(
"scale_factor"));
374 stData.getArray(
"scale_factor", _scaleFactor.begin(),
375 static_cast<int>(_scaleFactor.size()));
376 _offset.resize( stData.getArraySize(
"scale_offset"));
377 stData.getArray(
"scale_offset", _offset.begin(),
378 static_cast<int>(_offset.size()));
379 if( _scaleFactor.size() != _offset.size())
382 err <<
"sizes of 'scale_factor' array (" 383 << _scaleFactor.size() <<
") and 'scale_offset' " 384 "array (" << _offset.size() <<
") mismatch.";
388 stData.getValue(
"scale_algorithm", _algorithm);
389 _kernel.loadParameters( stData);
407 template<
typename STDATA>
411 _kernel.saveParameters( stData);
412 if( _algorithm !=
"")
414 stData.setValue(
"scale_used_algorithm", _algorithm);
416 if( _scaleFactor.size() != 0)
418 stData.setArray(
"scale_factor", _scaleFactor.begin(),
419 _scaleFactor.size());
420 stData.setArray(
"scale_offset", _offset.begin(),
429 return std::string(
"scaled_") + KERNEL::name();
434 return std::string(
"scaled feature vectors passed to ") + KERNEL::description();
449 KERNEL::getParamInfos( p);
452 p.back().addAlternative(
"minmax",
453 "scale each feature that min becomes -1 " 454 "and max becomes +1");
455 p.back().addAlternative(
"stddev",
456 "scale each feature that mean becomes 0 " 457 "and standard deviation becomes 1");
459 ParamInfo(
"scale_factor",
"sf",
"array",
460 "array containing scale factors for each " 461 "feature -- usually you don't want to specify " 464 ParamInfo(
"scale_offset",
"so",
"array",
465 "array containing offsets for each feature " 466 "-- usually you don't want to specify this " 472 std::string _algorithm;
473 mutable std::vector<double> _offset;
474 mutable std::vector<double> _scaleFactor;
475 mutable bool _cacheIsUpToDate;
476 mutable std::vector<BasicFV> _scaledFVsByUID;
482 #include "Kernel_SCALE.icc"
void saveParameters(STDATA &stData) const
save scale_factor and scale_offset.
#define CHECK_MEMBER_TEMPLATE(c)
unsigned int nComponents() const
number of components (features) in scale factor and offset
#define CHECK_CLASS_TEMPLATE3(c)
const std::string & algorithm() const
return selected algorithm (default is "")
#define SVM_ASSERT(condition)
void loadParameters(STDATA &stData)
void setAlgorithm(const std::string &algorithm)
set algorithm for data scaling
double offset(unsigned int index) const
return offset for the nth component of the feature vector
void scaleSingleFV(const FV_IN &fvIn, FV_OUT &fvOut) const
scale single feature vector with internal offset and scale factor.
#define CHECK_CLASS_TEMPLATE2(c)
void resize(size_type newSize)
void updateCache(const ForwardIter1 &, const ForwardIter1 &, Accessor1, const ForwardIter2 &, const ForwardIter2 &, Accessor2, ProgressReporter *=NULL) const
Ensure that TESTCLASS provides a loadParameters() and saveParamters() method.
The SVMError class is the parent class for all errors that are thrown by the LIBSVMTL.
void setUniqueID(unsigned int uid)
Kernel_SCALE(const KERNEL &kernel)
double k_function(const FV &x, const FV &y) const
kernel function.
static void getParamInfos(std::vector< ParamInfo > &p)
get information about the parameters, that are used in loadParameters() and saveParameters().
The Kernel_SCALE class provides a wrapper for other kernel classes , that scales the feature vectors...
static std::string name()
double scaleFactor(unsigned int index) const
return scale factor for the nth component of the feature vector
#define CHECK_MEMBER_TEMPLATE_2PARAM(c1, c2)
static std::string description()
The ParamInfo class contains informations about one parameter like key, help text, guiHints etc.