SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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/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  return n+1==get_num_kernels();
173  }
174 
175 
181  inline bool delete_kernel(int32_t idx)
182  {
183  bool succesful_deletion = kernel_array->delete_element(idx);
184 
185  if (get_num_kernels()==0)
186  {
187  num_lhs=0;
188  num_rhs=0;
189  }
190 
191  return succesful_deletion;
192  }
193 
199  {
201  }
202 
207  inline int32_t get_num_subkernels()
208  {
210  {
211  int32_t num_subkernels = 0;
212 
213  for (index_t k_idx=0; k_idx<get_num_kernels(); k_idx++)
214  {
215  CKernel* k = get_kernel(k_idx);
216  num_subkernels += k->get_num_subkernels();
217  SG_UNREF(k);
218  }
219  return num_subkernels;
220  }
221  else
222  return get_num_kernels();
223  }
224 
229  int32_t get_num_kernels()
230  {
231  return kernel_array->get_num_elements();
232  }
233 
238  virtual bool has_features()
239  {
240  return initialized;
241  }
242 
244  virtual void remove_lhs();
245 
247  virtual void remove_rhs();
248 
250  virtual void remove_lhs_and_rhs();
251 
259  virtual bool init_optimization(
260  int32_t count, int32_t *IDX, float64_t * weights);
261 
266  virtual bool delete_optimization();
267 
273  virtual float64_t compute_optimized(int32_t idx);
274 
283  virtual void compute_batch(
284  int32_t num_vec, int32_t* vec_idx, float64_t* target,
285  int32_t num_suppvec, int32_t* IDX, float64_t* alphas,
286  float64_t factor=1.0);
287 
292  static void* compute_optimized_kernel_helper(void* p);
293 
298  static void* compute_kernel_helper(void* p);
299 
312  CKernel* k, int32_t num_vec, int32_t* vec_idx, float64_t* target,
313  int32_t num_suppvec, int32_t* IDX, float64_t* weights);
314 
320  virtual void add_to_normal(int32_t idx, float64_t weight);
321 
323  virtual void clear_normal();
324 
330  virtual void compute_by_subkernel(
331  int32_t idx, float64_t * subkernel_contrib);
332 
338  virtual const float64_t* get_subkernel_weights(int32_t& num_weights);
339 
345 
350  virtual void set_subkernel_weights(SGVector<float64_t> weights);
351 
357 
359  bool precompute_subkernels();
360 
369 
378  index_t index=-1);
379 
385  {
387  return kernel_array;
388  }
389 
399  static CList* combine_kernels(CList* kernel_list);
400 
401  protected:
408  virtual float64_t compute(int32_t x, int32_t y);
409 
416  {
417  ASSERT(k)
418 
419  if (k->get_num_vec_lhs())
420  {
421  if (num_lhs)
424 
425  if (!get_num_subkernels())
426  {
427  initialized=true;
428 #ifdef USE_SVMLIGHT
429  cache_reset();
430 #endif //USE_SVMLIGHT
431  }
432  }
433  else
434  initialized=false;
435 
436  if (k->get_num_vec_rhs())
437  {
438  if (num_rhs)
441 
442  if (!get_num_subkernels())
443  {
444  initialized=true;
445 #ifdef USE_SVMLIGHT
446  cache_reset();
447 #endif //USE_SVMLIGHT
448  }
449  }
450  else
451  initialized=false;
452  }
453 
454  private:
455  void init();
456 
457  protected:
461  int32_t sv_count;
463  int32_t* sv_idx;
472 };
473 }
474 #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)
EKernelType
Definition: Kernel.h:57
virtual void set_subkernel_weights(SGVector< float64_t > weights)
int32_t index_t
Definition: common.h:62
int32_t num_rhs
number of feature vectors on right hand side
Definition: Kernel.h:1069
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
void cache_reset()
Definition: Kernel.h:602
CDynamicObjectArray * kernel_array
float64_t kernel(int32_t idx_a, int32_t idx_b)
Definition: Kernel.h:206
virtual void set_optimization_type(EOptimizationType t)
bool delete_kernel(int32_t idx)
virtual int32_t get_num_vec_lhs()
Definition: Kernel.h:516
#define SG_REF(x)
Definition: SGObject.h:51
EFeatureClass
shogun feature class
Definition: FeatureTypes.h:38
CDynamicObjectArray * get_array()
bool has_property(EKernelProperty p)
Definition: Kernel.h:723
static void * compute_optimized_kernel_helper(void *p)
virtual void remove_lhs_and_rhs()
virtual SGVector< float64_t > get_subkernel_weights()
#define ASSERT(x)
Definition: SGIO.h:201
CKernel * get_kernel(int32_t idx)
double float64_t
Definition: common.h:50
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:890
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...
static void * compute_kernel_helper(void *p)
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
Definition: Kernel.h:1067
virtual int32_t get_num_vec_rhs()
Definition: Kernel.h:525
EFeatureType
shogun feature type
Definition: FeatureTypes.h:19
CFeatures * rhs
feature vectors to occur on right hand side
Definition: Kernel.h:1061
virtual EFeatureType get_feature_type()
#define SG_UNREF(x)
Definition: SGObject.h:52
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.h:50
CFeatures * lhs
feature vectors to occur on left hand side
Definition: Kernel.h:1059
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:852
virtual float64_t compute_optimized(int32_t idx)
The Kernel base class.
Definition: Kernel.h:158
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)
bool insert_kernel(CKernel *k, int32_t idx)

SHOGUN Machine Learning Toolbox - Documentation