SHOGUN
v2.0.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Copyright (C) 2012 Soeren Sonnenburg 00008 * Copyright (C) 2012 Evgeniy Andreev (gsomix) 00009 */ 00010 00011 #ifndef _DIRECTORKERNEL_H___ 00012 #define _DIRECTORKERNEL_H___ 00013 00014 #include <shogun/lib/config.h> 00015 00016 #ifdef USE_SWIG_DIRECTORS 00017 #include <shogun/lib/common.h> 00018 #include <shogun/lib/DataType.h> 00019 #include <shogun/kernel/Kernel.h> 00020 00021 namespace shogun 00022 { 00023 #define IGNORE_IN_CLASSLIST 00024 IGNORE_IN_CLASSLIST class CDirectorKernel: public CKernel 00025 { 00026 public: 00030 CDirectorKernel() 00031 : CKernel(), external_features(false) 00032 { 00033 } 00034 00037 CDirectorKernel(bool is_external_features) 00038 : CKernel(), external_features(is_external_features) 00039 { 00040 } 00041 00045 CDirectorKernel(int32_t size, bool is_external_features) 00046 : CKernel(size), external_features(is_external_features) 00047 { 00048 } 00049 00053 virtual ~CDirectorKernel() 00054 { 00055 cleanup(); 00056 } 00057 00064 virtual bool init(CFeatures* l, CFeatures* r) 00065 { 00066 if (this->parallel->get_num_threads()!=1) 00067 { 00068 SG_WARNING("Enforcing to use only one thread due to restrictions of directors\n"); 00069 this->parallel->set_num_threads(1); 00070 } 00071 return CKernel::init(l, r); 00072 } 00073 00078 virtual bool set_normalizer(CKernelNormalizer* normalizer) 00079 { 00080 return CKernel::set_normalizer(normalizer); 00081 } 00082 00087 virtual CKernelNormalizer* get_normalizer() 00088 { 00089 return CKernel::get_normalizer(); 00090 } 00091 00095 virtual bool init_normalizer() 00096 { 00097 return CKernel::init_normalizer(); 00098 } 00099 00106 virtual void cleanup() 00107 { 00108 CKernel::cleanup(); 00109 } 00110 00111 virtual float64_t kernel_function(int32_t idx_a, int32_t idx_b) 00112 { 00113 SG_ERROR("Kernel function of Director Kernel needs to be overridden.\n"); 00114 return 0; 00115 } 00116 00122 virtual SGVector<float64_t> get_kernel_col(int32_t j) 00123 { 00124 return CKernel::get_kernel_col(j); 00125 } 00126 00132 virtual SGVector<float64_t> get_kernel_row(int32_t i) 00133 { 00134 return CKernel::get_kernel_row(i); 00135 } 00136 00141 virtual int32_t get_num_vec_lhs() 00142 { 00143 return CKernel::get_num_vec_lhs(); 00144 } 00145 00150 virtual int32_t get_num_vec_rhs() 00151 { 00152 return CKernel::get_num_vec_rhs(); 00153 } 00154 00159 virtual void set_num_vec_lhs(int32_t num) 00160 { 00161 num_lhs=num; 00162 } 00163 00168 virtual void set_num_vec_rhs(int32_t num) 00169 { 00170 num_rhs=num; 00171 } 00172 00177 virtual bool has_features() 00178 { 00179 if (!external_features) 00180 return CKernel::has_features(); 00181 else 00182 return true; 00183 } 00184 00186 virtual void remove_lhs_and_rhs() 00187 { 00188 CKernel::remove_lhs_and_rhs(); 00189 } 00190 00192 virtual void remove_lhs() 00193 { 00194 CKernel::remove_lhs(); 00195 } 00196 00198 virtual void remove_rhs() 00199 { 00200 CKernel::remove_rhs(); 00201 } 00202 00207 virtual EKernelType get_kernel_type() { return K_DIRECTOR; } 00208 00213 virtual EFeatureType get_feature_type() { return F_ANY; } 00214 00219 virtual EFeatureClass get_feature_class() { return C_ANY; } 00220 00225 inline virtual const char* get_name() const { return "DirectorKernel"; } 00226 00230 virtual void clear_normal() 00231 { 00232 CKernel::clear_normal(); 00233 } 00234 00240 virtual void add_to_normal(int32_t vector_idx, float64_t weight) 00241 { 00242 CKernel::add_to_normal(vector_idx, weight); 00243 } 00244 00249 virtual inline void set_optimization_type(EOptimizationType t) 00250 { 00251 CKernel::set_optimization_type(t); 00252 } 00253 00261 virtual bool init_optimization( 00262 int32_t count, int32_t *IDX, float64_t *weights) 00263 { 00264 return CKernel::init_optimization(count, IDX, weights); 00265 } 00266 00271 virtual bool delete_optimization() 00272 { 00273 return CKernel::delete_optimization(); 00274 } 00275 00281 virtual float64_t compute_optimized(int32_t vector_idx) 00282 { 00283 return CKernel::compute_optimized(vector_idx); 00284 } 00285 00294 virtual void compute_batch( 00295 int32_t num_vec, int32_t* vec_idx, float64_t* target, 00296 int32_t num_suppvec, int32_t* IDX, float64_t* alphas, 00297 float64_t factor=1.0) 00298 { 00299 CKernel::compute_batch(num_vec, vec_idx, target, num_suppvec, IDX, alphas, factor); 00300 } 00301 00306 virtual int32_t get_num_subkernels() 00307 { 00308 return CKernel::get_num_subkernels(); 00309 } 00310 00316 virtual void compute_by_subkernel( 00317 int32_t vector_idx, float64_t * subkernel_contrib) 00318 { 00319 CKernel::compute_by_subkernel(vector_idx, subkernel_contrib); 00320 } 00321 00327 virtual const float64_t* get_subkernel_weights(int32_t& num_weights) 00328 { 00329 return CKernel::get_subkernel_weights(num_weights); 00330 } 00331 00336 virtual void set_subkernel_weights(SGVector<float64_t> weights) 00337 { 00338 CKernel::set_subkernel_weights(weights); 00339 } 00340 00341 protected: 00361 virtual TParameter* migrate(DynArray<TParameter*>* param_base, 00362 const SGParamInfo* target) 00363 { 00364 return CSGObject::migrate(param_base, target); 00365 } 00366 00389 virtual void one_to_one_migration_prepare(DynArray<TParameter*>* param_base, 00390 const SGParamInfo* target, TParameter*& replacement, 00391 TParameter*& to_migrate, char* old_name=NULL) 00392 { 00393 return CSGObject::one_to_one_migration_prepare(param_base, target, 00394 replacement, to_migrate, old_name); 00395 } 00404 virtual void load_serializable_pre() throw (ShogunException) 00405 { 00406 CKernel::load_serializable_pre(); 00407 } 00408 00417 virtual void load_serializable_post() throw (ShogunException) 00418 { 00419 CKernel::load_serializable_post(); 00420 } 00421 00430 virtual void save_serializable_pre() throw (ShogunException) 00431 { 00432 CKernel::save_serializable_pre(); 00433 } 00434 00443 virtual void save_serializable_post() throw (ShogunException) 00444 { 00445 CKernel::save_serializable_post(); 00446 } 00447 00456 virtual float64_t compute(int32_t idx_a, int32_t idx_b) 00457 { 00458 return kernel_function(idx_a, idx_b); 00459 } 00460 00461 virtual void register_params() 00462 { 00463 CKernel::register_params(); 00464 } 00465 00466 protected: 00467 /* is external features */ 00468 bool external_features; 00469 }; 00470 } 00471 #endif /* USE_SWIG_DIRECTORS */ 00472 #endif /* _DIRECTORKERNEL_H__ */