CombinedKernel.h

Go to the documentation of this file.
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  * Written (W) 1999-2009 Soeren Sonnenburg
00008  * Written (W) 1999-2008 Gunnar Raetsch
00009  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
00010  */
00011 
00012 #ifndef _COMBINEDKERNEL_H___
00013 #define _COMBINEDKERNEL_H___
00014 
00015 #include "lib/List.h"
00016 #include "lib/io.h"
00017 #include "kernel/Kernel.h"
00018 
00019 #include "features/Features.h"
00020 #include "features/CombinedFeatures.h"
00021 
00040 class CCombinedKernel : public CKernel
00041 {
00042     public:
00049         CCombinedKernel(int32_t size=10, bool append_subkernel_weights=false);
00050 
00051         virtual ~CCombinedKernel();
00052 
00059         virtual bool init(CFeatures* lhs, CFeatures* rhs);
00060 
00062         virtual void cleanup();
00063 
00071         virtual bool load_init(FILE* src) { return false; }
00072 
00080         virtual bool save_init(FILE* dest) { return false; }
00081 
00086         virtual EKernelType get_kernel_type()
00087         {
00088             return K_COMBINED;
00089         }
00090 
00095         virtual EFeatureType get_feature_type()
00096         {
00097             return F_UNKNOWN;
00098         }
00099 
00104         virtual EFeatureClass get_feature_class()
00105         {
00106             return C_COMBINED;
00107         }
00108 
00113         virtual const char* get_name() const { return "Combined"; }
00114 
00116         void list_kernels();
00117 
00122         inline CKernel* get_first_kernel()
00123         {
00124             return kernel_list->get_first_element();
00125         }
00126 
00132         inline CKernel* get_first_kernel(CListElement<CKernel*>*&current)
00133         {
00134             return kernel_list->get_first_element(current);
00135         }
00136 
00142         inline CKernel* get_kernel(int32_t idx)
00143         {
00144             CKernel * k = get_first_kernel();
00145             for (int32_t i=1; i<idx; i++)
00146             {
00147                 SG_UNREF(k);
00148                 k = get_next_kernel();
00149             }
00150             return k;
00151         }
00152 
00157         inline CKernel* get_last_kernel()
00158         {
00159             return kernel_list->get_last_element();
00160         }
00161 
00166         inline CKernel* get_next_kernel()
00167         {
00168             return kernel_list->get_next_element();
00169         }
00170 
00176         inline CKernel* get_next_kernel(CListElement<CKernel*> *&current)
00177         {
00178             return kernel_list->get_next_element(current);
00179         }
00180 
00186         inline bool insert_kernel(CKernel* k)
00187         {
00188             ASSERT(k);
00189             adjust_num_lhs_rhs_initialized(k);
00190 
00191             if (!(k->has_property(KP_LINADD)))
00192                 unset_property(KP_LINADD);
00193 
00194             return kernel_list->insert_element(k);
00195         }
00196 
00202         inline bool append_kernel(CKernel* k)
00203         {
00204             ASSERT(k);
00205             adjust_num_lhs_rhs_initialized(k);
00206 
00207             if (!(k->has_property(KP_LINADD)))
00208                 unset_property(KP_LINADD);
00209 
00210             return kernel_list->append_element(k);
00211         }
00212 
00213 
00218         inline bool delete_kernel()
00219         {
00220             CKernel* k=kernel_list->delete_element();
00221             SG_UNREF(k);
00222 
00223             if (!k)
00224             {
00225                 num_lhs=0;
00226                 num_rhs=0;
00227             }
00228 
00229             return (k!=NULL);
00230         }
00231 
00236         inline bool get_append_subkernel_weights()
00237         {
00238             return append_subkernel_weights;
00239         }
00240 
00245         inline int32_t get_num_subkernels()
00246         {
00247             if (append_subkernel_weights)
00248             {
00249                 int32_t num_subkernels = 0 ;
00250                 CListElement<CKernel*> *current = NULL ;
00251                 CKernel * k = get_first_kernel(current) ;
00252 
00253                 while(k)
00254                 {
00255                     num_subkernels += k->get_num_subkernels() ;
00256                     SG_UNREF(k);
00257                     k = get_next_kernel(current) ;
00258                 }
00259                 return num_subkernels ;
00260             }
00261             else
00262                 return kernel_list->get_num_elements();
00263         }
00264 
00269         virtual inline int32_t get_num_vec_lhs()
00270         {
00271             return num_lhs;
00272         }
00273 
00278         virtual inline int32_t get_num_vec_rhs()
00279         {
00280             return num_rhs;
00281         }
00282 
00287         virtual inline bool has_features()
00288         {
00289             return initialized;
00290         }
00291 
00293         virtual void remove_lhs();
00294 
00296         virtual void remove_rhs();
00297 
00299         virtual void remove_lhs_and_rhs();
00300 
00308         virtual bool init_optimization(
00309             int32_t count, int32_t *IDX, float64_t * weights);
00310 
00315         virtual bool delete_optimization();
00316 
00322         virtual float64_t compute_optimized(int32_t idx);
00323 
00330         virtual void compute_batch(
00331             int32_t num_vec, int32_t* vec_idx, float64_t* target,
00332             int32_t num_suppvec, int32_t* IDX, float64_t* alphas,
00333             float64_t factor=1.0);
00334 
00339         static void* compute_optimized_kernel_helper(void* p);
00340 
00345         static void* compute_kernel_helper(void* p);
00346 
00357         void emulate_compute_batch(
00358             CKernel* k, int32_t num_vec, int32_t* vec_idx, float64_t* target,
00359             int32_t num_suppvec, int32_t* IDX, float64_t* weights);
00360 
00366         virtual void add_to_normal(int32_t idx, float64_t weight);
00367 
00369         virtual void clear_normal();
00370 
00376         virtual void compute_by_subkernel(
00377             int32_t idx, float64_t * subkernel_contrib);
00378 
00384         virtual const float64_t* get_subkernel_weights(int32_t& num_weights);
00385 
00391         virtual void get_subkernel_weights(float64_t** weights, int32_t* num_weights);
00392 
00398         virtual void set_subkernel_weights(
00399             float64_t* weights, int32_t num_weights);
00400 
00405         virtual void set_optimization_type(EOptimizationType t);
00406 
00408         bool precompute_subkernels();
00409 
00410     protected:
00417         virtual float64_t compute(int32_t x, int32_t y);
00418 
00424         inline void adjust_num_lhs_rhs_initialized(CKernel* k)
00425         {
00426             ASSERT(k);
00427 
00428             if (k->get_num_vec_lhs())
00429             {
00430                 if (num_lhs)
00431                     ASSERT(num_lhs==k->get_num_vec_lhs());
00432                 num_lhs=k->get_num_vec_lhs();
00433 
00434                 if (!get_num_subkernels())
00435                 {
00436                     initialized=true;
00437 
00438                 }
00439             }
00440             else
00441                 initialized=false;
00442 
00443             if (k->get_num_vec_rhs())
00444             {
00445                 if (num_rhs)
00446                     ASSERT(num_rhs==k->get_num_vec_rhs());
00447                 num_rhs=k->get_num_vec_rhs();
00448 
00449                 if (!get_num_subkernels())
00450                 {
00451                     initialized=true;
00452 
00453                 }
00454             }
00455             else
00456                 initialized=false;
00457         }
00458 
00459     protected:
00461         CList<CKernel*>* kernel_list;
00463         int32_t   sv_count;
00465         int32_t*  sv_idx;
00467         float64_t* sv_weight;
00469         float64_t* subkernel_weights_buffer;
00471         bool append_subkernel_weights;
00473         int32_t num_lhs;
00475         int32_t num_rhs;
00477         bool initialized;
00478 };
00479 
00480 #endif /* _COMBINEDKERNEL_H__ */

SHOGUN Machine Learning Toolbox - Documentation