SHOGUN  v3.2.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
CombinedKernel.h
浏览该文件的文档.
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>
17 #include <shogun/io/SGIO.h>
18 #include <shogun/kernel/Kernel.h>
19 
22 
23 namespace shogun
24 {
25 class CFeatures;
26 class CCombinedFeatures;
27 class CList;
28 class CListElement;
47 class CCombinedKernel : public CKernel
48 {
49  public:
56  CCombinedKernel(int32_t size=10, bool append_subkernel_weights=false);
57 
58  virtual ~CCombinedKernel();
59 
68  virtual bool init(CFeatures* lhs, CFeatures* rhs);
69 
71  virtual void cleanup();
72 
78  {
79  return K_COMBINED;
80  }
81 
87  {
88  return F_UNKNOWN;
89  }
90 
96  {
97  return C_COMBINED;
98  }
99 
104  virtual const char* get_name() const { return "CombinedKernel"; }
105 
107  void list_kernels();
108 
114  {
115  return get_kernel(0);
116  }
117 
123  inline CKernel* get_kernel(int32_t idx)
124  {
125  return (CKernel*) kernel_array->get_element(idx);
126  }
127 
133  {
134  return get_kernel(get_num_kernels()-1);
135  }
136 
144  inline bool insert_kernel(CKernel* k, int32_t idx)
145  {
146  ASSERT(k)
148 
149  if (!(k->has_property(KP_LINADD)))
151 
152  return kernel_array->insert_element(k, idx);
153  }
154 
160  inline bool append_kernel(CKernel* k)
161  {
162  ASSERT(k)
164 
165  if (!(k->has_property(KP_LINADD)))
167 
168  int n = get_num_kernels();
170  return n+1==get_num_kernels();
171  }
172 
173 
179  inline bool delete_kernel(int32_t idx)
180  {
181  bool succesful_deletion = kernel_array->delete_element(idx);
182 
183  if (get_num_kernels()==0)
184  {
185  num_lhs=0;
186  num_rhs=0;
187  }
188 
189  return succesful_deletion;
190  }
191 
197  {
199  }
200 
205  inline int32_t get_num_subkernels()
206  {
208  {
209  int32_t num_subkernels = 0;
210 
211  for (index_t k_idx=0; k_idx<get_num_kernels(); k_idx++)
212  {
213  CKernel* k = get_kernel(k_idx);
214  num_subkernels += k->get_num_subkernels();
215  SG_UNREF(k);
216  }
217  return num_subkernels;
218  }
219  else
220  return get_num_kernels();
221  }
222 
227  int32_t get_num_kernels()
228  {
229  return kernel_array->get_num_elements();
230  }
231 
236  virtual bool has_features()
237  {
238  return initialized;
239  }
240 
242  virtual void remove_lhs();
243 
245  virtual void remove_rhs();
246 
248  virtual void remove_lhs_and_rhs();
249 
257  virtual bool init_optimization(
258  int32_t count, int32_t *IDX, float64_t * weights);
259 
264  virtual bool delete_optimization();
265 
271  virtual float64_t compute_optimized(int32_t idx);
272 
281  virtual void compute_batch(
282  int32_t num_vec, int32_t* vec_idx, float64_t* target,
283  int32_t num_suppvec, int32_t* IDX, float64_t* alphas,
284  float64_t factor=1.0);
285 
290  static void* compute_optimized_kernel_helper(void* p);
291 
296  static void* compute_kernel_helper(void* p);
297 
310  CKernel* k, int32_t num_vec, int32_t* vec_idx, float64_t* target,
311  int32_t num_suppvec, int32_t* IDX, float64_t* weights);
312 
318  virtual void add_to_normal(int32_t idx, float64_t weight);
319 
321  virtual void clear_normal();
322 
328  virtual void compute_by_subkernel(
329  int32_t idx, float64_t * subkernel_contrib);
330 
336  virtual const float64_t* get_subkernel_weights(int32_t& num_weights);
337 
343 
348  virtual void set_subkernel_weights(SGVector<float64_t> weights);
349 
355 
357  bool precompute_subkernels();
358 
367 
376  index_t index=-1);
377 
383  {
385  return kernel_array;
386  }
387 
397  static CList* combine_kernels(CList* kernel_list);
398 
399  protected:
406  virtual float64_t compute(int32_t x, int32_t y);
407 
414  {
415  ASSERT(k)
416 
417  if (k->get_num_vec_lhs())
418  {
419  if (num_lhs)
422 
423  if (!get_num_subkernels())
424  {
425  initialized=true;
426 
427  }
428  }
429  else
430  initialized=false;
431 
432  if (k->get_num_vec_rhs())
433  {
434  if (num_rhs)
437 
438  if (!get_num_subkernels())
439  {
440  initialized=true;
441 
442  }
443  }
444  else
445  initialized=false;
446  }
447 
448  private:
449  void init();
450 
451  protected:
455  int32_t sv_count;
457  int32_t* sv_idx;
466 };
467 }
468 #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:51
virtual void set_subkernel_weights(SGVector< float64_t > weights)
int32_t index_t
Definition: common.h:60
int32_t num_rhs
number of feature vectors on right hand side
Definition: Kernel.h:714
virtual bool init(CFeatures *lhs, CFeatures *rhs)
#define SG_UNREF(x)
Definition: SGRefObject.h:35
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
Definition: Parameter.h:26
CDynamicObjectArray * kernel_array
float64_t kernel(int32_t idx_a, int32_t idx_b)
Definition: Kernel.h:198
virtual void set_optimization_type(EOptimizationType t)
bool delete_kernel(int32_t idx)
virtual int32_t get_num_vec_lhs()
Definition: Kernel.h:355
EFeatureClass
shogun feature class
Definition: FeatureTypes.h:35
CDynamicObjectArray * get_array()
bool has_property(EKernelProperty p)
Definition: Kernel.h:447
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:203
CKernel * get_kernel(int32_t idx)
double float64_t
Definition: common.h:48
virtual bool init_optimization(int32_t count, int32_t *IDX, float64_t *weights)
virtual float64_t compute(int32_t x, int32_t y)
#define SG_REF(x)
Definition: SGRefObject.h:34
void unset_property(EKernelProperty p)
Definition: Kernel.h:601
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:712
virtual int32_t get_num_vec_rhs()
Definition: Kernel.h:364
EFeatureType
shogun feature type
Definition: FeatureTypes.h:16
CFeatures * rhs
feature vectors to occur on right hand side
Definition: Kernel.h:706
virtual EFeatureType get_feature_type()
virtual bool has_features()
static CCombinedKernel * obtain_from_generic(CKernel *kernel)
EOptimizationType
Definition: Kernel.h:44
CFeatures * lhs
feature vectors to occur on left hand side
Definition: Kernel.h:704
virtual bool delete_optimization()
The class Features is the base class of all feature objects.
Definition: Features.h:62
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:361
virtual float64_t compute_optimized(int32_t idx)
The Kernel base class.
Definition: Kernel.h:150
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:82
CCombinedKernel(int32_t size=10, bool append_subkernel_weights=false)
bool insert_kernel(CKernel *k, int32_t idx)

SHOGUN Machine Learning Toolbox - Documentation