SHOGUN
v3.2.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 Jacob Walker 00008 * 00009 * Code adapted from CCombinedKernel 00010 */ 00011 00012 #ifndef _PRODUCTKERNEL_H___ 00013 #define _PRODUCTKERNEL_H___ 00014 00015 #include <shogun/lib/DynamicObjectArray.h> 00016 #include <shogun/io/SGIO.h> 00017 #include <shogun/kernel/Kernel.h> 00018 00019 #include <shogun/features/Features.h> 00020 #include <shogun/features/CombinedFeatures.h> 00021 00022 namespace shogun 00023 { 00024 class CFeatures; 00025 class CCombinedFeatures; 00026 class CDynamicObjectArray; 00027 00039 class CProductKernel : public CKernel 00040 { 00041 public: 00046 CProductKernel(int32_t size=10); 00047 00048 virtual ~CProductKernel(); 00049 00056 virtual bool init(CFeatures* lhs, CFeatures* rhs); 00057 00059 virtual void cleanup(); 00060 00065 virtual EKernelType get_kernel_type() { return K_PRODUCT; } 00066 00071 virtual EFeatureType get_feature_type() { return F_UNKNOWN; } 00072 00077 virtual EFeatureClass get_feature_class() { return C_COMBINED; } 00078 00083 virtual const char* get_name() const { return "ProductKernel"; } 00084 00086 void list_kernels(); 00087 00093 inline CKernel* get_kernel(int32_t idx) 00094 { 00095 return (CKernel*) kernel_array->get_element(idx); 00096 } 00097 00105 inline bool insert_kernel(CKernel* k, int32_t idx) 00106 { 00107 ASSERT(k) 00108 adjust_num_lhs_rhs_initialized(k); 00109 00110 if (!(k->has_property(KP_LINADD))) 00111 unset_property(KP_LINADD); 00112 00113 return kernel_array->insert_element(k, idx); 00114 } 00115 00121 inline bool append_kernel(CKernel* k) 00122 { 00123 ASSERT(k) 00124 adjust_num_lhs_rhs_initialized(k); 00125 00126 if (!(k->has_property(KP_LINADD))) 00127 unset_property(KP_LINADD); 00128 00129 int32_t n = get_num_subkernels(); 00130 kernel_array->push_back(k); 00131 return n+1==get_num_subkernels(); 00132 } 00133 00139 inline bool delete_kernel(int32_t idx) 00140 { 00141 return kernel_array->delete_element(idx); 00142 } 00143 00148 inline int32_t get_num_subkernels() 00149 { 00150 return kernel_array->get_num_elements(); 00151 } 00152 00157 virtual bool has_features() 00158 { 00159 return initialized; 00160 } 00161 00163 virtual void remove_lhs(); 00164 00166 virtual void remove_rhs(); 00167 00169 virtual void remove_lhs_and_rhs(); 00170 00172 bool precompute_subkernels(); 00173 00177 CProductKernel* KernelToProductKernel(CKernel* n) 00178 { 00179 return dynamic_cast<CProductKernel*>(n); 00180 } 00181 00189 SGMatrix<float64_t> get_parameter_gradient(const TParameter* param, 00190 index_t index=-1); 00191 00196 inline CDynamicObjectArray* get_array() 00197 { 00198 SG_REF(kernel_array); 00199 return kernel_array; 00200 } 00201 00202 protected: 00209 virtual float64_t compute(int32_t x, int32_t y); 00210 00216 inline void adjust_num_lhs_rhs_initialized(CKernel* k) 00217 { 00218 ASSERT(k) 00219 00220 if (k->get_num_vec_lhs()) 00221 { 00222 if (num_lhs) 00223 ASSERT(num_lhs==k->get_num_vec_lhs()) 00224 num_lhs=k->get_num_vec_lhs(); 00225 00226 if (!get_num_subkernels()) 00227 { 00228 initialized=true; 00229 #ifdef USE_SVMLIGHT 00230 cache_reset(); 00231 #endif //USE_SVMLIGHT 00232 } 00233 } 00234 else 00235 initialized=false; 00236 00237 if (k->get_num_vec_rhs()) 00238 { 00239 if (num_rhs) 00240 ASSERT(num_rhs==k->get_num_vec_rhs()) 00241 num_rhs=k->get_num_vec_rhs(); 00242 00243 if (!get_num_subkernels()) 00244 { 00245 initialized=true; 00246 #ifdef USE_SVMLIGHT 00247 cache_reset(); 00248 #endif //USE_SVMLIGHT 00249 } 00250 } 00251 else 00252 initialized=false; 00253 } 00254 00255 private: 00256 void init(); 00257 00258 protected: 00260 CDynamicObjectArray* kernel_array; 00262 bool initialized; 00263 }; 00264 } 00265 #endif /* _PRODUCTKERNEL_H__ */