SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DirectorKernel.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  * Copyright (C) 2012 Soeren Sonnenburg
8  * Copyright (C) 2012 Evgeniy Andreev (gsomix)
9  */
10 
11 #ifndef _DIRECTORKERNEL_H___
12 #define _DIRECTORKERNEL_H___
13 
14 #include <shogun/lib/config.h>
15 
16 #ifdef USE_SWIG_DIRECTORS
17 #include <shogun/lib/common.h>
18 #include <shogun/lib/DataType.h>
19 #include <shogun/kernel/Kernel.h>
20 
21 namespace shogun
22 {
23 #define IGNORE_IN_CLASSLIST
24 IGNORE_IN_CLASSLIST class CDirectorKernel: public CKernel
25 {
26  public:
30  CDirectorKernel()
31  : CKernel(), external_features(false)
32  {
33  }
34 
37  CDirectorKernel(bool is_external_features)
38  : CKernel(), external_features(is_external_features)
39  {
40  }
41 
45  CDirectorKernel(int32_t size, bool is_external_features)
46  : CKernel(size), external_features(is_external_features)
47  {
48  }
49 
53  virtual ~CDirectorKernel()
54  {
55  cleanup();
56  }
57 
64  virtual bool init(CFeatures* l, CFeatures* r)
65  {
66  if (this->parallel->get_num_threads()!=1)
67  {
68  SG_WARNING("Enforcing to use only one thread due to restrictions of directors\n")
69  this->parallel->set_num_threads(1);
70  }
71  return CKernel::init(l, r);
72  }
73 
78  virtual bool set_normalizer(CKernelNormalizer* normalizer)
79  {
80  return CKernel::set_normalizer(normalizer);
81  }
82 
87  virtual CKernelNormalizer* get_normalizer()
88  {
89  return CKernel::get_normalizer();
90  }
91 
95  virtual bool init_normalizer()
96  {
97  return CKernel::init_normalizer();
98  }
99 
106  virtual void cleanup()
107  {
109  }
110 
111  virtual float64_t kernel_function(int32_t idx_a, int32_t idx_b)
112  {
113  SG_ERROR("Kernel function of Director Kernel needs to be overridden.\n")
114  return 0;
115  }
116 
122  virtual SGVector<float64_t> get_kernel_col(int32_t j)
123  {
124  return CKernel::get_kernel_col(j);
125  }
126 
132  virtual SGVector<float64_t> get_kernel_row(int32_t i)
133  {
134  return CKernel::get_kernel_row(i);
135  }
136 
141  virtual int32_t get_num_vec_lhs()
142  {
143  return CKernel::get_num_vec_lhs();
144  }
145 
150  virtual int32_t get_num_vec_rhs()
151  {
152  return CKernel::get_num_vec_rhs();
153  }
154 
159  virtual void set_num_vec_lhs(int32_t num)
160  {
161  num_lhs=num;
162  }
163 
168  virtual void set_num_vec_rhs(int32_t num)
169  {
170  num_rhs=num;
171  }
172 
177  virtual bool has_features()
178  {
179  if (!external_features)
180  return CKernel::has_features();
181  else
182  return true;
183  }
184 
186  virtual void remove_lhs_and_rhs()
187  {
189  }
190 
192  virtual void remove_lhs()
193  {
195  }
196 
198  virtual void remove_rhs()
199  {
201  }
202 
207  virtual EKernelType get_kernel_type() { return K_DIRECTOR; }
208 
213  virtual EFeatureType get_feature_type() { return F_ANY; }
214 
219  virtual EFeatureClass get_feature_class() { return C_ANY; }
220 
225  virtual const char* get_name() const { return "DirectorKernel"; }
226 
230  virtual void clear_normal()
231  {
233  }
234 
240  virtual void add_to_normal(int32_t vector_idx, float64_t weight)
241  {
242  CKernel::add_to_normal(vector_idx, weight);
243  }
244 
249  virtual void set_optimization_type(EOptimizationType t)
250  {
252  }
253 
261  virtual bool init_optimization(
262  int32_t count, int32_t *IDX, float64_t *weights)
263  {
264  return CKernel::init_optimization(count, IDX, weights);
265  }
266 
271  virtual bool delete_optimization()
272  {
274  }
275 
281  virtual float64_t compute_optimized(int32_t vector_idx)
282  {
283  return CKernel::compute_optimized(vector_idx);
284  }
285 
294  virtual void compute_batch(
295  int32_t num_vec, int32_t* vec_idx, float64_t* target,
296  int32_t num_suppvec, int32_t* IDX, float64_t* alphas,
297  float64_t factor=1.0)
298  {
299  CKernel::compute_batch(num_vec, vec_idx, target, num_suppvec, IDX, alphas, factor);
300  }
301 
306  virtual int32_t get_num_subkernels()
307  {
309  }
310 
316  virtual void compute_by_subkernel(
317  int32_t vector_idx, float64_t * subkernel_contrib)
318  {
319  CKernel::compute_by_subkernel(vector_idx, subkernel_contrib);
320  }
321 
327  virtual const float64_t* get_subkernel_weights(int32_t& num_weights)
328  {
329  return CKernel::get_subkernel_weights(num_weights);
330  }
331 
336  virtual void set_subkernel_weights(SGVector<float64_t> weights)
337  {
339  }
340 
341  protected:
361  virtual TParameter* migrate(DynArray<TParameter*>* param_base,
362  const SGParamInfo* target)
363  {
364  return CSGObject::migrate(param_base, target);
365  }
366 
389  virtual void one_to_one_migration_prepare(DynArray<TParameter*>* param_base,
390  const SGParamInfo* target, TParameter*& replacement,
391  TParameter*& to_migrate, char* old_name=NULL)
392  {
393  return CSGObject::one_to_one_migration_prepare(param_base, target,
394  replacement, to_migrate, old_name);
395  }
404  virtual void load_serializable_pre() throw (ShogunException)
405  {
407  }
408 
417  virtual void load_serializable_post() throw (ShogunException)
418  {
420  }
421 
430  virtual void save_serializable_pre() throw (ShogunException)
431  {
433  }
434 
443  virtual void save_serializable_post() throw (ShogunException)
444  {
446  }
447 
456  virtual float64_t compute(int32_t idx_a, int32_t idx_b)
457  {
458  return kernel_function(idx_a, idx_b);
459  }
460 
461  virtual void register_params()
462  {
464  }
465 
466  protected:
467  /* is external features */
468  bool external_features;
469 };
470 }
471 #endif /* USE_SWIG_DIRECTORS */
472 #endif /* _DIRECTORKERNEL_H__ */

SHOGUN Machine Learning Toolbox - Documentation