SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
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,
112  K_LINEARARD = 500,
115 };
116 
119 {
120  KP_NONE = 0,
121  KP_LINADD = 1, // Kernels that can be optimized via doing normal updates w + dw
122  KP_KERNCOMBINATION = 2, // Kernels that are infact a linear combination of subkernels K=\sum_i b_i*K_i
123  KP_BATCHEVALUATION = 4 // Kernels that can on the fly generate normals in linadd and more quickly/memory efficient process batches instead of single examples
124 };
125 
126 class CSVM;
127 
153 class CKernel : public CSGObject
154 {
165  friend class CDiceKernelNormalizer;
167 
168  friend class CStreamingKernel;
169 
170  public:
171 
175  CKernel();
176 
177 
182  CKernel(int32_t size);
183 
190  CKernel(CFeatures* l, CFeatures* r, int32_t size);
191 
192  virtual ~CKernel();
193 
201  inline float64_t kernel(int32_t idx_a, int32_t idx_b)
202  {
203  REQUIRE(idx_a>=0 && idx_b>=0 && idx_a<num_lhs && idx_b<num_rhs,
204  "%s::kernel(): index out of Range: idx_a=%d/%d idx_b=%d/%d\n",
205  get_name(), idx_a,num_lhs, idx_b,num_rhs);
206 
207  return normalizer->normalize(compute(idx_a, idx_b), idx_a, idx_b);
208  }
209 
215  {
216  return get_kernel_matrix<float64_t>();
217  }
218 
226  preallocated=SGVector<float64_t>())
227  {
228  REQUIRE(lhs, "CKernel::get_kernel_diagonal(): Left-handside "
229  "features missing!\n");
230 
231  REQUIRE(rhs, "CKernel::get_kernel_diagonal(): Right-handside "
232  "features missing!\n");
233 
235  "CKernel::get_kernel_diagonal(): Left- and right-"
236  "handside features must be equal sized\n");
237 
238  /* allocate space if necessary */
239  if (!preallocated.vector)
240  preallocated=SGVector<float64_t>(lhs->get_num_vectors());
241  else
242  {
243  REQUIRE(preallocated.vlen==lhs->get_num_vectors(),
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 
745  protected:
751  {
752  properties |= p;
753  }
754 
760  {
761  properties &= (properties | p) ^ p;
762  }
763 
768  inline void set_is_initialized(bool p_init) { optimization_initialized=p_init; }
769 
780  virtual float64_t compute(int32_t x, int32_t y)=0;
781 
788  int32_t compute_row_start(int64_t offs, int32_t n, bool symmetric)
789  {
790  int32_t i_start;
791 
792  if (symmetric)
793  i_start=(int32_t) CMath::floor(n-CMath::sqrt(CMath::sq((float64_t) n)-offs));
794  else
795  i_start=(int32_t) (offs/int64_t(n));
796 
797  return i_start;
798  }
799 
804  template <class T> static void* get_kernel_matrix_helper(void* p);
805 
814  virtual void load_serializable_post() throw (ShogunException);
815 
824  virtual void save_serializable_pre() throw (ShogunException);
825 
834  virtual void save_serializable_post() throw (ShogunException);
835 
840  virtual void register_params();
841 
842  private:
845  void init();
846 
847 
848 
850 
851  protected:
853  int32_t cache_size;
854 
855 
856 
859  KERNELCACHE_ELEM* kernel_matrix;
860 
865 
868 
870  int32_t num_lhs;
872  int32_t num_rhs;
873 
876 
883 
885  uint64_t properties;
886 
890 };
891 
892 }
893 #endif /* _KERNEL_H__ */
virtual void clear_normal()
Definition: Kernel.cpp:357
virtual const char * get_name() const =0
virtual void load_serializable_post()
Definition: Kernel.cpp:427
int32_t compute_row_start(int64_t offs, int32_t n, bool symmetric)
Definition: Kernel.h:788
The MultitaskKernel allows Multitask Learning via a modified kernel function.
virtual void cleanup()
Definition: Kernel.cpp:147
virtual void compute_by_subkernel(int32_t vector_idx, float64_t *subkernel_contrib)
Definition: Kernel.cpp:367
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:872
static void * get_kernel_matrix_helper(void *p)
Definition: Kernel.cpp:789
Class ShogunException defines an exception which is thrown whenever an error inside of shogun occurs...
virtual bool set_normalizer(CKernelNormalizer *normalizer)
Definition: Kernel.cpp:124
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:576
static T sq(T x)
x^2
Definition: Math.h:395
bool get_lhs_equals_rhs()
Definition: Kernel.h:540
parameter struct
Definition: Parameter.h:32
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:768
virtual bool delete_optimization()
Definition: Kernel.cpp:333
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:201
virtual void set_optimization_type(EOptimizationType t)
Definition: Kernel.h:629
uint64_t properties
Definition: Kernel.h:885
virtual void remove_rhs()
takes all necessary steps if the rhs is removed from kernel
Definition: Kernel.cpp:197
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:214
#define SG_REF(x)
Definition: SGObject.h:51
static float64_t floor(float64_t d)
Definition: Math.h:365
int32_t cache_size
cache_size in MB
Definition: Kernel.h:853
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:676
float64_t combined_kernel_weight
Definition: Kernel.h:875
virtual void register_params()
Definition: Kernel.cpp:450
void save(CFile *writer)
Definition: Kernel.cpp:160
virtual SGVector< float64_t > get_kernel_col(int32_t j)
Definition: Kernel.h:259
virtual void remove_lhs_and_rhs()
Definition: Kernel.cpp:168
bool has_property(EKernelProperty p)
Definition: Kernel.h:605
virtual CKernelNormalizer * get_normalizer()
Definition: Kernel.cpp:136
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:735
EKernelProperty
Definition: Kernel.h:118
virtual float64_t sum_symmetric_block(index_t block_begin, index_t block_size, bool no_diag=true)
Definition: Kernel.cpp:523
virtual SGVector< float64_t > get_subkernel_weights()
Definition: Kernel.cpp:379
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:859
A File access base class.
Definition: File.h:34
virtual void save_serializable_post()
Definition: Kernel.cpp:442
virtual float64_t compute_optimized(int32_t vector_idx)
Definition: Kernel.cpp:339
EOptimizationType get_optimization_type()
Definition: Kernel.h:623
void unset_property(EKernelProperty p)
Definition: Kernel.h:759
void list_kernel()
Definition: Kernel.cpp:210
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:622
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:870
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:386
virtual bool init_normalizer()
Definition: Kernel.cpp:142
bool optimization_initialized
Definition: Kernel.h:878
float float32_t
Definition: common.h:49
EFeatureType
shogun feature type
Definition: FeatureTypes.h:19
EOptimizationType opt_type
Definition: Kernel.h:882
void load(CFile *loader)
Definition: Kernel.cpp:154
CFeatures * rhs
feature vectors to occur on right hand side
Definition: Kernel.h:864
static CKernel * obtain_from_generic(CSGObject *kernel)
Definition: Kernel.cpp:395
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:345
EOptimizationType
Definition: Kernel.h:47
bool lhs_equals_rhs
lhs
Definition: Kernel.h:867
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:326
CFeatures * lhs
feature vectors to occur on left hand side
Definition: Kernel.h:862
The class Features is the base class of all feature objects.
Definition: Features.h:68
virtual void save_serializable_pre()
Definition: Kernel.cpp:434
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:225
virtual void remove_lhs()
Definition: Kernel.cpp:185
virtual int32_t get_num_subkernels()
Definition: Kernel.cpp:362
bool init_optimization_svm(CSVM *svm)
Definition: Kernel.cpp:409
A generic Support Vector Machine Interface.
Definition: SVM.h:49
The Kernel base class.
Definition: Kernel.h:153
int32_t get_cache_size()
Definition: Kernel.h:593
CKernelNormalizer * normalizer
Definition: Kernel.h:889
virtual SGVector< float64_t > get_kernel_row(int32_t i)
Definition: Kernel.h:276
virtual float64_t normalize(float64_t value, int32_t idx_lhs, int32_t idx_rhs)=0
static float32_t sqrt(float32_t x)
x^0.5
Definition: Math.h:401
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:352
float64_t KERNELCACHE_ELEM
Definition: Kernel.h:32
friend class CStreamingKernel
Definition: Kernel.h:168
void set_property(EKernelProperty p)
Definition: Kernel.h:750
VarianceKernelNormalizer divides by the ``variance''.
virtual EFeatureClass get_feature_class()=0
CFeatures * get_lhs()
Definition: Kernel.h:501

SHOGUN Machine Learning Toolbox - Documentation