SHOGUN  6.1.3
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/config.h>
16 
17 #include <shogun/lib/List.h>
19 #include <shogun/io/SGIO.h>
20 #include <shogun/kernel/Kernel.h>
21 
24 
25 namespace shogun
26 {
27 class CFeatures;
28 class CCombinedFeatures;
29 class CList;
30 class CListElement;
49 class CCombinedKernel : public CKernel
50 {
51  public:
58  CCombinedKernel(int32_t size=10, bool append_subkernel_weights=false);
59 
60  virtual ~CCombinedKernel();
61 
70  virtual bool init(CFeatures* lhs, CFeatures* rhs);
71 
73  virtual void cleanup();
74 
80  {
81  return K_COMBINED;
82  }
83 
89  {
90  return F_UNKNOWN;
91  }
92 
98  {
99  return C_COMBINED;
100  }
101 
106  virtual const char* get_name() const { return "CombinedKernel"; }
107 
109  void list_kernels();
110 
116  {
117  return get_kernel(0);
118  }
119 
125  inline CKernel* get_kernel(int32_t idx)
126  {
127  if (idx < get_num_kernels())
128  {
129  return (CKernel*)kernel_array->get_element(idx);
130  }
131  else
132  {
133  return 0;
134  }
135  }
136 
142  {
143  return get_kernel(get_num_kernels()-1);
144  }
145 
153  inline bool insert_kernel(CKernel* k, int32_t idx)
154  {
155  ASSERT(k)
157 
158  if (!(k->has_property(KP_LINADD)))
160 
161  return kernel_array->insert_element(k, idx);
162  }
163 
169  inline bool append_kernel(CKernel* k)
170  {
171  ASSERT(k)
173 
174  if (!(k->has_property(KP_LINADD)))
176 
177  int n = get_num_kernels();
179 
182 
183  return n+1==get_num_kernels();
184  }
185 
186 
192  inline bool delete_kernel(int32_t idx)
193  {
194  bool succesful_deletion = kernel_array->delete_element(idx);
195 
196  if (get_num_kernels()==0)
197  {
198  num_lhs=0;
199  num_rhs=0;
200  }
201 
202  if(enable_subkernel_weight_opt && succesful_deletion && get_num_kernels()>0)
204 
205  return succesful_deletion;
206  }
207 
213  {
215  }
216 
221  inline int32_t get_num_subkernels()
222  {
224  {
225  int32_t num_subkernels = 0;
226 
227  for (index_t k_idx=0; k_idx<get_num_kernels(); k_idx++)
228  {
229  CKernel* k = get_kernel(k_idx);
230  num_subkernels += k->get_num_subkernels();
231  SG_UNREF(k);
232  }
233  return num_subkernels;
234  }
235  else
236  return get_num_kernels();
237  }
238 
243  int32_t get_num_kernels()
244  {
245  return kernel_array->get_num_elements();
246  }
247 
252  virtual bool has_features()
253  {
254  return initialized;
255  }
256 
258  virtual void remove_lhs();
259 
261  virtual void remove_rhs();
262 
264  virtual void remove_lhs_and_rhs();
265 
273  virtual bool init_optimization(
274  int32_t count, int32_t *IDX, float64_t * weights);
275 
280  virtual bool delete_optimization();
281 
287  virtual float64_t compute_optimized(int32_t idx);
288 
297  virtual void compute_batch(
298  int32_t num_vec, int32_t* vec_idx, float64_t* target,
299  int32_t num_suppvec, int32_t* IDX, float64_t* alphas,
300  float64_t factor=1.0);
301 
314  CKernel* k, int32_t num_vec, int32_t* vec_idx, float64_t* target,
315  int32_t num_suppvec, int32_t* IDX, float64_t* weights);
316 
322  virtual void add_to_normal(int32_t idx, float64_t weight);
323 
325  virtual void clear_normal();
326 
332  virtual void compute_by_subkernel(
333  int32_t idx, float64_t * subkernel_contrib);
334 
340  virtual const float64_t* get_subkernel_weights(int32_t& num_weights);
341 
347 
352  virtual void set_subkernel_weights(SGVector<float64_t> weights);
353 
359 
361  bool precompute_subkernels();
362 
371 
380  index_t index=-1);
381 
387  {
389  return kernel_array;
390  }
391 
401  static CList* combine_kernels(CList* kernel_list);
402 
405  virtual void enable_subkernel_weight_learning();
406 
407  protected:
408  virtual void init_subkernel_weights();
409 
416  virtual float64_t compute(int32_t x, int32_t y);
417 
424  {
425  ASSERT(k)
426 
427  if (k->get_num_vec_lhs())
428  {
429  if (num_lhs)
432 
433  if (!get_num_subkernels())
434  {
435  initialized=true;
436 #ifdef USE_SVMLIGHT
437  cache_reset();
438 #endif //USE_SVMLIGHT
439  }
440  }
441  else
442  initialized=false;
443 
444  if (k->get_num_vec_rhs())
445  {
446  if (num_rhs)
449 
450  if (!get_num_subkernels())
451  {
452  initialized=true;
453 #ifdef USE_SVMLIGHT
454  cache_reset();
455 #endif //USE_SVMLIGHT
456  }
457  }
458  else
459  initialized=false;
460  }
461 
462  private:
463  void init();
464 
465  protected:
469  int32_t sv_count;
471  int32_t* sv_idx;
480 
483 
488 };
489 }
490 #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)
SGVector< float64_t > subkernel_log_weights
virtual void set_subkernel_weights(SGVector< float64_t > weights)
int32_t index_t
Definition: common.h:72
int32_t num_rhs
number of feature vectors on right hand side
virtual bool init(CFeatures *lhs, CFeatures *rhs)
SGMatrix< float64_t > get_parameter_gradient(const TParameter *param, index_t index=-1)
bool insert_element(CSGObject *e, int32_t index)
bool append_kernel(CKernel *k)
parameter struct
CDynamicObjectArray * kernel_array
float64_t kernel(int32_t idx_a, int32_t idx_b)
virtual void set_optimization_type(EOptimizationType t)
bool delete_kernel(int32_t idx)
virtual int32_t get_num_vec_lhs()
#define SG_REF(x)
Definition: SGObject.h:52
EFeatureClass
shogun feature class
Definition: FeatureTypes.h:38
CDynamicObjectArray * get_array()
bool has_property(EKernelProperty p)
virtual void remove_lhs_and_rhs()
virtual SGVector< float64_t > get_subkernel_weights()
#define ASSERT(x)
Definition: SGIO.h:176
CKernel * get_kernel(int32_t idx)
double float64_t
Definition: common.h:60
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)
static CList * combine_kernels(CList *kernel_list)
The Combined kernel is used to combine a number of kernels into a single CombinedKernel object by lin...
Dynamic array class for CSGObject pointers that creates an array that can be used like a list or an a...
int32_t num_lhs
number of feature vectors on left hand side
virtual int32_t get_num_vec_rhs()
EFeatureType
shogun feature type
Definition: FeatureTypes.h:19
CFeatures * rhs
feature vectors to occur on right hand side
virtual EFeatureType get_feature_type()
#define SG_UNREF(x)
Definition: SGObject.h:53
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
virtual bool has_features()
static CCombinedKernel * obtain_from_generic(CKernel *kernel)
EOptimizationType
Definition: kernel/Kernel.h:49
CFeatures * lhs
feature vectors to occur on left hand side
virtual bool delete_optimization()
The class Features is the base class of all feature objects.
Definition: Features.h:69
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:840
virtual float64_t compute_optimized(int32_t idx)
The Kernel base class.
CSGObject * get_element(int32_t index) const
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:84
CCombinedKernel(int32_t size=10, bool append_subkernel_weights=false)
virtual void init_subkernel_weights()
virtual void enable_subkernel_weight_learning()
bool insert_kernel(CKernel *k, int32_t idx)

SHOGUN Machine Learning Toolbox - Documentation