PolyKernel.cpp

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  * Copyright (C) 1999-2008 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #include "lib/config.h"
00012 #include "lib/common.h"
00013 #include "lib/io.h"
00014 #include "kernel/PolyKernel.h"
00015 #include "features/RealFeatures.h"
00016 
00017 CPolyKernel::CPolyKernel(INT size, INT d, bool i, bool un)
00018 : CSimpleKernel<DREAL>(size), degree(d), inhomogene(i),
00019     use_normalization(un), sqrtdiag_lhs(NULL), sqrtdiag_rhs(NULL),
00020     initialized(false)
00021 {
00022 }
00023 
00024 CPolyKernel::CPolyKernel(CRealFeatures* l, CRealFeatures* r, INT d, bool i, bool un, INT size)
00025 : CSimpleKernel<DREAL>(size),degree(d),inhomogene(i), use_normalization(un),
00026     sqrtdiag_lhs(NULL), sqrtdiag_rhs(NULL), initialized(false)
00027 {
00028     init(l,r);
00029 }
00030 
00031 CPolyKernel::~CPolyKernel()
00032 {
00033     cleanup();
00034 }
00035 
00036 bool CPolyKernel::init(CFeatures* l, CFeatures* r)
00037 {
00038     bool result=CSimpleKernel<DREAL>::init(l,r);
00039 
00040     initialized = false ;
00041     INT i;
00042 
00043     if (sqrtdiag_lhs!=sqrtdiag_rhs)
00044       delete[] sqrtdiag_rhs;
00045     sqrtdiag_rhs=NULL;
00046     delete[] sqrtdiag_lhs;
00047     sqrtdiag_lhs=NULL;
00048 
00049     if (use_normalization)
00050     {
00051         sqrtdiag_lhs=new DREAL[lhs->get_num_vectors()];
00052 
00053         for (i=0; i<lhs->get_num_vectors(); i++)
00054             sqrtdiag_lhs[i]=1;
00055 
00056         if (l==r)
00057             sqrtdiag_rhs=sqrtdiag_lhs;
00058         else
00059         {
00060             sqrtdiag_rhs=new DREAL[rhs->get_num_vectors()];
00061             for (i=0; i<rhs->get_num_vectors(); i++)
00062                 sqrtdiag_rhs[i]=1;
00063         }
00064 
00065         this->lhs=(CRealFeatures*) l;
00066         this->rhs=(CRealFeatures*) l;
00067 
00068         //compute normalize to 1 values
00069         for (i=0; i<lhs->get_num_vectors(); i++)
00070         {
00071             sqrtdiag_lhs[i]=sqrt(compute(i,i));
00072 
00073             //trap divide by zero exception
00074             if (sqrtdiag_lhs[i]==0)
00075                 sqrtdiag_lhs[i]=1e-16;
00076         }
00077 
00078         // if lhs is different from rhs (train/test data)
00079         // compute also the normalization for rhs
00080         if (sqrtdiag_lhs!=sqrtdiag_rhs)
00081         {
00082             this->lhs=(CRealFeatures*) r;
00083             this->rhs=(CRealFeatures*) r;
00084 
00085             //compute normalize to 1 values
00086             for (i=0; i<rhs->get_num_vectors(); i++)
00087             {
00088                 sqrtdiag_rhs[i]=sqrt(compute(i,i));
00089 
00090                 //trap divide by zero exception
00091                 if (sqrtdiag_rhs[i]==0)
00092                     sqrtdiag_rhs[i]=1e-16;
00093             }
00094         }
00095     }
00096 
00097     this->lhs=(CRealFeatures*) l;
00098     this->rhs=(CRealFeatures*) r;
00099 
00100     initialized = true;
00101     return result;
00102 }
00103 
00104 void CPolyKernel::cleanup()
00105 {
00106     if (sqrtdiag_lhs != sqrtdiag_rhs)
00107         delete[] sqrtdiag_rhs;
00108     sqrtdiag_rhs=NULL;
00109 
00110     delete[] sqrtdiag_lhs;
00111     sqrtdiag_lhs=NULL;
00112 
00113     initialized=false;
00114     CKernel::cleanup();
00115 }
00116 
00117 bool CPolyKernel::load_init(FILE* src)
00118 {
00119     return false;
00120 }
00121 
00122 bool CPolyKernel::save_init(FILE* dest)
00123 {
00124     return false;
00125 }
00126   
00127 DREAL CPolyKernel::compute(INT idx_a, INT idx_b)
00128 {
00129   INT alen=0;
00130   INT blen=0;
00131   bool afree=false;
00132   bool bfree=false;
00133 
00134   double* avec=((CRealFeatures*) lhs)->get_feature_vector(idx_a, alen, afree);
00135   double* bvec=((CRealFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree);
00136   ASSERT(alen==blen);
00137 
00138   DREAL sqrt_a= 1.0;
00139   DREAL sqrt_b= 1.0;
00140   if (initialized && use_normalization)
00141   {
00142       sqrt_a=sqrtdiag_lhs[idx_a] ;
00143       sqrt_b=sqrtdiag_rhs[idx_b] ;
00144   }
00145 
00146   DREAL sqrt_both=sqrt_a*sqrt_b;
00147 
00148   DREAL result=CMath::dot(avec, bvec, alen);
00149 
00150   if (inhomogene)
00151       result+=1;
00152 
00153   DREAL re=result;
00154 
00155   for (INT j=1; j<degree; j++)
00156       result*=re;
00157 
00158   ((CRealFeatures*) lhs)->free_feature_vector(avec, idx_a, afree);
00159   ((CRealFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree);
00160 
00161   return result/sqrt_both;
00162 }

SHOGUN Machine Learning Toolbox - Documentation