SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CombinedKernel.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 _COMBINEDKERNEL_H___
13 #define _COMBINEDKERNEL_H___
14 
15 #include <shogun/lib/List.h>
16 #include <shogun/io/SGIO.h>
17 #include <shogun/kernel/Kernel.h>
18 
21 
22 namespace shogun
23 {
24 class CFeatures;
25 class CCombinedFeatures;
26 class CList;
27 class CListElement;
46 class CCombinedKernel : public CKernel
47 {
48  public:
55  CCombinedKernel(int32_t size=10, bool append_subkernel_weights=false);
56 
57  virtual ~CCombinedKernel();
58 
65  virtual bool init(CFeatures* lhs, CFeatures* rhs);
66 
68  virtual void cleanup();
69 
75  {
76  return K_COMBINED;
77  }
78 
84  {
85  return F_UNKNOWN;
86  }
87 
93  {
94  return C_COMBINED;
95  }
96 
101  virtual const char* get_name() const { return "CombinedKernel"; }
102 
104  void list_kernels();
105 
111  {
113  }
114 
121  {
122  return (CKernel*) kernel_list->get_first_element(current);
123  }
124 
130  inline CKernel* get_kernel(int32_t idx)
131  {
132  CKernel * k = get_first_kernel();
133  for (int32_t i=0; i<idx; i++)
134  {
135  SG_UNREF(k);
136  k = get_next_kernel();
137  }
138  return k;
139  }
140 
146  {
147  return (CKernel*) kernel_list->get_last_element();
148  }
149 
155  {
156  return (CKernel*) kernel_list->get_next_element();
157  }
158 
165  {
166  return (CKernel*) kernel_list->get_next_element(current);
167  }
168 
174  inline bool insert_kernel(CKernel* k)
175  {
176  ASSERT(k);
178 
179  if (!(k->has_property(KP_LINADD)))
181 
182  return kernel_list->insert_element(k);
183  }
184 
190  inline bool append_kernel(CKernel* k)
191  {
192  ASSERT(k);
194 
195  if (!(k->has_property(KP_LINADD)))
197 
198  return kernel_list->append_element(k);
199  }
200 
201 
206  inline bool delete_kernel()
207  {
209  SG_UNREF(k);
210 
211  if (!k)
212  {
213  num_lhs=0;
214  num_rhs=0;
215  }
216 
217  return (k!=NULL);
218  }
219 
225  {
227  }
228 
233  inline int32_t get_num_subkernels()
234  {
236  {
237  int32_t num_subkernels = 0 ;
238  CListElement* current = NULL ;
239  CKernel * k = get_first_kernel(current) ;
240 
241  while(k)
242  {
243  num_subkernels += k->get_num_subkernels() ;
244  SG_UNREF(k);
245  k = get_next_kernel(current) ;
246  }
247  return num_subkernels ;
248  }
249  else
250  return kernel_list->get_num_elements();
251  }
252 
257  virtual inline bool has_features()
258  {
259  return initialized;
260  }
261 
263  virtual void remove_lhs();
264 
266  virtual void remove_rhs();
267 
269  virtual void remove_lhs_and_rhs();
270 
278  virtual bool init_optimization(
279  int32_t count, int32_t *IDX, float64_t * weights);
280 
285  virtual bool delete_optimization();
286 
292  virtual float64_t compute_optimized(int32_t idx);
293 
300  virtual void compute_batch(
301  int32_t num_vec, int32_t* vec_idx, float64_t* target,
302  int32_t num_suppvec, int32_t* IDX, float64_t* alphas,
303  float64_t factor=1.0);
304 
309  static void* compute_optimized_kernel_helper(void* p);
310 
315  static void* compute_kernel_helper(void* p);
316 
328  CKernel* k, int32_t num_vec, int32_t* vec_idx, float64_t* target,
329  int32_t num_suppvec, int32_t* IDX, float64_t* weights);
330 
336  virtual void add_to_normal(int32_t idx, float64_t weight);
337 
339  virtual void clear_normal();
340 
346  virtual void compute_by_subkernel(
347  int32_t idx, float64_t * subkernel_contrib);
348 
354  virtual const float64_t* get_subkernel_weights(int32_t& num_weights);
355 
361 
366  virtual void set_subkernel_weights(SGVector<float64_t> weights);
367 
373 
375  bool precompute_subkernels();
376 
381  {
382  return dynamic_cast<CCombinedKernel*>(n);
383  }
384 
385  protected:
392  virtual float64_t compute(int32_t x, int32_t y);
393 
400  {
401  ASSERT(k);
402 
403  if (k->get_num_vec_lhs())
404  {
405  if (num_lhs)
408 
409  if (!get_num_subkernels())
410  {
411  initialized=true;
412 
413  }
414  }
415  else
416  initialized=false;
417 
418  if (k->get_num_vec_rhs())
419  {
420  if (num_rhs)
423 
424  if (!get_num_subkernels())
425  {
426  initialized=true;
427 
428  }
429  }
430  else
431  initialized=false;
432  }
433 
434  private:
435  void init();
436 
437  protected:
441  int32_t sv_count;
443  int32_t* sv_idx;
452 };
453 }
454 #endif /* _COMBINEDKERNEL_H__ */
virtual void compute_by_subkernel(int32_t idx, float64_t *subkernel_contrib)
virtual EKernelType get_kernel_type()
virtual EFeatureClass get_feature_class()
virtual const char * get_name() const
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)
CSGObject * get_next_element()
Definition: List.h:162
EKernelType
Definition: Kernel.h:52
virtual void set_subkernel_weights(SGVector< float64_t > weights)
int32_t num_rhs
number of feature vectors on right hand side
Definition: Kernel.h:818
virtual bool init(CFeatures *lhs, CFeatures *rhs)
CCombinedKernel * KernelToCombinedKernel(shogun::CKernel *n)
bool append_kernel(CKernel *k)
CKernel * get_next_kernel(CListElement *&current)
virtual void set_optimization_type(EOptimizationType t)
virtual int32_t get_num_vec_lhs()
Definition: Kernel.h:437
CSGObject * delete_element()
Definition: List.h:413
EFeatureClass
shogun feature class
Definition: FeatureTypes.h:35
bool has_property(EKernelProperty p)
Definition: Kernel.h:529
static void * compute_optimized_kernel_helper(void *p)
CSGObject * get_first_element()
Definition: List.h:128
virtual void remove_lhs_and_rhs()
bool insert_kernel(CKernel *k)
virtual SGVector< float64_t > get_subkernel_weights()
#define ASSERT(x)
Definition: SGIO.h:102
CKernel * get_kernel(int32_t idx)
CKernel * get_first_kernel(CListElement *&current)
double float64_t
Definition: common.h:56
virtual bool init_optimization(int32_t count, int32_t *IDX, float64_t *weights)
virtual float64_t compute(int32_t x, int32_t y)
void unset_property(EKernelProperty p)
Definition: Kernel.h:657
int32_t get_num_elements()
Definition: List.h:122
The Combined kernel is used to combine a number of kernels into a single CombinedKernel object by lin...
static void * compute_kernel_helper(void *p)
int32_t num_lhs
number of feature vectors on left hand side
Definition: Kernel.h:816
virtual int32_t get_num_vec_rhs()
Definition: Kernel.h:446
EFeatureType
shogun feature type
Definition: FeatureTypes.h:16
CFeatures * rhs
feature vectors to occur on right hand side
Definition: Kernel.h:810
virtual EFeatureType get_feature_type()
#define SG_UNREF(x)
Definition: SGObject.h:45
virtual bool has_features()
EOptimizationType
Definition: Kernel.h:45
CFeatures * lhs
feature vectors to occur on left hand side
Definition: Kernel.h:808
virtual bool delete_optimization()
Class ListElement, defines how an element of the the list looks like.
Definition: List.h:22
The class Features is the base class of all feature objects.
Definition: Features.h:56
bool append_element(CSGObject *data)
Definition: List.h:307
virtual void add_to_normal(int32_t idx, float64_t weight)
void emulate_compute_batch(CKernel *k, int32_t num_vec, int32_t *vec_idx, float64_t *target, int32_t num_suppvec, int32_t *IDX, float64_t *weights)
virtual int32_t get_num_subkernels()
Definition: Kernel.cpp:340
virtual float64_t compute_optimized(int32_t idx)
The Kernel base class.
Definition: Kernel.h:172
CSGObject * get_last_element()
Definition: List.h:145
void adjust_num_lhs_rhs_initialized(CKernel *k)
float64_t * subkernel_weights_buffer
Class List implements a doubly connected list for low-level-objects.
Definition: List.h:78
bool insert_element(CSGObject *data)
Definition: List.h:364
CCombinedKernel(int32_t size=10, bool append_subkernel_weights=false)

SHOGUN Machine Learning Toolbox - Documentation