SimpleLocalityImprovedStringKernel.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 Gunnar Raetsch
00008  * Copyright (C) 1999-2008 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #include "lib/common.h"
00012 #include "lib/io.h"
00013 #include "kernel/SimpleLocalityImprovedStringKernel.h"
00014 #include "features/Features.h"
00015 #include "features/StringFeatures.h"
00016 
00017 CSimpleLocalityImprovedStringKernel::CSimpleLocalityImprovedStringKernel(
00018     int32_t size, int32_t l, int32_t id, int32_t od)
00019 : CStringKernel<char>(size), length(l), inner_degree(id), outer_degree(od),
00020     pyramid_weights(NULL)
00021 {
00022 }
00023 
00024 CSimpleLocalityImprovedStringKernel::CSimpleLocalityImprovedStringKernel(
00025     CStringFeatures<char>* l, CStringFeatures<char>* r,
00026     int32_t len, int32_t id, int32_t od)
00027 : CStringKernel<char>(10), length(len), inner_degree(id), outer_degree(od),
00028     pyramid_weights(NULL)
00029 {
00030     init(l, r);
00031 }
00032 
00033 CSimpleLocalityImprovedStringKernel::~CSimpleLocalityImprovedStringKernel()
00034 {
00035     cleanup();
00036 }
00037 
00038 bool CSimpleLocalityImprovedStringKernel::init(CFeatures* l, CFeatures* r)
00039 {
00040     bool result = CStringKernel<char>::init(l,r);
00041 
00042     if (!result)
00043         return false;
00044     int32_t num_features = ((CStringFeatures<char>*) l)->get_max_vector_length();
00045     pyramid_weights = new float64_t[num_features];
00046     ASSERT(pyramid_weights);
00047     SG_INFO("initializing pyramid weights: size=%ld length=%i\n",
00048         num_features, length);
00049 
00050     const int32_t PYRAL = 2 * length - 1; // total window length
00051     float64_t PYRAL_pot;
00052     int32_t DEGREE1_1  = (inner_degree & 0x1)==0;
00053     int32_t DEGREE1_1n = (inner_degree & ~0x1)!=0;
00054     int32_t DEGREE1_2  = (inner_degree & 0x2)!=0;
00055     int32_t DEGREE1_3  = (inner_degree & ~0x3)!=0;
00056     int32_t DEGREE1_4  = (inner_degree & 0x4)!=0;
00057     {
00058     float64_t PYRAL_ = PYRAL;
00059     PYRAL_pot = DEGREE1_1 ? 1.0 : PYRAL_;
00060     if (DEGREE1_1n)
00061     {
00062         PYRAL_ *= PYRAL_;
00063         if (DEGREE1_2)
00064             PYRAL_pot *= PYRAL_;
00065         if (DEGREE1_3)
00066         {
00067             PYRAL_ *= PYRAL_;
00068             if (DEGREE1_4)
00069                 PYRAL_pot *= PYRAL_;
00070         }
00071     }
00072     }
00073 
00074     int32_t pyra_len  = num_features-PYRAL+1;
00075     int32_t pyra_len2 = (int32_t) pyra_len/2;
00076     {
00077     int32_t j;
00078     for (j = 0; j < pyra_len; j++)
00079         pyramid_weights[j] = 4*((float64_t)((j < pyra_len2)? j+1 : pyra_len-j))/((float64_t)pyra_len);
00080     for (j = 0; j < pyra_len; j++)
00081         pyramid_weights[j] /= PYRAL_pot;
00082     }
00083 
00084     return init_normalizer();
00085 }
00086 
00087 void CSimpleLocalityImprovedStringKernel::cleanup()
00088 {
00089     delete[] pyramid_weights;
00090     pyramid_weights = NULL;
00091 
00092     CKernel::cleanup();
00093 }
00094 
00095 bool CSimpleLocalityImprovedStringKernel::load_init(FILE* src)
00096 {
00097     return false;
00098 }
00099 
00100 bool CSimpleLocalityImprovedStringKernel::save_init(FILE* dest)
00101 {
00102     return false;
00103 }
00104 
00105 float64_t CSimpleLocalityImprovedStringKernel::dot_pyr (const char* const x1,
00106          const char* const x2, const int32_t NOF_NTS, const int32_t NTWIDTH,
00107          const int32_t DEGREE1, const int32_t DEGREE2, float64_t *pyra)
00108 {
00109     const int32_t PYRAL = 2*NTWIDTH-1; // total window length
00110     int32_t pyra_len, pyra_len2;
00111     float64_t pot, PYRAL_pot;
00112     float64_t sum;
00113     int32_t DEGREE1_1 = (DEGREE1 & 0x1)==0;
00114     int32_t DEGREE1_1n = (DEGREE1 & ~0x1)!=0;
00115     int32_t DEGREE1_2 = (DEGREE1 & 0x2)!=0;
00116     int32_t DEGREE1_3 = (DEGREE1 & ~0x3)!=0;
00117     int32_t DEGREE1_4 = (DEGREE1 & 0x4)!=0;
00118     {
00119     float64_t PYRAL_ = PYRAL;
00120     PYRAL_pot = DEGREE1_1 ? 1.0 : PYRAL_;
00121     if (DEGREE1_1n)
00122     {
00123         PYRAL_ *= PYRAL_;
00124         if (DEGREE1_2) PYRAL_pot *= PYRAL_;
00125         if (DEGREE1_3)
00126         {
00127             PYRAL_ *= PYRAL_;
00128             if (DEGREE1_4) PYRAL_pot *= PYRAL_;
00129         }
00130     }
00131     }
00132 
00133     ASSERT((DEGREE1 & ~0x7) == 0);
00134     ASSERT((DEGREE2 & ~0x7) == 0);
00135 
00136     pyra_len = NOF_NTS-PYRAL+1;
00137     pyra_len2 = (int32_t) pyra_len/2;
00138     {
00139     int32_t j;
00140     for (j = 0; j < pyra_len; j++)
00141         pyra[j] = 4*((float64_t)((j < pyra_len2) ? j+1 : pyra_len-j))/((float64_t)pyra_len);
00142     for (j = 0; j < pyra_len; j++)
00143         pyra[j] /= PYRAL_pot;
00144     }
00145 
00146     register int32_t conv;
00147     register int32_t i;
00148     register int32_t j;
00149 
00150     sum = 0.0;
00151     conv = 0;
00152     for (j = 0; j < PYRAL; j++)
00153         conv += (x1[j] == x2[j]) ? 1 : 0;
00154 
00155     for (i = 0; i < NOF_NTS-PYRAL+1; i++)
00156     {
00157         register float64_t pot2;
00158         if (i>0)
00159             conv += ((x1[i+PYRAL-1] == x2[i+PYRAL-1]) ? 1 : 0 ) - 
00160                 ((x1[i-1] == x2[i-1]) ? 1 : 0);
00161         { /* potencing of conv -- float64_t is faster*/
00162         register float64_t conv2 = conv;
00163         pot2 = (DEGREE1_1) ? 1.0 : conv2;
00164             if (DEGREE1_1n)
00165             {
00166                 conv2 *= conv2;
00167                 if (DEGREE1_2)
00168                     pot2 *= conv2;
00169                 if (DEGREE1_3 && DEGREE1_4)
00170                     pot2 *= conv2*conv2;
00171             }
00172         }
00173         sum += pot2*pyra[i];
00174     }
00175 
00176     pot = ((DEGREE2 & 0x1) == 0) ? 1.0 : sum;
00177     if ((DEGREE2 & ~0x1) != 0)
00178     {
00179         sum *= sum;
00180         if ((DEGREE2 & 0x2) != 0)
00181             pot *= sum;
00182         if ((DEGREE2 & ~0x3) != 0)
00183         {
00184             sum *= sum;
00185             if ((DEGREE2 & 0x4) != 0)
00186                 pot *= sum;
00187         }
00188     }
00189     return pot;
00190 }
00191 
00192 float64_t CSimpleLocalityImprovedStringKernel::compute(
00193     int32_t idx_a, int32_t idx_b)
00194 {
00195     int32_t alen, blen;
00196 
00197     char* avec = ((CStringFeatures<char>*) lhs)->get_feature_vector(idx_a, alen);
00198     char* bvec = ((CStringFeatures<char>*) rhs)->get_feature_vector(idx_b, blen);
00199 
00200     // can only deal with strings of same length
00201     ASSERT(alen==blen);
00202 
00203     float64_t dpt;
00204 
00205     dpt = dot_pyr(avec, bvec, alen, length, inner_degree, outer_degree, pyramid_weights);
00206     dpt = dpt / pow((float64_t)alen, (float64_t)outer_degree);
00207     return (float64_t) dpt;
00208 }

SHOGUN Machine Learning Toolbox - Documentation