SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Kernel.cpp
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 #include <shogun/lib/config.h>
13 #include <shogun/lib/common.h>
14 #include <shogun/io/SGIO.h>
15 #include <shogun/io/File.h>
16 #include <shogun/lib/Time.h>
17 #include <shogun/lib/Signal.h>
18 
19 #include <shogun/base/Parallel.h>
20 
21 #include <shogun/kernel/Kernel.h>
24 #include <shogun/base/Parameter.h>
25 
27 
28 #include <string.h>
29 #include <unistd.h>
30 #include <math.h>
31 
32 #ifdef HAVE_PTHREAD
33 #include <pthread.h>
34 #endif
35 
36 using namespace shogun;
37 
39 {
40  init();
42 }
43 
44 CKernel::CKernel(int32_t size) : CSGObject()
45 {
46  init();
47 
48  if (size<10)
49  size=10;
50 
51  cache_size=size;
53 }
54 
55 
56 CKernel::CKernel(CFeatures* p_lhs, CFeatures* p_rhs, int32_t size) : CSGObject()
57 {
58  init();
59 
60  if (size<10)
61  size=10;
62 
63  cache_size=size;
64 
66  init(p_lhs, p_rhs);
68 }
69 
71 {
72  if (get_is_initialized())
73  SG_ERROR("Kernel still initialized on destruction.\n");
74 
77 
78  SG_INFO("Kernel deleted (%p).\n", this);
79 }
80 
81 
82 
83 bool CKernel::init(CFeatures* l, CFeatures* r)
84 {
85  //make sure features were indeed supplied
86  ASSERT(l);
87  ASSERT(r);
88 
89  //make sure features are compatible
92 
93  //remove references to previous features
95 
96  //increase reference counts
97  SG_REF(l);
98  if (l==r)
99  lhs_equals_rhs=true;
100  else // l!=r
101  SG_REF(r);
102 
103  lhs=l;
104  rhs=r;
105 
108 
111 
112  return true;
113 }
114 
116 {
117  SG_REF(n);
118  if (lhs && rhs)
119  n->init(this);
120 
122  normalizer=n;
123 
124  return (normalizer!=NULL);
125 }
126 
128 {
130  return normalizer;
131 }
132 
134 {
135  return normalizer->init(this);
136 }
137 
139 {
141 }
142 
143 
144 
145 void CKernel::load(CFile* loader)
146 {
149 }
150 
151 void CKernel::save(CFile* writer)
152 {
153  SGMatrix<float64_t> k_matrix=get_kernel_matrix<float64_t>();
155  writer->set_matrix(k_matrix.matrix, k_matrix.num_rows, k_matrix.num_cols);
156  SG_FREE(k_matrix.matrix);
158 }
159 
161 {
162  if (rhs!=lhs)
163  SG_UNREF(rhs);
164  rhs = NULL;
165  num_rhs=0;
166 
167  SG_UNREF(lhs);
168  lhs = NULL;
169  num_lhs=0;
170  lhs_equals_rhs=false;
171 
172 
173 }
174 
176 {
177  if (rhs==lhs)
178  rhs=NULL;
179  SG_UNREF(lhs);
180  lhs = NULL;
181  num_lhs=0;
182  lhs_equals_rhs=false;
183 
184 }
185 
188 {
189  if (rhs!=lhs)
190  SG_UNREF(rhs);
191  rhs = NULL;
192  num_rhs=0;
193  lhs_equals_rhs=false;
194 
195 
196 }
197 
198 #define ENUM_CASE(n) case n: SG_INFO(#n " "); break;
199 
201 {
202  SG_INFO( "%p - \"%s\" weight=%1.2f OPT:%s", this, get_name(),
204  get_optimization_type()==FASTBUTMEMHUNGRY ? "FASTBUTMEMHUNGRY" :
205  "SLOWBUTMEMEFFICIENT");
206 
207  switch (get_kernel_type())
208  {
262  }
263 
264  switch (get_feature_class())
265  {
276  ENUM_CASE(C_WD)
281  }
282 
283  switch (get_feature_type())
284  {
299  }
300  SG_INFO( "\n");
301 }
302 #undef ENUM_CASE
303 
305  int32_t count, int32_t *IDX, float64_t * weights)
306 {
307  SG_ERROR( "kernel does not support linadd optimization\n");
308  return false ;
309 }
310 
312 {
313  SG_ERROR( "kernel does not support linadd optimization\n");
314  return false;
315 }
316 
318 {
319  SG_ERROR( "kernel does not support linadd optimization\n");
320  return 0;
321 }
322 
324  int32_t num_vec, int32_t* vec_idx, float64_t* target, int32_t num_suppvec,
325  int32_t* IDX, float64_t* weights, float64_t factor)
326 {
327  SG_ERROR( "kernel does not support batch computation\n");
328 }
329 
330 void CKernel::add_to_normal(int32_t vector_idx, float64_t weight)
331 {
332  SG_ERROR( "kernel does not support linadd optimization, add_to_normal not implemented\n");
333 }
334 
336 {
337  SG_ERROR( "kernel does not support linadd optimization, clear_normal not implemented\n");
338 }
339 
341 {
342  return 1;
343 }
344 
346  int32_t vector_idx, float64_t * subkernel_contrib)
347 {
348  SG_ERROR( "kernel compute_by_subkernel not implemented\n");
349 }
350 
351 const float64_t* CKernel::get_subkernel_weights(int32_t &num_weights)
352 {
353  num_weights=1 ;
354  return &combined_kernel_weight ;
355 }
356 
358 {
359  ASSERT(weights.vector);
360  if (weights.vlen!=1)
361  SG_ERROR( "number of subkernel weights should be one ...\n");
362 
363  combined_kernel_weight = weights.vector[0] ;
364 }
365 
367 {
368  int32_t num_suppvec=svm->get_num_support_vectors();
369  int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
370  float64_t* sv_weight=SG_MALLOC(float64_t, num_suppvec);
371 
372  for (int32_t i=0; i<num_suppvec; i++)
373  {
374  sv_idx[i] = svm->get_support_vector(i);
375  sv_weight[i] = svm->get_alpha(i);
376  }
377  bool ret = init_optimization(num_suppvec, sv_idx, sv_weight);
378 
379  SG_FREE(sv_idx);
380  SG_FREE(sv_weight);
381  return ret;
382 }
383 
385 {
387  if (lhs_equals_rhs)
388  rhs=lhs;
389 }
390 
392 {
394 
395  if (lhs_equals_rhs)
396  rhs=NULL;
397 }
398 
400 {
402 
403  if (lhs_equals_rhs)
404  rhs=lhs;
405 }
406 
408  m_parameters->add(&cache_size, "cache_size",
409  "Cache size in MB.");
410  m_parameters->add((CSGObject**) &lhs, "lhs",
411  "Feature vectors to occur on left hand side.");
412  m_parameters->add((CSGObject**) &rhs, "rhs",
413  "Feature vectors to occur on right hand side.");
414  m_parameters->add(&lhs_equals_rhs, "lhs_equals_rhs",
415  "If features on lhs are the same as on rhs.");
416  m_parameters->add(&num_lhs, "num_lhs",
417  "Number of feature vectors on left hand side.");
418  m_parameters->add(&num_rhs, "num_rhs",
419  "Number of feature vectors on right hand side.");
420  m_parameters->add(&combined_kernel_weight, "combined_kernel_weight",
421  "Combined kernel weight.");
423  "optimization_initialized",
424  "Optimization is initialized.");
425  m_parameters->add((machine_int_t*) &opt_type, "opt_type",
426  "Optimization type.");
427  m_parameters->add(&properties, "properties",
428  "Kernel properties.");
429  m_parameters->add((CSGObject**) &normalizer, "normalizer",
430  "Normalize the kernel.");
431 }
432 
433 
434 void CKernel::init()
435 {
436  cache_size=10;
437  kernel_matrix=NULL;
438  lhs=NULL;
439  rhs=NULL;
440  num_lhs=0;
441  num_rhs=0;
446  normalizer=NULL;
447 
448 
449 
451 }
virtual void clear_normal()
Definition: Kernel.cpp:335
virtual const char * get_name() const =0
virtual void load_serializable_post()
Definition: Kernel.cpp:384
virtual bool init(CFeatures *lhs, CFeatures *rhs)
Definition: Kernel.cpp:83
int32_t get_num_support_vectors()
#define SG_INFO(...)
Definition: SGIO.h:73
virtual void cleanup()
Definition: Kernel.cpp:138
#define SG_RESET_LOCALE
Definition: SGIO.h:67
virtual void set_matrix(const bool *matrix, int32_t num_feat, int32_t num_vec)
Definition: File.cpp:109
virtual void compute_by_subkernel(int32_t vector_idx, float64_t *subkernel_contrib)
Definition: Kernel.cpp:345
virtual EFeatureType get_feature_type()=0
int32_t num_rhs
number of feature vectors on right hand side
Definition: Kernel.h:818
Class ShogunException defines an exception which is thrown whenever an error inside of shogun occurs...
virtual bool set_normalizer(CKernelNormalizer *normalizer)
Definition: Kernel.cpp:115
virtual int32_t get_num_vectors() const =0
virtual void save_serializable_pre()
Definition: SGObject.cpp:427
#define SG_ERROR(...)
Definition: SGIO.h:75
virtual bool delete_optimization()
Definition: Kernel.cpp:311
Parameter * m_parameters
Definition: SGObject.h:297
#define ENUM_CASE(n)
Definition: Kernel.cpp:198
uint64_t properties
Definition: Kernel.h:831
virtual void remove_rhs()
takes all necessary steps if the rhs is removed from kernel
Definition: Kernel.cpp:187
#define SG_REF(x)
Definition: SGObject.h:44
#define SG_SET_LOCALE_C
Definition: SGIO.h:66
int32_t cache_size
cache_size in MB
Definition: Kernel.h:799
bool get_is_initialized()
Definition: Kernel.h:559
float64_t combined_kernel_weight
Definition: Kernel.h:821
virtual void register_params()
Definition: Kernel.cpp:407
void save(CFile *writer)
Definition: Kernel.cpp:151
virtual void remove_lhs_and_rhs()
Definition: Kernel.cpp:160
void add(bool *param, const char *name, const char *description="")
Definition: Parameter.cpp:23
virtual CKernelNormalizer * get_normalizer()
Definition: Kernel.cpp:127
#define ASSERT(x)
Definition: SGIO.h:102
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:76
virtual EFeatureClass get_feature_class()=0
double float64_t
Definition: common.h:56
virtual EFeatureType get_feature_type()=0
KERNELCACHE_ELEM * kernel_matrix
Definition: Kernel.h:805
A File access base class.
Definition: File.h:33
virtual void save_serializable_post()
Definition: Kernel.cpp:399
virtual float64_t compute_optimized(int32_t vector_idx)
Definition: Kernel.cpp:317
EOptimizationType get_optimization_type()
Definition: Kernel.h:547
index_t num_rows
Definition: DataType.h:366
virtual void save_serializable_post()
Definition: SGObject.cpp:432
void list_kernel()
Definition: Kernel.cpp:200
float64_t get_alpha(int32_t idx)
float64_t get_combined_kernel_weight()
Definition: Kernel.h:608
virtual const float64_t * get_subkernel_weights(int32_t &num_weights)
Definition: Kernel.cpp:351
Identity Kernel Normalization, i.e. no normalization is applied.
index_t num_cols
Definition: DataType.h:368
int32_t num_lhs
number of feature vectors on left hand side
Definition: Kernel.h:816
The class Kernel Normalizer defines a function to post-process kernel values.
int32_t get_support_vector(int32_t idx)
#define SG_FREE(ptr)
Definition: memory.h:39
virtual void set_subkernel_weights(SGVector< float64_t > weights)
Definition: Kernel.cpp:357
virtual bool init_normalizer()
Definition: Kernel.cpp:133
bool optimization_initialized
Definition: Kernel.h:824
EOptimizationType opt_type
Definition: Kernel.h:828
void load(CFile *loader)
Definition: Kernel.cpp:145
virtual void load_serializable_post()
Definition: SGObject.cpp:422
CFeatures * rhs
feature vectors to occur on right hand side
Definition: Kernel.h:810
#define SG_UNREF(x)
Definition: SGObject.h:45
virtual bool init(CKernel *k)=0
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:323
bool lhs_equals_rhs
lhs
Definition: Kernel.h:813
int machine_int_t
Definition: common.h:65
virtual EKernelType get_kernel_type()=0
virtual bool init_optimization(int32_t count, int32_t *IDX, float64_t *weights)
Definition: Kernel.cpp:304
CFeatures * lhs
feature vectors to occur on left hand side
Definition: Kernel.h:808
The class Features is the base class of all feature objects.
Definition: Features.h:56
virtual void save_serializable_pre()
Definition: Kernel.cpp:391
virtual void remove_lhs()
Definition: Kernel.cpp:175
virtual int32_t get_num_subkernels()
Definition: Kernel.cpp:340
bool init_optimization_svm(CSVM *svm)
Definition: Kernel.cpp:366
A generic Support Vector Machine Interface.
Definition: SVM.h:46
CKernelNormalizer * normalizer
Definition: Kernel.h:835
virtual ~CKernel()
Definition: Kernel.cpp:70
virtual void add_to_normal(int32_t vector_idx, float64_t weight)
Definition: Kernel.cpp:330
#define SG_MALLOC(type, len)
Definition: memory.h:36
index_t vlen
Definition: DataType.h:248
virtual EFeatureClass get_feature_class()=0

SHOGUN Machine Learning Toolbox - Documentation