/usr/include/shogun/kernel/Kernel.h is in libshogun-dev 3.2.0-7.3build4.
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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 | /*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 1999-2009 Soeren Sonnenburg
* Written (W) 1999-2008 Gunnar Raetsch
* Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
*/
#ifndef _KERNEL_H___
#define _KERNEL_H___
#include <shogun/lib/common.h>
#include <shogun/lib/Signal.h>
#include <shogun/io/SGIO.h>
#include <shogun/io/File.h>
#include <shogun/mathematics/Math.h>
#include <shogun/features/FeatureTypes.h>
#include <shogun/base/SGObject.h>
#include <shogun/features/Features.h>
#include <shogun/kernel/normalizer/KernelNormalizer.h>
namespace shogun
{
class CFile;
class CFeatures;
class CKernelNormalizer;
#ifdef USE_SHORTREAL_KERNELCACHE
/** kernel cache element */
typedef float32_t KERNELCACHE_ELEM;
#else
/** kernel cache element */
typedef float64_t KERNELCACHE_ELEM;
#endif
/** kernel cache index */
typedef int64_t KERNELCACHE_IDX;
/** optimization type */
enum EOptimizationType
{
FASTBUTMEMHUNGRY,
SLOWBUTMEMEFFICIENT
};
/** kernel type */
enum EKernelType
{
K_UNKNOWN = 0,
K_LINEAR = 10,
K_POLY = 20,
K_GAUSSIAN = 30,
K_GAUSSIANSHIFT = 32,
K_GAUSSIANMATCH = 33,
K_HISTOGRAM = 40,
K_SALZBERG = 41,
K_LOCALITYIMPROVED = 50,
K_SIMPLELOCALITYIMPROVED = 60,
K_FIXEDDEGREE = 70,
K_WEIGHTEDDEGREE = 80,
K_WEIGHTEDDEGREEPOS = 81,
K_WEIGHTEDDEGREERBF = 82,
K_WEIGHTEDCOMMWORDSTRING = 90,
K_POLYMATCH = 100,
K_ALIGNMENT = 110,
K_COMMWORDSTRING = 120,
K_COMMULONGSTRING = 121,
K_SPECTRUMRBF = 122,
K_SPECTRUMMISMATCHRBF = 123,
K_COMBINED = 140,
K_AUC = 150,
K_CUSTOM = 160,
K_SIGMOID = 170,
K_CHI2 = 180,
K_DIAG = 190,
K_CONST = 200,
K_DISTANCE = 220,
K_LOCALALIGNMENT = 230,
K_PYRAMIDCHI2 = 240,
K_OLIGO = 250,
K_MATCHWORD = 260,
K_TPPK = 270,
K_REGULATORYMODULES = 280,
K_SPARSESPATIALSAMPLE = 290,
K_HISTOGRAMINTERSECTION = 300,
K_WAVELET = 310,
K_WAVE = 320,
K_CAUCHY = 330,
K_TSTUDENT = 340,
K_RATIONAL_QUADRATIC = 350,
K_MULTIQUADRIC = 360,
K_EXPONENTIAL = 370,
K_SPHERICAL = 380,
K_SPLINE = 390,
K_ANOVA = 400,
K_POWER = 410,
K_LOG = 420,
K_CIRCULAR = 430,
K_INVERSEMULTIQUADRIC = 440,
K_DISTANTSEGMENTS = 450,
K_BESSEL = 460,
K_JENSENSHANNON = 470,
K_DIRECTOR = 480,
K_PRODUCT = 490,
K_LINEARARD = 500,
K_GAUSSIANARD = 510,
K_STREAMING = 520
};
/** kernel property */
enum EKernelProperty
{
KP_NONE = 0,
KP_LINADD = 1, // Kernels that can be optimized via doing normal updates w + dw
KP_KERNCOMBINATION = 2, // Kernels that are infact a linear combination of subkernels K=\sum_i b_i*K_i
KP_BATCHEVALUATION = 4 // Kernels that can on the fly generate normals in linadd and more quickly/memory efficient process batches instead of single examples
};
class CSVM;
/** @brief The Kernel base class.
*
* Non-mathematically spoken, a kernel is a function
* that given two input objects \f${\bf x}\f$ and \f${\bf x'}\f$ returns a
* score describing the similarity of the vectors. The score should be larger
* when the objects are more similar.
*
* It can be defined as
*
* \f[
* k({\bf x},{\bf x'})= \Phi_k({\bf x})\cdot \Phi_k({\bf x'})
* \f]
*
* where \f$\Phi\f$ maps the objects into some potentially high dimensional
* feature space.
*
* Apart from the input features, the base kernel takes only one argument (the
* size of the kernel cache) that is used to efficiently train kernel-machines
* like e.g. SVMs.
*
* In case you would like to define your own kernel, you only have to define a
* new compute() function (and the kernel name via get_name() and
* the kernel type get_kernel_type()). A good example to look at is the
* GaussianKernel.
*/
class CKernel : public CSGObject
{
friend class CVarianceKernelNormalizer;
friend class CSqrtDiagKernelNormalizer;
friend class CAvgDiagKernelNormalizer;
friend class CRidgeKernelNormalizer;
friend class CFirstElementKernelNormalizer;
friend class CMultitaskKernelNormalizer;
friend class CMultitaskKernelMklNormalizer;
friend class CMultitaskKernelMaskNormalizer;
friend class CMultitaskKernelMaskPairNormalizer;
friend class CTanimotoKernelNormalizer;
friend class CDiceKernelNormalizer;
friend class CZeroMeanCenterKernelNormalizer;
friend class CStreamingKernel;
public:
/** default constructor
*
*/
CKernel();
/** constructor
*
* @param size cache size
*/
CKernel(int32_t size);
/** constructor
*
* @param l features for left-hand side
* @param r features for right-hand side
* @param size cache size
*/
CKernel(CFeatures* l, CFeatures* r, int32_t size);
virtual ~CKernel();
/** get kernel function for lhs feature vector a
* and rhs feature vector b
*
* @param idx_a index of feature vector a
* @param idx_b index of feature vector b
* @return computed kernel function
*/
inline float64_t kernel(int32_t idx_a, int32_t idx_b)
{
REQUIRE(idx_a>=0 && idx_b>=0 && idx_a<num_lhs && idx_b<num_rhs,
"%s::kernel(): index out of Range: idx_a=%d/%d idx_b=%d/%d\n",
get_name(), idx_a,num_lhs, idx_b,num_rhs);
return normalizer->normalize(compute(idx_a, idx_b), idx_a, idx_b);
}
/** get kernel matrix
*
* @return computed kernel matrix (needs to be cleaned up)
*/
SGMatrix<float64_t> get_kernel_matrix()
{
return get_kernel_matrix<float64_t>();
}
/** @return Vector with diagonal elements of the kernel matrix.
* Note that left- and right-handside features must be set and of equal
* size
*
* @param preallocated vector with space for results
*/
SGVector<float64_t> get_kernel_diagonal(SGVector<float64_t>
preallocated=SGVector<float64_t>())
{
REQUIRE(lhs, "CKernel::get_kernel_diagonal(): Left-handside "
"features missing!\n");
REQUIRE(rhs, "CKernel::get_kernel_diagonal(): Right-handside "
"features missing!\n");
REQUIRE(lhs->get_num_vectors()==rhs->get_num_vectors(),
"CKernel::get_kernel_diagonal(): Left- and right-"
"handside features must be equal sized\n");
/* allocate space if necessary */
if (!preallocated.vector)
preallocated=SGVector<float64_t>(lhs->get_num_vectors());
else
{
REQUIRE(preallocated.vlen==lhs->get_num_vectors(),
"%s::get_kernel_diagonal(): Preallocated vector has"
" wrong size!\n", get_name());
}
for (index_t i=0; i<preallocated.vlen; ++i)
preallocated[i]=kernel(i, i);
return preallocated;
}
/**
* get column j
*
* @return the jth column of the kernel matrix
*/
virtual SGVector<float64_t> get_kernel_col(int32_t j)
{
SGVector<float64_t> col = SGVector<float64_t>(num_rhs);
for (int32_t i=0; i!=num_rhs; i++)
col[i] = kernel(i,j);
return col;
}
/**
* get row i
*
* @return the ith row of the kernel matrix
*/
virtual SGVector<float64_t> get_kernel_row(int32_t i)
{
SGVector<float64_t> row = SGVector<float64_t>(num_lhs);
for (int32_t j=0; j!=num_lhs; j++)
row[j] = kernel(i,j);
return row;
}
/** get kernel matrix (templated)
*
* @return the kernel matrix
*/
template <class T> SGMatrix<T> get_kernel_matrix();
/** initialize kernel
* e.g. setup lhs/rhs of kernel, precompute normalization
* constants etc.
* make sure to check that your kernel can deal with the
* supplied features (!)
*
* @param lhs features for left-hand side
* @param rhs features for right-hand side
* @return if init was successful
*/
virtual bool init(CFeatures* lhs, CFeatures* rhs);
/** set the current kernel normalizer
*
* @return if successful
*/
virtual bool set_normalizer(CKernelNormalizer* normalizer);
/** obtain the current kernel normalizer
*
* @return the kernel normalizer
*/
virtual CKernelNormalizer* get_normalizer();
/** initialize the current kernel normalizer
* @return if init was successful
*/
virtual bool init_normalizer();
/** clean up your kernel
*
* base method only removes lhs and rhs
* overload to add further cleanup but make sure CKernel::cleanup() is
* called
*/
virtual void cleanup();
/** load the kernel matrix
*
* @param loader File object via which to load data
*/
void load(CFile* loader);
/** save kernel matrix
*
* @param writer File object via which to save data
*/
void save(CFile* writer);
/** get left-hand side of features used in kernel
*
* @return features of left-hand side
*/
inline CFeatures* get_lhs() { SG_REF(lhs); return lhs; }
/** get right-hand side of features used in kernel
*
* @return features of right-hand side
*/
inline CFeatures* get_rhs() { SG_REF(rhs); return rhs; }
/** get number of vectors of lhs features
*
* @return number of vectors of left-hand side
*/
virtual int32_t get_num_vec_lhs()
{
return num_lhs;
}
/** get number of vectors of rhs features
*
* @return number of vectors of right-hand side
*/
virtual int32_t get_num_vec_rhs()
{
return num_rhs;
}
/** test whether features have been assigned to lhs and rhs
*
* @return true if features are assigned
*/
virtual bool has_features()
{
return lhs && rhs;
}
/** test whether features on lhs and rhs are the same
*
* @return true if features are the same
*/
inline bool get_lhs_equals_rhs()
{
return lhs_equals_rhs;
}
/** remove lhs and rhs from kernel */
virtual void remove_lhs_and_rhs();
/** remove lhs from kernel */
virtual void remove_lhs();
/** remove rhs from kernel */
virtual void remove_rhs();
/** return what type of kernel we are, e.g.
* Linear,Polynomial, Gaussian,...
*
* abstract base method
*
* @return kernel type
*/
virtual EKernelType get_kernel_type()=0 ;
/** return feature type the kernel can deal with
*
* abstract base method
*
* @return feature type
*/
virtual EFeatureType get_feature_type()=0;
/** return feature class the kernel can deal with
*
* abstract base method
*
* @return feature class
*/
virtual EFeatureClass get_feature_class()=0;
/** set the size of the kernel cache
*
* @param size of kernel cache
*/
inline void set_cache_size(int32_t size)
{
cache_size = size;
}
/** return the size of the kernel cache
*
* @return size of kernel cache
*/
inline int32_t get_cache_size() { return cache_size; }
/** list kernel */
void list_kernel();
/** check if kernel has given property
*
* @param p kernel property
* @return if kernel has given property
*/
inline bool has_property(EKernelProperty p) { return (properties & p) != 0; }
/** for optimizable kernels, i.e. kernels where the weight
* vector can be computed explicitly (if it fits into memory)
*/
virtual void clear_normal();
/** add vector*factor to 'virtual' normal vector
*
* @param vector_idx index
* @param weight weight
*/
virtual void add_to_normal(int32_t vector_idx, float64_t weight);
/** get optimization type
*
* @return optimization type
*/
inline EOptimizationType get_optimization_type() { return opt_type; }
/** set optimization type
*
* @param t optimization type to set
*/
virtual void set_optimization_type(EOptimizationType t) { opt_type=t;}
/** check if optimization is initialized
*
* @return if optimization is initialized
*/
inline bool get_is_initialized() { return optimization_initialized; }
/** initialize optimization
*
* @param count count
* @param IDX index
* @param weights weights
* @return if initializing was successful
*/
virtual bool init_optimization(
int32_t count, int32_t *IDX, float64_t *weights);
/** delete optimization
*
* @return if deleting was successful
*/
virtual bool delete_optimization();
/** initialize optimization
*
* @param svm svm model
* @return if initializing was successful
*/
bool init_optimization_svm(CSVM * svm) ;
/** compute optimized
*
* @param vector_idx index to compute
* @return optimized value at given index
*/
virtual float64_t compute_optimized(int32_t vector_idx);
/** computes output for a batch of examples in an optimized fashion
* (favorable if kernel supports it, i.e. has KP_BATCHEVALUATION. to
* the outputvector target (of length num_vec elements) the output for
* the examples enumerated in vec_idx are added. therefore make sure
* that it is initialized with ZERO. the following num_suppvec, IDX,
* alphas arguments are the number of support vectors, their indices
* and weights
*/
virtual void compute_batch(
int32_t num_vec, int32_t* vec_idx, float64_t* target,
int32_t num_suppvec, int32_t* IDX, float64_t* alphas,
float64_t factor=1.0);
/** get combined kernel weight
*
* @return combined kernel weight
*/
inline float64_t get_combined_kernel_weight() { return combined_kernel_weight; }
/** set combined kernel weight
*
* @param nw new combined kernel weight
*/
inline void set_combined_kernel_weight(float64_t nw) { combined_kernel_weight=nw; }
/** get number of subkernels
*
* @return number of subkernels
*/
virtual int32_t get_num_subkernels();
/** compute by subkernel
*
* @param vector_idx index
* @param subkernel_contrib subkernel contribution
*/
virtual void compute_by_subkernel(
int32_t vector_idx, float64_t * subkernel_contrib);
/** get subkernel weights
*
* @param num_weights number of weights will be stored here
* @return subkernel weights
*/
virtual const float64_t* get_subkernel_weights(int32_t& num_weights);
/** get subkernel weights (swig compatible)
*
* @return subkernel weights
*/
virtual SGVector<float64_t> get_subkernel_weights();
/** set subkernel weights
*
* @param weights new subkernel weights
*/
virtual void set_subkernel_weights(SGVector<float64_t> weights);
/** return derivative with respect to specified parameter
*
* @param param the parameter
* @param index the index of the element if parameter is a vector
*
* @return gradient with respect to parameter
*/
virtual SGMatrix<float64_t> get_parameter_gradient(
const TParameter* param, index_t index=-1)
{
SG_ERROR("Can't compute derivative wrt %s parameter\n", param->m_name)
return SGMatrix<float64_t>();
}
/** Obtains a kernel from a generic SGObject with error checking. Note
* that if passing NULL, result will be NULL
* @param kernel Object to cast to CKernel, is *not* SG_REFed
* @return object casted to CKernel, NULL if not possible
*/
static CKernel* obtain_from_generic(CSGObject* kernel);
protected:
/** set property
*
* @param p kernel property to set
*/
inline void set_property(EKernelProperty p)
{
properties |= p;
}
/** unset property
*
* @param p kernel property to unset
*/
inline void unset_property(EKernelProperty p)
{
properties &= (properties | p) ^ p;
}
/** set is initialized
*
* @param p_init if optimization shall be set to initialized
*/
inline void set_is_initialized(bool p_init) { optimization_initialized=p_init; }
/** compute kernel function for features a and b
* idx_{a,b} denote the index of the feature vectors
* in the corresponding feature object
*
* abstract base method
*
* @param x index a
* @param y index b
* @return computed kernel function at indices a,b
*/
virtual float64_t compute(int32_t x, int32_t y)=0;
/** compute row start offset for parallel kernel matrix computation
*
* @param offs offset
* @param n number of columns
* @param symmetric whether matrix is symmetric
*/
int32_t compute_row_start(int64_t offs, int32_t n, bool symmetric)
{
int32_t i_start;
if (symmetric)
i_start=(int32_t) CMath::floor(n-CMath::sqrt(CMath::sq((float64_t) n)-offs));
else
i_start=(int32_t) (offs/int64_t(n));
return i_start;
}
/** helper for computing the kernel matrix in a parallel way
*
* @param p thread parameters
*/
template <class T> static void* get_kernel_matrix_helper(void* p);
/** Can (optionally) be overridden to post-initialize some member
* variables which are not PARAMETER::ADD'ed. Make sure that at
* first the overridden method BASE_CLASS::LOAD_SERIALIZABLE_POST
* is called.
*
* @exception ShogunException Will be thrown if an error
* occurres.
*/
virtual void load_serializable_post() throw (ShogunException);
/** Can (optionally) be overridden to pre-initialize some member
* variables which are not PARAMETER::ADD'ed. Make sure that at
* first the overridden method BASE_CLASS::SAVE_SERIALIZABLE_PRE
* is called.
*
* @exception ShogunException Will be thrown if an error
* occurres.
*/
virtual void save_serializable_pre() throw (ShogunException);
/** Can (optionally) be overridden to post-initialize some member
* variables which are not PARAMETER::ADD'ed. Make sure that at
* first the overridden method BASE_CLASS::SAVE_SERIALIZABLE_POST
* is called.
*
* @exception ShogunException Will be thrown if an error
* occurres.
*/
virtual void save_serializable_post() throw (ShogunException);
/** Separate the function of parameter registration
* This can be the first stage of a *general* framework for
* cross-validation or other parameter-based operations
*/
virtual void register_params();
private:
/** Do basic initialisations like default settings
* and registering parameters */
void init();
//@}
protected:
/// cache_size in MB
int32_t cache_size;
/// this *COULD* store the whole kernel matrix
/// usually not applicable / necessary to compute the whole matrix
KERNELCACHE_ELEM* kernel_matrix;
/// feature vectors to occur on left hand side
CFeatures* lhs;
/// feature vectors to occur on right hand side
CFeatures* rhs;
/// lhs
bool lhs_equals_rhs;
/// number of feature vectors on left hand side
int32_t num_lhs;
/// number of feature vectors on right hand side
int32_t num_rhs;
/** combined kernel weight */
float64_t combined_kernel_weight;
/** if optimization is initialized */
bool optimization_initialized;
/** optimization type (currently FASTBUTMEMHUNGRY and
* SLOWBUTMEMEFFICIENT)
*/
EOptimizationType opt_type;
/** kernel properties */
uint64_t properties;
/** normalize the kernel(i,j) function based on this normalization
* function */
CKernelNormalizer* normalizer;
};
}
#endif /* _KERNEL_H__ */
|