SHOGUN  4.1.0
Kernel.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 1999-2009 Soeren Sonnenburg
8  * Written (W) 1999-2008 Gunnar Raetsch
9  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
10  */
11 
12 #ifndef _KERNEL_H___
13 #define _KERNEL_H___
14 
15 #include <shogun/lib/config.h>
16 
17 #include <shogun/lib/common.h>
18 #include <shogun/lib/Signal.h>
19 #include <shogun/io/SGIO.h>
20 #include <shogun/io/File.h>
23 #include <shogun/base/SGObject.h>
24 #include <shogun/lib/SGMatrix.h>
27 
28 namespace shogun
29 {
30  class CFile;
31  class CFeatures;
33 
34 #ifdef USE_SHORTREAL_KERNELCACHE
35 
37 #else
38 
40 #endif
41 
43 typedef int64_t KERNELCACHE_IDX;
44 
45 
48 {
51 };
52 
55 {
56  K_UNKNOWN = 0,
57  K_LINEAR = 10,
58  K_POLY = 20,
59  K_GAUSSIAN = 30,
63  K_SALZBERG = 41,
71  K_POLYMATCH = 100,
72  K_ALIGNMENT = 110,
77  K_COMBINED = 140,
78  K_AUC = 150,
79  K_CUSTOM = 160,
80  K_SIGMOID = 170,
81  K_CHI2 = 180,
82  K_DIAG = 190,
83  K_CONST = 200,
84  K_DISTANCE = 220,
87  K_OLIGO = 250,
88  K_MATCHWORD = 260,
89  K_TPPK = 270,
93  K_WAVELET = 310,
94  K_WAVE = 320,
95  K_CAUCHY = 330,
96  K_TSTUDENT = 340,
100  K_SPHERICAL = 380,
101  K_SPLINE = 390,
102  K_ANOVA = 400,
103  K_POWER = 410,
104  K_LOG = 420,
105  K_CIRCULAR = 430,
108  K_BESSEL = 460,
110  K_DIRECTOR = 480,
111  K_PRODUCT = 490,
115  K_STREAMING = 520,
117 };
118 
121 {
122  KP_NONE = 0,
123  KP_LINADD = 1, // Kernels that can be optimized via doing normal updates w + dw
124  KP_KERNCOMBINATION = 2, // Kernels that are infact a linear combination of subkernels K=\sum_i b_i*K_i
125  KP_BATCHEVALUATION = 4 // Kernels that can on the fly generate normals in linadd and more quickly/memory efficient process batches instead of single examples
126 };
127 
128 class CSVM;
129 
155 class CKernel : public CSGObject
156 {
167  friend class CDiceKernelNormalizer;
169 
170  friend class CStreamingKernel;
171 
172  public:
173 
177  CKernel();
178 
179 
184  CKernel(int32_t size);
185 
192  CKernel(CFeatures* l, CFeatures* r, int32_t size);
193 
194  virtual ~CKernel();
195 
203  inline float64_t kernel(int32_t idx_a, int32_t idx_b)
204  {
205  REQUIRE(idx_a>=0 && idx_b>=0 && idx_a<num_lhs && idx_b<num_rhs,
206  "%s::kernel(): index out of Range: idx_a=%d/%d idx_b=%d/%d\n",
207  get_name(), idx_a,num_lhs, idx_b,num_rhs);
208 
209  return normalizer->normalize(compute(idx_a, idx_b), idx_a, idx_b);
210  }
211 
217  {
218  return get_kernel_matrix<float64_t>();
219  }
220 
228  preallocated=SGVector<float64_t>())
229  {
230  REQUIRE(lhs, "CKernel::get_kernel_diagonal(): Left-handside "
231  "features missing!\n");
232 
233  REQUIRE(rhs, "CKernel::get_kernel_diagonal(): Right-handside "
234  "features missing!\n");
235 
236  int32_t length=CMath::min(lhs->get_num_vectors(),rhs->get_num_vectors());
237 
238  /* allocate space if necessary */
239  if (!preallocated.vector)
240  preallocated=SGVector<float64_t>(length);
241  else
242  {
243  REQUIRE(preallocated.vlen==length,
244  "%s::get_kernel_diagonal(): Preallocated vector has"
245  " wrong size!\n", get_name());
246  }
247 
248  for (index_t i=0; i<preallocated.vlen; ++i)
249  preallocated[i]=kernel(i, i);
250 
251  return preallocated;
252  }
253 
260  {
261 
263 
264  for (int32_t i=0; i!=num_rhs; i++)
265  col[i] = kernel(i,j);
266 
267  return col;
268  }
269 
270 
277  {
279 
280  for (int32_t j=0; j!=num_lhs; j++)
281  row[j] = kernel(i,j);
282 
283  return row;
284  }
285 
309  virtual float64_t sum_symmetric_block(index_t block_begin,
310  index_t block_size, bool no_diag=true);
311 
340  virtual float64_t sum_block(index_t block_begin_row,
341  index_t block_begin_col, index_t block_size_row,
342  index_t block_size_col, bool no_diag=false);
343 
368  block_begin, index_t block_size, bool no_diag=true);
369 
400  index_t block_begin, index_t block_size, bool no_diag=true);
401 
438  index_t block_begin_row, index_t block_begin_col,
439  index_t block_size_row, index_t block_size_col,
440  bool no_diag=false);
441 
446  template <class T> SGMatrix<T> get_kernel_matrix();
447 
458  virtual bool init(CFeatures* lhs, CFeatures* rhs);
459 
465 
471 
475  virtual bool init_normalizer();
476 
483  virtual void cleanup();
484 
489  void load(CFile* loader);
490 
495  void save(CFile* writer);
496 
501  inline CFeatures* get_lhs() { SG_REF(lhs); return lhs; }
502 
507  inline CFeatures* get_rhs() { SG_REF(rhs); return rhs; }
508 
513  virtual int32_t get_num_vec_lhs()
514  {
515  return num_lhs;
516  }
517 
522  virtual int32_t get_num_vec_rhs()
523  {
524  return num_rhs;
525  }
526 
531  virtual bool has_features()
532  {
533  return lhs && rhs;
534  }
535 
540  inline bool get_lhs_equals_rhs()
541  {
542  return lhs_equals_rhs;
543  }
544 
546  virtual void remove_lhs_and_rhs();
547 
549  virtual void remove_lhs();
550 
552  virtual void remove_rhs();
553 
561  virtual EKernelType get_kernel_type()=0 ;
562 
569  virtual EFeatureType get_feature_type()=0;
570 
577  virtual EFeatureClass get_feature_class()=0;
578 
583  inline void set_cache_size(int32_t size)
584  {
585  cache_size = size;
586 
587  }
588 
593  inline int32_t get_cache_size() { return cache_size; }
594 
595 
596 
598  void list_kernel();
599 
605  inline bool has_property(EKernelProperty p) { return (properties & p) != 0; }
606 
610  virtual void clear_normal();
611 
617  virtual void add_to_normal(int32_t vector_idx, float64_t weight);
618 
624 
630 
636 
644  virtual bool init_optimization(
645  int32_t count, int32_t *IDX, float64_t *weights);
646 
651  virtual bool delete_optimization();
652 
658  bool init_optimization_svm(CSVM * svm) ;
659 
665  virtual float64_t compute_optimized(int32_t vector_idx);
666 
675  virtual void compute_batch(
676  int32_t num_vec, int32_t* vec_idx, float64_t* target,
677  int32_t num_suppvec, int32_t* IDX, float64_t* alphas,
678  float64_t factor=1.0);
679 
685 
691 
696  virtual int32_t get_num_subkernels();
697 
703  virtual void compute_by_subkernel(
704  int32_t vector_idx, float64_t * subkernel_contrib);
705 
711  virtual const float64_t* get_subkernel_weights(int32_t& num_weights);
712 
718 
723  virtual void set_subkernel_weights(SGVector<float64_t> weights);
724 
733  const TParameter* param, index_t index=-1)
734  {
735  SG_ERROR("Can't compute derivative wrt %s parameter\n", param->m_name)
736  return SGMatrix<float64_t>();
737  }
738 
747  const TParameter* param, index_t index=-1)
748  {
749  return get_parameter_gradient(param,index).get_diagonal_vector();
750  }
751 
758  protected:
764  {
765  properties |= p;
766  }
767 
773  {
774  properties &= (properties | p) ^ p;
775  }
776 
781  inline void set_is_initialized(bool p_init) { optimization_initialized=p_init; }
782 
793  virtual float64_t compute(int32_t x, int32_t y)=0;
794 
801  int32_t compute_row_start(int64_t offs, int32_t n, bool symmetric)
802  {
803  int32_t i_start;
804 
805  if (symmetric)
806  i_start=(int32_t) CMath::floor(n-CMath::sqrt(CMath::sq((float64_t) n)-offs));
807  else
808  i_start=(int32_t) (offs/int64_t(n));
809 
810  return i_start;
811  }
812 
817  template <class T> static void* get_kernel_matrix_helper(void* p);
818 
827  virtual void load_serializable_post() throw (ShogunException);
828 
837  virtual void save_serializable_pre() throw (ShogunException);
838 
847  virtual void save_serializable_post() throw (ShogunException);
848 
853  virtual void register_params();
854 
855  private:
858  void init();
859 
860 
861 
863 
864  protected:
866  int32_t cache_size;
867 
868 
869 
872  KERNELCACHE_ELEM* kernel_matrix;
873 
878 
881 
883  int32_t num_lhs;
885  int32_t num_rhs;
886 
889 
896 
898  uint64_t properties;
899 
903 };
904 
905 }
906 #endif /* _KERNEL_H__ */
virtual void clear_normal()
Definition: Kernel.cpp:371
virtual const char * get_name() const =0
virtual void load_serializable_post()
Definition: Kernel.cpp:441
int32_t compute_row_start(int64_t offs, int32_t n, bool symmetric)
Definition: Kernel.h:801
The MultitaskKernel allows Multitask Learning via a modified kernel function.
virtual void cleanup()
Definition: Kernel.cpp:158
virtual void compute_by_subkernel(int32_t vector_idx, float64_t *subkernel_contrib)
Definition: Kernel.cpp:381
EKernelType
Definition: Kernel.h:54
virtual float64_t compute(int32_t x, int32_t y)=0
int32_t index_t
Definition: common.h:62
DiceKernelNormalizer performs kernel normalization inspired by the Dice coefficient (see http://en...
The MultitaskKernel allows Multitask Learning via a modified kernel function.
int32_t num_rhs
number of feature vectors on right hand side
Definition: Kernel.h:885
static void * get_kernel_matrix_helper(void *p)
Definition: Kernel.cpp:803
Class ShogunException defines an exception which is thrown whenever an error inside of shogun occurs...
virtual bool set_normalizer(CKernelNormalizer *normalizer)
Definition: Kernel.cpp:135
virtual float64_t sum_block(index_t block_begin_row, index_t block_begin_col, index_t block_size_row, index_t block_size_col, bool no_diag=false)
Definition: Kernel.cpp:590
static T sq(T x)
Definition: Math.h:450
bool get_lhs_equals_rhs()
Definition: Kernel.h:540
parameter struct
virtual int32_t get_num_vectors() const =0
CFeatures * get_rhs()
Definition: Kernel.h:507
#define SG_ERROR(...)
Definition: SGIO.h:129
#define REQUIRE(x,...)
Definition: SGIO.h:206
void set_is_initialized(bool p_init)
Definition: Kernel.h:781
virtual bool delete_optimization()
Definition: Kernel.cpp:347
int64_t KERNELCACHE_IDX
Definition: Kernel.h:43
void set_cache_size(int32_t size)
Definition: Kernel.h:583
float64_t kernel(int32_t idx_a, int32_t idx_b)
Definition: Kernel.h:203
virtual void set_optimization_type(EOptimizationType t)
Definition: Kernel.h:629
uint64_t properties
Definition: Kernel.h:898
virtual void remove_rhs()
takes all necessary steps if the rhs is removed from kernel
Definition: Kernel.cpp:208
TanimotoKernelNormalizer performs kernel normalization inspired by the Tanimoto coefficient (see http...
virtual int32_t get_num_vec_lhs()
Definition: Kernel.h:513
SGMatrix< float64_t > get_kernel_matrix()
Definition: Kernel.h:216
#define SG_REF(x)
Definition: SGObject.h:51
static float64_t floor(float64_t d)
Definition: Math.h:407
int32_t cache_size
cache_size in MB
Definition: Kernel.h:866
EFeatureClass
shogun feature class
Definition: FeatureTypes.h:38
bool get_is_initialized()
Definition: Kernel.h:635
virtual SGMatrix< float64_t > row_wise_sum_squared_sum_symmetric_block(index_t block_begin, index_t block_size, bool no_diag=true)
Definition: Kernel.cpp:690
float64_t combined_kernel_weight
Definition: Kernel.h:888
virtual void register_params()
Definition: Kernel.cpp:464
void save(CFile *writer)
Definition: Kernel.cpp:171
virtual SGVector< float64_t > get_kernel_col(int32_t j)
Definition: Kernel.h:259
virtual void remove_lhs_and_rhs()
Definition: Kernel.cpp:179
bool has_property(EKernelProperty p)
Definition: Kernel.h:605
virtual CKernelNormalizer * get_normalizer()
Definition: Kernel.cpp:147
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:112
virtual SGVector< float64_t > row_col_wise_sum_block(index_t block_begin_row, index_t block_begin_col, index_t block_size_row, index_t block_size_col, bool no_diag=false)
Definition: Kernel.cpp:749
EKernelProperty
Definition: Kernel.h:120
virtual float64_t sum_symmetric_block(index_t block_begin, index_t block_size, bool no_diag=true)
Definition: Kernel.cpp:537
virtual SGVector< float64_t > get_subkernel_weights()
Definition: Kernel.cpp:393
double float64_t
Definition: common.h:50
virtual EFeatureType get_feature_type()=0
void set_combined_kernel_weight(float64_t nw)
Definition: Kernel.h:690
KERNELCACHE_ELEM * kernel_matrix
Definition: Kernel.h:872
A File access base class.
Definition: File.h:34
virtual void save_serializable_post()
Definition: Kernel.cpp:456
virtual float64_t compute_optimized(int32_t vector_idx)
Definition: Kernel.cpp:353
EOptimizationType get_optimization_type()
Definition: Kernel.h:623
void unset_property(EKernelProperty p)
Definition: Kernel.h:772
void list_kernel()
Definition: Kernel.cpp:221
float64_t get_combined_kernel_weight()
Definition: Kernel.h:684
virtual SGVector< float64_t > row_wise_sum_symmetric_block(index_t block_begin, index_t block_size, bool no_diag=true)
Definition: Kernel.cpp:636
The MultitaskKernel allows Multitask Learning via a modified kernel function.
Normalize the kernel by a constant obtained from the first element of the kernel matrix, i.e. .
Normalize the kernel by adding a constant term to its diagonal. This aids kernels to become positive ...
int32_t num_lhs
number of feature vectors on left hand side
Definition: Kernel.h:883
The class Kernel Normalizer defines a function to post-process kernel values.
ZeroMeanCenterKernelNormalizer centers the kernel in feature space.
virtual int32_t get_num_vec_rhs()
Definition: Kernel.h:522
virtual void set_subkernel_weights(SGVector< float64_t > weights)
Definition: Kernel.cpp:400
virtual bool init_normalizer()
Definition: Kernel.cpp:153
bool optimization_initialized
Definition: Kernel.h:891
float float32_t
Definition: common.h:49
EFeatureType
shogun feature type
Definition: FeatureTypes.h:19
EOptimizationType opt_type
Definition: Kernel.h:895
void load(CFile *loader)
Definition: Kernel.cpp:165
CFeatures * rhs
feature vectors to occur on right hand side
Definition: Kernel.h:877
static CKernel * obtain_from_generic(CSGObject *kernel)
Definition: Kernel.cpp:409
Base-class for parameterized Kernel Normalizers.
SqrtDiagKernelNormalizer divides by the Square Root of the product of the diagonal elements...
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
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)
Definition: Kernel.cpp:359
EOptimizationType
Definition: Kernel.h:47
bool lhs_equals_rhs
lhs
Definition: Kernel.h:880
Normalize the kernel by either a constant or the average value of the diagonal elements (depending on...
virtual EKernelType get_kernel_type()=0
virtual bool init_optimization(int32_t count, int32_t *IDX, float64_t *weights)
Definition: Kernel.cpp:340
CFeatures * lhs
feature vectors to occur on left hand side
Definition: Kernel.h:875
The class Features is the base class of all feature objects.
Definition: Features.h:68
static T min(T a, T b)
Definition: Math.h:157
virtual void save_serializable_pre()
Definition: Kernel.cpp:448
virtual SGMatrix< float64_t > get_parameter_gradient(const TParameter *param, index_t index=-1)
Definition: Kernel.h:732
SGVector< float64_t > get_kernel_diagonal(SGVector< float64_t > preallocated=SGVector< float64_t >())
Definition: Kernel.h:227
virtual void remove_lhs()
Definition: Kernel.cpp:196
virtual int32_t get_num_subkernels()
Definition: Kernel.cpp:376
bool init_optimization_svm(CSVM *svm)
Definition: Kernel.cpp:423
A generic Support Vector Machine Interface.
Definition: SVM.h:49
The Kernel base class.
Definition: Kernel.h:155
int32_t get_cache_size()
Definition: Kernel.h:593
CKernelNormalizer * normalizer
Definition: Kernel.h:902
virtual SGVector< float64_t > get_kernel_row(int32_t i)
Definition: Kernel.h:276
SGVector< T > get_diagonal_vector() const
Definition: SGMatrix.cpp:1099
virtual float64_t normalize(float64_t value, int32_t idx_lhs, int32_t idx_rhs)=0
static float32_t sqrt(float32_t x)
Definition: Math.h:459
virtual bool has_features()
Definition: Kernel.h:531
virtual ~CKernel()
Definition: Kernel.cpp:70
virtual void add_to_normal(int32_t vector_idx, float64_t weight)
Definition: Kernel.cpp:366
virtual SGVector< float64_t > get_parameter_gradient_diagonal(const TParameter *param, index_t index=-1)
Definition: Kernel.h:746
float64_t KERNELCACHE_ELEM
Definition: Kernel.h:32
friend class CStreamingKernel
Definition: Kernel.h:170
void set_property(EKernelProperty p)
Definition: Kernel.h:763
VarianceKernelNormalizer divides by the ``variance''.
virtual EFeatureClass get_feature_class()=0
CFeatures * get_lhs()
Definition: Kernel.h:501

SHOGUN Machine Learning Toolbox - Documentation