SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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/common.h>
16 #include <shogun/lib/Signal.h>
17 #include <shogun/io/SGIO.h>
18 #include <shogun/io/File.h>
21 #include <shogun/base/SGObject.h>
24 
25 namespace shogun
26 {
27  class CFile;
28  class CFeatures;
30 
31 #ifdef USE_SHORTREAL_KERNELCACHE
32 
34 #else
35 
37 #endif
38 
40 typedef int64_t KERNELCACHE_IDX;
41 
42 
45 {
48 };
49 
52 {
53  K_UNKNOWN = 0,
54  K_LINEAR = 10,
55  K_POLY = 20,
56  K_GAUSSIAN = 30,
60  K_SALZBERG = 41,
68  K_POLYMATCH = 100,
69  K_ALIGNMENT = 110,
74  K_COMBINED = 140,
75  K_AUC = 150,
76  K_CUSTOM = 160,
77  K_SIGMOID = 170,
78  K_CHI2 = 180,
79  K_DIAG = 190,
80  K_CONST = 200,
81  K_DISTANCE = 220,
84  K_OLIGO = 250,
85  K_MATCHWORD = 260,
86  K_TPPK = 270,
90  K_WAVELET = 310,
91  K_WAVE = 320,
92  K_CAUCHY = 330,
93  K_TSTUDENT = 340,
97  K_SPHERICAL = 380,
98  K_SPLINE = 390,
99  K_ANOVA = 400,
100  K_POWER = 410,
101  K_LOG = 420,
102  K_CIRCULAR = 430,
105  K_BESSEL = 460,
107  K_DIRECTOR = 480,
108  K_PRODUCT = 490,
109  K_LINEARARD = 500,
112 };
113 
116 {
117  KP_NONE = 0,
118  KP_LINADD = 1, // Kernels that can be optimized via doing normal updates w + dw
119  KP_KERNCOMBINATION = 2, // Kernels that are infact a linear combination of subkernels K=\sum_i b_i*K_i
120  KP_BATCHEVALUATION = 4 // Kernels that can on the fly generate normals in linadd and more quickly/memory efficient process batches instead of single examples
121 };
122 
123 class CSVM;
124 
150 class CKernel : public CSGObject
151 {
162  friend class CDiceKernelNormalizer;
164 
165  friend class CStreamingKernel;
166 
167  public:
168 
172  CKernel();
173 
174 
179  CKernel(int32_t size);
180 
187  CKernel(CFeatures* l, CFeatures* r, int32_t size);
188 
189  virtual ~CKernel();
190 
198  inline float64_t kernel(int32_t idx_a, int32_t idx_b)
199  {
200  REQUIRE(idx_a>=0 && idx_b>=0 && idx_a<num_lhs && idx_b<num_rhs,
201  "%s::kernel(): index out of Range: idx_a=%d/%d idx_b=%d/%d\n",
202  get_name(), idx_a,num_lhs, idx_b,num_rhs);
203 
204  return normalizer->normalize(compute(idx_a, idx_b), idx_a, idx_b);
205  }
206 
212  {
213  return get_kernel_matrix<float64_t>();
214  }
215 
223  preallocated=SGVector<float64_t>())
224  {
225  REQUIRE(lhs, "CKernel::get_kernel_diagonal(): Left-handside "
226  "features missing!\n");
227 
228  REQUIRE(rhs, "CKernel::get_kernel_diagonal(): Right-handside "
229  "features missing!\n");
230 
232  "CKernel::get_kernel_diagonal(): Left- and right-"
233  "handside features must be equal sized\n");
234 
235  /* allocate space if necessary */
236  if (!preallocated.vector)
237  preallocated=SGVector<float64_t>(lhs->get_num_vectors());
238  else
239  {
240  REQUIRE(preallocated.vlen==lhs->get_num_vectors(),
241  "%s::get_kernel_diagonal(): Preallocated vector has"
242  " wrong size!\n", get_name());
243  }
244 
245  for (index_t i=0; i<preallocated.vlen; ++i)
246  preallocated[i]=kernel(i, i);
247 
248  return preallocated;
249  }
250 
257  {
258 
260 
261  for (int32_t i=0; i!=num_rhs; i++)
262  col[i] = kernel(i,j);
263 
264  return col;
265  }
266 
267 
274  {
276 
277  for (int32_t j=0; j!=num_lhs; j++)
278  row[j] = kernel(i,j);
279 
280  return row;
281  }
282 
287  template <class T> SGMatrix<T> get_kernel_matrix();
288 
289 
300  virtual bool init(CFeatures* lhs, CFeatures* rhs);
301 
307 
313 
317  virtual bool init_normalizer();
318 
325  virtual void cleanup();
326 
331  void load(CFile* loader);
332 
337  void save(CFile* writer);
338 
343  inline CFeatures* get_lhs() { SG_REF(lhs); return lhs; }
344 
349  inline CFeatures* get_rhs() { SG_REF(rhs); return rhs; }
350 
355  virtual int32_t get_num_vec_lhs()
356  {
357  return num_lhs;
358  }
359 
364  virtual int32_t get_num_vec_rhs()
365  {
366  return num_rhs;
367  }
368 
373  virtual bool has_features()
374  {
375  return lhs && rhs;
376  }
377 
382  inline bool get_lhs_equals_rhs()
383  {
384  return lhs_equals_rhs;
385  }
386 
388  virtual void remove_lhs_and_rhs();
389 
391  virtual void remove_lhs();
392 
394  virtual void remove_rhs();
395 
403  virtual EKernelType get_kernel_type()=0 ;
404 
411  virtual EFeatureType get_feature_type()=0;
412 
419  virtual EFeatureClass get_feature_class()=0;
420 
425  inline void set_cache_size(int32_t size)
426  {
427  cache_size = size;
428 
429  }
430 
435  inline int32_t get_cache_size() { return cache_size; }
436 
437 
438 
440  void list_kernel();
441 
447  inline bool has_property(EKernelProperty p) { return (properties & p) != 0; }
448 
452  virtual void clear_normal();
453 
459  virtual void add_to_normal(int32_t vector_idx, float64_t weight);
460 
466 
472 
478 
486  virtual bool init_optimization(
487  int32_t count, int32_t *IDX, float64_t *weights);
488 
493  virtual bool delete_optimization();
494 
500  bool init_optimization_svm(CSVM * svm) ;
501 
507  virtual float64_t compute_optimized(int32_t vector_idx);
508 
517  virtual void compute_batch(
518  int32_t num_vec, int32_t* vec_idx, float64_t* target,
519  int32_t num_suppvec, int32_t* IDX, float64_t* alphas,
520  float64_t factor=1.0);
521 
527 
533 
538  virtual int32_t get_num_subkernels();
539 
545  virtual void compute_by_subkernel(
546  int32_t vector_idx, float64_t * subkernel_contrib);
547 
553  virtual const float64_t* get_subkernel_weights(int32_t& num_weights);
554 
560 
565  virtual void set_subkernel_weights(SGVector<float64_t> weights);
566 
575  const TParameter* param, index_t index=-1)
576  {
577  SG_ERROR("Can't compute derivative wrt %s parameter\n", param->m_name)
578  return SGMatrix<float64_t>();
579  }
580 
587  protected:
593  {
594  properties |= p;
595  }
596 
602  {
603  properties &= (properties | p) ^ p;
604  }
605 
610  inline void set_is_initialized(bool p_init) { optimization_initialized=p_init; }
611 
622  virtual float64_t compute(int32_t x, int32_t y)=0;
623 
630  int32_t compute_row_start(int64_t offs, int32_t n, bool symmetric)
631  {
632  int32_t i_start;
633 
634  if (symmetric)
635  i_start=(int32_t) CMath::floor(n-CMath::sqrt(CMath::sq((float64_t) n)-offs));
636  else
637  i_start=(int32_t) (offs/int64_t(n));
638 
639  return i_start;
640  }
641 
646  template <class T> static void* get_kernel_matrix_helper(void* p);
647 
656  virtual void load_serializable_post() throw (ShogunException);
657 
666  virtual void save_serializable_pre() throw (ShogunException);
667 
676  virtual void save_serializable_post() throw (ShogunException);
677 
682  virtual void register_params();
683 
684  private:
687  void init();
688 
689 
690 
692 
693  protected:
695  int32_t cache_size;
696 
697 
698 
702 
707 
710 
712  int32_t num_lhs;
714  int32_t num_rhs;
715 
718 
725 
727  uint64_t properties;
728 
732 };
733 
734 }
735 #endif /* _KERNEL_H__ */

SHOGUN Machine Learning Toolbox - Documentation