Kernel.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-2008 Soeren Sonnenburg
00008  * Written (W) 1999-2008 Gunnar Raetsch
00009  * Copyright (C) 1999-2008 Fraunhofer Institute FIRST and Max-Planck-Society
00010  */
00011 
00012 #ifndef _KERNEL_H___
00013 #define _KERNEL_H___
00014 
00015 #include "lib/common.h"
00016 #include "base/SGObject.h"
00017 #include "features/Features.h"
00018 
00019 class CSVM;
00020 
00022 class CKernel : public CSGObject
00023 {
00024     public:
00029         CKernel(INT size);
00030 
00037         CKernel(CFeatures* l, CFeatures* r, INT size);
00038 
00039         virtual ~CKernel();
00040 
00048         inline DREAL kernel(INT idx_a, INT idx_b)
00049         {
00050             if (idx_a < 0 || idx_b <0)
00051                 return 0;
00052 
00053             ASSERT(lhs);
00054             ASSERT(rhs);
00055 
00056             if (lhs==rhs)
00057             {
00058                 INT num_vectors = lhs->get_num_vectors();
00059 
00060                 if (idx_a>=num_vectors)
00061                     idx_a=2*num_vectors-1-idx_a;
00062 
00063                 if (idx_b>=num_vectors)
00064                     idx_b=2*num_vectors-1-idx_b;
00065             }
00066 
00067             if (precompute_matrix && (precomputed_matrix==NULL) && (lhs==rhs))
00068                 do_precompute_matrix() ;
00069 
00070             if (precompute_matrix && (precomputed_matrix!=NULL))
00071             {
00072                 if (idx_a>=idx_b)
00073                     return precomputed_matrix[idx_a*(idx_a+1)/2+idx_b] ;
00074                 else
00075                     return precomputed_matrix[idx_b*(idx_b+1)/2+idx_a] ;
00076             }
00077 
00078             return compute(idx_a, idx_b);
00079         }
00080 
00087         void get_kernel_matrix(DREAL** dst, INT* m, INT* n);
00088 
00096         virtual DREAL* get_kernel_matrix_real(int &m, int &n, DREAL* target);
00097 
00105         virtual SHORTREAL* get_kernel_matrix_shortreal(int &m, int &n,
00106             SHORTREAL* target);
00107 
00118         virtual bool init(CFeatures* lhs, CFeatures* rhs);
00119 
00126         virtual void cleanup();
00127 
00133         bool load(CHAR* fname);
00134 
00140         bool save(CHAR* fname);
00141 
00149         virtual bool load_init(FILE* src)=0;
00150 
00158         virtual bool save_init(FILE* dest)=0;
00159 
00164         inline CFeatures* get_lhs() { SG_REF(lhs); return lhs; }
00165 
00170         inline CFeatures* get_rhs() { SG_REF(rhs); return rhs; }
00171 
00176         inline INT get_num_vec_lhs()
00177         {
00178             if (!lhs)
00179                 return 0;
00180             else
00181                 return lhs->get_num_vectors();
00182         }
00183 
00188         inline INT get_num_vec_rhs()
00189         {
00190             if (!rhs)
00191                 return 0;
00192             else
00193                 return rhs->get_num_vectors();
00194         }
00195 
00200         inline bool has_features()
00201         {
00202             return lhs && rhs;
00203         }
00204 
00206         virtual void remove_lhs_and_rhs();
00207 
00209         virtual void remove_lhs();
00210 
00212         virtual void remove_rhs();
00213 
00221         virtual EKernelType get_kernel_type()=0 ;
00222 
00229         virtual EFeatureType get_feature_type()=0;
00230 
00237         virtual EFeatureClass get_feature_class()=0;
00238 
00243         virtual const CHAR* get_name()=0 ;
00244 
00249         inline void set_cache_size(INT size)
00250         {
00251             cache_size = size;
00252 
00253         }
00254 
00259         inline int get_cache_size() { return cache_size; }
00260 
00261 
00262 
00264         void list_kernel();
00265 
00271         inline bool has_property(EKernelProperty p) { return (properties & p) != 0; }
00272 
00276         virtual void clear_normal();
00277 
00283         virtual void add_to_normal(INT vector_idx, DREAL weight);
00284 
00289         inline EOptimizationType get_optimization_type() { return opt_type; }
00290 
00295         virtual inline void set_optimization_type(EOptimizationType t) { opt_type=t;}
00296 
00301         inline bool get_is_initialized() { return optimization_initialized; }
00302 
00310         virtual bool init_optimization(INT count, INT *IDX, DREAL *weights);
00311 
00316         virtual bool delete_optimization();
00317 
00323         bool init_optimization_svm(CSVM * svm) ;
00324 
00330         virtual DREAL compute_optimized(INT vector_idx);
00331 
00340         virtual void compute_batch(INT num_vec, INT* vec_idx, DREAL* target, INT num_suppvec, INT* IDX, DREAL* alphas, DREAL factor=1.0);
00341 
00346         inline DREAL get_combined_kernel_weight() { return combined_kernel_weight; }
00347 
00352         inline void set_combined_kernel_weight(double nw) { combined_kernel_weight=nw; }
00353 
00358         virtual INT get_num_subkernels();
00359 
00365         virtual void compute_by_subkernel(INT vector_idx, DREAL * subkernel_contrib);
00366 
00372         virtual const DREAL* get_subkernel_weights(INT& num_weights);
00373 
00379         virtual void set_subkernel_weights(DREAL* weights, INT num_weights);
00380 
00381         //FIXME: precompute matrix should be dropped, handling should be via customkernel
00386         inline bool get_precompute_matrix() { return precompute_matrix ;  }
00387 
00392         inline bool get_precompute_subkernel_matrix() { return precompute_subkernel_matrix ;  }
00393 
00399         inline virtual void set_precompute_matrix(bool flag, bool subkernel_flag)
00400         {
00401             precompute_matrix = flag;
00402             precompute_subkernel_matrix = subkernel_flag;
00403 
00404             if (!precompute_matrix)
00405             {
00406                 delete[] precomputed_matrix;
00407                 precomputed_matrix = NULL;
00408             }
00409         }
00410 
00411     protected:
00416         inline void set_property(EKernelProperty p)
00417         {
00418             properties |= p;
00419         }
00420 
00425         inline void unset_property(EKernelProperty p)
00426         {
00427             properties &= (properties | p) ^ p;
00428         }
00429 
00434         inline void set_is_initialized(bool p_init) { optimization_initialized=p_init; }
00435 
00446         virtual DREAL compute(INT x, INT y)=0;
00447 
00449         void do_precompute_matrix();
00450 
00456         void init_sqrt_diag(DREAL *v, INT num);
00457 
00458 
00460 
00461 
00462     protected:
00464         INT cache_size;
00465 
00466 
00467 
00470         KERNELCACHE_ELEM* kernel_matrix;
00471 
00473         SHORTREAL * precomputed_matrix;
00475         bool precompute_subkernel_matrix;
00477         bool precompute_matrix ;
00478 
00480         CFeatures* lhs;
00482         CFeatures* rhs;
00483 
00485         DREAL combined_kernel_weight;
00486 
00488         bool optimization_initialized;
00492         EOptimizationType opt_type;
00493 
00495         ULONG  properties;
00496 };
00497 
00498 #endif /* _KERNEL_H__ */

SHOGUN Machine Learning Toolbox - Documentation