SHOGUN  3.2.1
 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/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__ */

SHOGUN Machine Learning Toolbox - Documentation