SHOGUN  v3.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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>
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 #ifdef USE_SVMLIGHT
427  cache_reset();
428 #endif //USE_SVMLIGHT
429  }
430  }
431  else
432  initialized=false;
433 
434  if (k->get_num_vec_rhs())
435  {
436  if (num_rhs)
439 
440  if (!get_num_subkernels())
441  {
442  initialized=true;
443 #ifdef USE_SVMLIGHT
444  cache_reset();
445 #endif //USE_SVMLIGHT
446  }
447  }
448  else
449  initialized=false;
450  }
451 
452  private:
453  void init();
454 
455  protected:
459  int32_t sv_count;
461  int32_t* sv_idx;
470 };
471 }
472 #endif /* _COMBINEDKERNEL_H__ */

SHOGUN Machine Learning Toolbox - Documentation