SHOGUN  6.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
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  return (CKernel*) kernel_array->get_element(idx);
128  }
129 
135  {
136  return get_kernel(get_num_kernels()-1);
137  }
138 
146  inline bool insert_kernel(CKernel* k, int32_t idx)
147  {
148  ASSERT(k)
150 
151  if (!(k->has_property(KP_LINADD)))
153 
154  return kernel_array->insert_element(k, idx);
155  }
156 
162  inline bool append_kernel(CKernel* k)
163  {
164  ASSERT(k)
166 
167  if (!(k->has_property(KP_LINADD)))
169 
170  int n = get_num_kernels();
172 
175 
176  return n+1==get_num_kernels();
177  }
178 
179 
185  inline bool delete_kernel(int32_t idx)
186  {
187  bool succesful_deletion = kernel_array->delete_element(idx);
188 
189  if (get_num_kernels()==0)
190  {
191  num_lhs=0;
192  num_rhs=0;
193  }
194 
195  if(enable_subkernel_weight_opt && succesful_deletion && get_num_kernels()>0)
197 
198  return succesful_deletion;
199  }
200 
206  {
208  }
209 
214  inline int32_t get_num_subkernels()
215  {
217  {
218  int32_t num_subkernels = 0;
219 
220  for (index_t k_idx=0; k_idx<get_num_kernels(); k_idx++)
221  {
222  CKernel* k = get_kernel(k_idx);
223  num_subkernels += k->get_num_subkernels();
224  SG_UNREF(k);
225  }
226  return num_subkernels;
227  }
228  else
229  return get_num_kernels();
230  }
231 
236  int32_t get_num_kernels()
237  {
238  return kernel_array->get_num_elements();
239  }
240 
245  virtual bool has_features()
246  {
247  return initialized;
248  }
249 
251  virtual void remove_lhs();
252 
254  virtual void remove_rhs();
255 
257  virtual void remove_lhs_and_rhs();
258 
266  virtual bool init_optimization(
267  int32_t count, int32_t *IDX, float64_t * weights);
268 
273  virtual bool delete_optimization();
274 
280  virtual float64_t compute_optimized(int32_t idx);
281 
290  virtual void compute_batch(
291  int32_t num_vec, int32_t* vec_idx, float64_t* target,
292  int32_t num_suppvec, int32_t* IDX, float64_t* alphas,
293  float64_t factor=1.0);
294 
307  CKernel* k, int32_t num_vec, int32_t* vec_idx, float64_t* target,
308  int32_t num_suppvec, int32_t* IDX, float64_t* weights);
309 
315  virtual void add_to_normal(int32_t idx, float64_t weight);
316 
318  virtual void clear_normal();
319 
325  virtual void compute_by_subkernel(
326  int32_t idx, float64_t * subkernel_contrib);
327 
333  virtual const float64_t* get_subkernel_weights(int32_t& num_weights);
334 
340 
345  virtual void set_subkernel_weights(SGVector<float64_t> weights);
346 
352 
354  bool precompute_subkernels();
355 
364 
373  index_t index=-1);
374 
380  {
382  return kernel_array;
383  }
384 
394  static CList* combine_kernels(CList* kernel_list);
395 
398  virtual void enable_subkernel_weight_learning();
399 
400  protected:
401  virtual void init_subkernel_weights();
402 
409  virtual float64_t compute(int32_t x, int32_t y);
410 
417  {
418  ASSERT(k)
419 
420  if (k->get_num_vec_lhs())
421  {
422  if (num_lhs)
425 
426  if (!get_num_subkernels())
427  {
428  initialized=true;
429 #ifdef USE_SVMLIGHT
430  cache_reset();
431 #endif //USE_SVMLIGHT
432  }
433  }
434  else
435  initialized=false;
436 
437  if (k->get_num_vec_rhs())
438  {
439  if (num_rhs)
442 
443  if (!get_num_subkernels())
444  {
445  initialized=true;
446 #ifdef USE_SVMLIGHT
447  cache_reset();
448 #endif //USE_SVMLIGHT
449  }
450  }
451  else
452  initialized=false;
453  }
454 
455  private:
456  void init();
457 
458  protected:
462  int32_t sv_count;
464  int32_t* sv_idx;
473 
476 
481 };
482 }
483 #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:200
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:50
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:68
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:839
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