SHOGUN  6.0.0
kernel/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,
64  K_SALZBERG = 41,
72  K_POLYMATCH = 100,
73  K_ALIGNMENT = 110,
78  K_COMBINED = 140,
79  K_AUC = 150,
80  K_CUSTOM = 160,
81  K_SIGMOID = 170,
82  K_CHI2 = 180,
83  K_DIAG = 190,
84  K_CONST = 200,
85  K_DISTANCE = 220,
88  K_OLIGO = 250,
89  K_MATCHWORD = 260,
90  K_TPPK = 270,
94  K_WAVELET = 310,
95  K_WAVE = 320,
96  K_CAUCHY = 330,
97  K_TSTUDENT = 340,
101  K_SPHERICAL = 380,
102  K_SPLINE = 390,
103  K_ANOVA = 400,
104  K_POWER = 410,
105  K_LOG = 420,
106  K_CIRCULAR = 430,
109  K_BESSEL = 460,
111  K_DIRECTOR = 480,
112  K_PRODUCT = 490,
116  K_STREAMING = 520,
118 };
119 
122 {
123  KP_NONE = 0,
124  KP_LINADD = 1, // Kernels that can be optimized via doing normal updates w + dw
125  KP_KERNCOMBINATION = 2, // Kernels that are infact a linear combination of subkernels K=\sum_i b_i*K_i
126  KP_BATCHEVALUATION = 4 // Kernels that can on the fly generate normals in linadd and more quickly/memory efficient process batches instead of single examples
127 };
128 
129 class CSVM;
130 
156 class CKernel : public CSGObject
157 {
168  friend class CDiceKernelNormalizer;
170 
171  friend class CStreamingKernel;
172 
173  public:
174 
178  CKernel();
179 
180 
185  CKernel(int32_t size);
186 
193  CKernel(CFeatures* l, CFeatures* r, int32_t size);
194 
195  virtual ~CKernel();
196 
204  inline float64_t kernel(int32_t idx_a, int32_t idx_b)
205  {
206  REQUIRE(idx_a>=0 && idx_b>=0 && idx_a<num_lhs && idx_b<num_rhs,
207  "%s::kernel(): index out of Range: idx_a=%d/%d idx_b=%d/%d\n",
208  get_name(), idx_a,num_lhs, idx_b,num_rhs);
209 
210  return normalizer->normalize(compute(idx_a, idx_b), idx_a, idx_b);
211  }
212 
218  {
219  return get_kernel_matrix<float64_t>();
220  }
221 
229  preallocated=SGVector<float64_t>())
230  {
231  REQUIRE(lhs, "CKernel::get_kernel_diagonal(): Left-handside "
232  "features missing!\n");
233 
234  REQUIRE(rhs, "CKernel::get_kernel_diagonal(): Right-handside "
235  "features missing!\n");
236 
237  int32_t length=CMath::min(lhs->get_num_vectors(),rhs->get_num_vectors());
238 
239  /* allocate space if necessary */
240  if (!preallocated.vector)
241  preallocated=SGVector<float64_t>(length);
242  else
243  {
244  REQUIRE(preallocated.vlen==length,
245  "%s::get_kernel_diagonal(): Preallocated vector has"
246  " wrong size!\n", get_name());
247  }
248 
249  for (index_t i=0; i<preallocated.vlen; ++i)
250  preallocated[i]=kernel(i, i);
251 
252  return preallocated;
253  }
254 
261  {
262 
264 
265  for (int32_t i=0; i!=num_rhs; i++)
266  col[i] = kernel(i,j);
267 
268  return col;
269  }
270 
271 
278  {
280 
281  for (int32_t j=0; j!=num_lhs; j++)
282  row[j] = kernel(i,j);
283 
284  return row;
285  }
286 
310  virtual float64_t sum_symmetric_block(index_t block_begin,
311  index_t block_size, bool no_diag=true);
312 
341  virtual float64_t sum_block(index_t block_begin_row,
342  index_t block_begin_col, index_t block_size_row,
343  index_t block_size_col, bool no_diag=false);
344 
369  block_begin, index_t block_size, bool no_diag=true);
370 
401  index_t block_begin, index_t block_size, bool no_diag=true);
402 
439  index_t block_begin_row, index_t block_begin_col,
440  index_t block_size_row, index_t block_size_col,
441  bool no_diag=false);
442 
447  template <class T> SGMatrix<T> get_kernel_matrix();
448 
459  virtual bool init(CFeatures* lhs, CFeatures* rhs);
460 
466 
472 
476  virtual bool init_normalizer();
477 
484  virtual void cleanup();
485 
490  void load(CFile* loader);
491 
496  void save(CFile* writer);
497 
502  inline CFeatures* get_lhs() { SG_REF(lhs); return lhs; }
503 
508  inline CFeatures* get_rhs() { SG_REF(rhs); return rhs; }
509 
514  virtual int32_t get_num_vec_lhs()
515  {
516  return num_lhs;
517  }
518 
523  virtual int32_t get_num_vec_rhs()
524  {
525  return num_rhs;
526  }
527 
532  virtual bool has_features()
533  {
534  return lhs && rhs;
535  }
536 
541  inline bool get_lhs_equals_rhs()
542  {
543  return lhs_equals_rhs;
544  }
545 
547  virtual void remove_lhs_and_rhs();
548 
550  virtual void remove_lhs();
551 
553  virtual void remove_rhs();
554 
562  virtual EKernelType get_kernel_type()=0 ;
563 
570  virtual EFeatureType get_feature_type()=0;
571 
578  virtual EFeatureClass get_feature_class()=0;
579 
584  inline void set_cache_size(int32_t size)
585  {
586  cache_size = size;
587 
588  }
589 
594  inline int32_t get_cache_size() { return cache_size; }
595 
596 
597 
599  void list_kernel();
600 
606  inline bool has_property(EKernelProperty p) { return (properties & p) != 0; }
607 
611  virtual void clear_normal();
612 
618  virtual void add_to_normal(int32_t vector_idx, float64_t weight);
619 
625 
631 
637 
645  virtual bool init_optimization(
646  int32_t count, int32_t *IDX, float64_t *weights);
647 
652  virtual bool delete_optimization();
653 
659  bool init_optimization_svm(CSVM * svm) ;
660 
666  virtual float64_t compute_optimized(int32_t vector_idx);
667 
676  virtual void compute_batch(
677  int32_t num_vec, int32_t* vec_idx, float64_t* target,
678  int32_t num_suppvec, int32_t* IDX, float64_t* alphas,
679  float64_t factor=1.0);
680 
686 
692 
697  virtual int32_t get_num_subkernels();
698 
704  virtual void compute_by_subkernel(
705  int32_t vector_idx, float64_t * subkernel_contrib);
706 
712  virtual const float64_t* get_subkernel_weights(int32_t& num_weights);
713 
719 
724  virtual void set_subkernel_weights(SGVector<float64_t> weights);
725 
734  const TParameter* param, index_t index=-1)
735  {
736  SG_ERROR("Can't compute derivative wrt %s parameter\n", param->m_name)
737  return SGMatrix<float64_t>();
738  }
739 
748  const TParameter* param, index_t index=-1)
749  {
750  return get_parameter_gradient(param,index).get_diagonal_vector();
751  }
752 
759  protected:
765  {
766  properties |= p;
767  }
768 
774  {
775  properties &= (properties | p) ^ p;
776  }
777 
782  inline void set_is_initialized(bool p_init) { optimization_initialized=p_init; }
783 
794  virtual float64_t compute(int32_t x, int32_t y)=0;
795 
802  int32_t compute_row_start(int64_t offs, int32_t n, bool symmetric)
803  {
804  int32_t i_start;
805 
806  if (symmetric)
807  i_start=(int32_t) CMath::floor(n-CMath::sqrt(CMath::sq((float64_t) n)-offs));
808  else
809  i_start=(int32_t) (offs/int64_t(n));
810 
811  return i_start;
812  }
813 
818  template <class T> static void* get_kernel_matrix_helper(void* p);
819 
828  virtual void load_serializable_post() throw (ShogunException);
829 
838  virtual void save_serializable_pre() throw (ShogunException);
839 
848  virtual void save_serializable_post() throw (ShogunException);
849 
854  virtual void register_params();
855 
856  private:
859  void init();
860 
861 
862 
864 
865  protected:
867  int32_t cache_size;
868 
869 
870 
873  KERNELCACHE_ELEM* kernel_matrix;
874 
879 
882 
884  int32_t num_lhs;
886  int32_t num_rhs;
887 
890 
897 
899  uint64_t properties;
900 
904 };
905 
906 }
907 #endif /* _KERNEL_H__ */
virtual void clear_normal()
Definition: Kernel.cpp:370
virtual const char * get_name() const =0
virtual void load_serializable_post()
Definition: Kernel.cpp:440
virtual bool init(CFeatures *lhs, CFeatures *rhs)
Definition: Kernel.cpp:81
int32_t compute_row_start(int64_t offs, int32_t n, bool symmetric)
The MultitaskKernel allows Multitask Learning via a modified kernel function.
virtual void cleanup()
Definition: Kernel.cpp:156
virtual void compute_by_subkernel(int32_t vector_idx, float64_t *subkernel_contrib)
Definition: Kernel.cpp:380
virtual float64_t compute(int32_t x, int32_t y)=0
int32_t index_t
Definition: common.h:72
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
static void * get_kernel_matrix_helper(void *p)
Definition: Kernel.cpp:799
Class ShogunException defines an exception which is thrown whenever an error inside of shogun occurs...
virtual bool set_normalizer(CKernelNormalizer *normalizer)
Definition: Kernel.cpp:133
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:587
static T sq(T x)
Definition: Math.h:445
bool get_lhs_equals_rhs()
parameter struct
virtual int32_t get_num_vectors() const =0
CFeatures * get_rhs()
#define SG_ERROR(...)
Definition: SGIO.h:128
#define REQUIRE(x,...)
Definition: SGIO.h:205
void set_is_initialized(bool p_init)
virtual bool delete_optimization()
Definition: Kernel.cpp:346
int64_t KERNELCACHE_IDX
Definition: kernel/Kernel.h:43
void set_cache_size(int32_t size)
float64_t kernel(int32_t idx_a, int32_t idx_b)
virtual void set_optimization_type(EOptimizationType t)
uint64_t properties
virtual void remove_rhs()
takes all necessary steps if the rhs is removed from kernel
Definition: Kernel.cpp:206
TanimotoKernelNormalizer performs kernel normalization inspired by the Tanimoto coefficient (see http...
virtual int32_t get_num_vec_lhs()
SGMatrix< float64_t > get_kernel_matrix()
#define SG_REF(x)
Definition: SGObject.h:52
static float64_t floor(float64_t d)
Definition: Math.h:402
int32_t cache_size
cache_size in MB
EFeatureClass
shogun feature class
Definition: FeatureTypes.h:38
bool get_is_initialized()
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:686
float64_t combined_kernel_weight
virtual void register_params()
Definition: Kernel.cpp:463
void save(CFile *writer)
Definition: Kernel.cpp:169
virtual SGVector< float64_t > get_kernel_col(int32_t j)
virtual void remove_lhs_and_rhs()
Definition: Kernel.cpp:177
bool has_property(EKernelProperty p)
virtual CKernelNormalizer * get_normalizer()
Definition: Kernel.cpp:145
SGVector< T > get_diagonal_vector() const
Definition: SGMatrix.cpp:1200
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:125
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:745
virtual float64_t sum_symmetric_block(index_t block_begin, index_t block_size, bool no_diag=true)
Definition: Kernel.cpp:536
virtual SGVector< float64_t > get_subkernel_weights()
Definition: Kernel.cpp:392
double float64_t
Definition: common.h:60
virtual EFeatureType get_feature_type()=0
void set_combined_kernel_weight(float64_t nw)
KERNELCACHE_ELEM * kernel_matrix
A File access base class.
Definition: File.h:34
virtual void save_serializable_post()
Definition: Kernel.cpp:455
virtual float64_t compute_optimized(int32_t vector_idx)
Definition: Kernel.cpp:352
EOptimizationType get_optimization_type()
void unset_property(EKernelProperty p)
void list_kernel()
Definition: Kernel.cpp:219
float64_t get_combined_kernel_weight()
virtual SGVector< float64_t > row_wise_sum_symmetric_block(index_t block_begin, index_t block_size, bool no_diag=true)
Definition: Kernel.cpp:632
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
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()
virtual void set_subkernel_weights(SGVector< float64_t > weights)
Definition: Kernel.cpp:399
virtual bool init_normalizer()
Definition: Kernel.cpp:151
bool optimization_initialized
float float32_t
Definition: common.h:59
EFeatureType
shogun feature type
Definition: FeatureTypes.h:19
EOptimizationType opt_type
void load(CFile *loader)
Definition: Kernel.cpp:163
CFeatures * rhs
feature vectors to occur on right hand side
static CKernel * obtain_from_generic(CSGObject *kernel)
Definition: Kernel.cpp:408
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:358
EOptimizationType
Definition: kernel/Kernel.h:47
bool lhs_equals_rhs
lhs
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:339
CFeatures * lhs
feature vectors to occur on left hand side
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:153
virtual void save_serializable_pre()
Definition: Kernel.cpp:447
virtual SGMatrix< float64_t > get_parameter_gradient(const TParameter *param, index_t index=-1)
SGVector< float64_t > get_kernel_diagonal(SGVector< float64_t > preallocated=SGVector< float64_t >())
virtual void remove_lhs()
Definition: Kernel.cpp:194
virtual int32_t get_num_subkernels()
Definition: Kernel.cpp:375
bool init_optimization_svm(CSVM *svm)
Definition: Kernel.cpp:422
A generic Support Vector Machine Interface.
Definition: SVM.h:49
The Kernel base class.
int32_t get_cache_size()
CKernelNormalizer * normalizer
virtual SGVector< float64_t > get_kernel_row(int32_t i)
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:454
virtual bool has_features()
virtual ~CKernel()
Definition: Kernel.cpp:68
virtual void add_to_normal(int32_t vector_idx, float64_t weight)
Definition: Kernel.cpp:365
virtual SGVector< float64_t > get_parameter_gradient_diagonal(const TParameter *param, index_t index=-1)
float64_t KERNELCACHE_ELEM
Definition: kernel/Kernel.h:32
friend class CStreamingKernel
void set_property(EKernelProperty p)
VarianceKernelNormalizer divides by the ``variance&#39;&#39;.
virtual EFeatureClass get_feature_class()=0
CFeatures * get_lhs()

SHOGUN Machine Learning Toolbox - Documentation