WeightedDegreeRBFKernel.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-2009 Soeren Sonnenburg
00008  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #include "lib/common.h"
00012 #include "kernel/WeightedDegreeRBFKernel.h"
00013 #include "features/Features.h"
00014 #include "features/SimpleFeatures.h"
00015 #include "lib/io.h"
00016 
00017 #ifdef HAVE_BOOST_SERIALIZATION
00018 #include <boost/serialization/export.hpp>
00019 BOOST_CLASS_EXPORT(shogun::CWeightedDegreeRBFKernel);
00020 #endif //HAVE_BOOST_SERIALIZATION
00021 
00022 using namespace shogun;
00023 
00024 CWeightedDegreeRBFKernel::CWeightedDegreeRBFKernel()
00025 : CSimpleKernel<float64_t>(), width(1), degree(1), weights(0)
00026 {
00027 }
00028 
00029 
00030 CWeightedDegreeRBFKernel::CWeightedDegreeRBFKernel(int32_t size, float64_t w, int32_t d, int32_t nof_prop)
00031 : CSimpleKernel<float64_t>(size), width(w), degree(d), nof_properties(nof_prop), weights(0)
00032 {
00033     init_wd_weights();
00034 }
00035 
00036 CWeightedDegreeRBFKernel::CWeightedDegreeRBFKernel(
00037     CSimpleFeatures<float64_t>* l, CSimpleFeatures<float64_t>* r, float64_t w, int32_t d, int32_t nof_prop, int32_t size)
00038 : CSimpleKernel<float64_t>(size), width(w), degree(d), nof_properties(nof_prop), weights(0)
00039 {
00040     init_wd_weights();
00041     init(l,r);
00042 }
00043 
00044 CWeightedDegreeRBFKernel::~CWeightedDegreeRBFKernel()
00045 {
00046     delete[] weights;
00047     weights=NULL;
00048 }
00049 
00050 bool CWeightedDegreeRBFKernel::init(CFeatures* l, CFeatures* r)
00051 {
00052     CSimpleKernel<float64_t>::init(l, r);
00053     SG_DEBUG("Initialized WeightedDegreeRBFKernel (%p).\n", this);
00054     return init_normalizer();
00055 }
00056 
00057 bool CWeightedDegreeRBFKernel::init_wd_weights()
00058 {
00059     ASSERT(degree>0);
00060 
00061     if (weights!=0) delete[] weights;
00062     weights=new float64_t[degree];
00063     if (weights)
00064     {
00065         int32_t i;
00066         float64_t sum=0;
00067         for (i=0; i<degree; i++)
00068         {
00069             weights[i]=degree-i;
00070             sum+=weights[i];
00071         }
00072         for (i=0; i<degree; i++)
00073             weights[i]/=sum;
00074 
00075         SG_DEBUG("Initialized weights for WeightedDegreeRBFKernel (%p).\n", this);
00076         return true;
00077     }
00078     else
00079         return false;
00080 }
00081 
00082 
00083 float64_t CWeightedDegreeRBFKernel::compute(int32_t idx_a, int32_t idx_b)
00084 {
00085     int32_t alen, blen;
00086     bool afree, bfree;
00087 
00088     float64_t* avec=((CSimpleFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
00089     float64_t* bvec=((CSimpleFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
00090     ASSERT(alen==blen);
00091     ASSERT(alen%nof_properties == 0);
00092 
00093     float64_t result=0;
00094 
00095     for (int32_t i=0; i<alen; i+=nof_properties)
00096     {
00097         float64_t resulti = 0.0;
00098 
00099         for (int32_t d=0; (i+(d*nof_properties)<alen) && (d<degree); d++)
00100         {
00101             float64_t resultid = 0.0;
00102             int32_t limit = (d + 1 ) * nof_properties;
00103             for (int32_t k=0; k < limit; k++)
00104             {
00105                 resultid+=CMath::sq(avec[i+k]-bvec[i+k]);
00106             }
00107             
00108             resulti += weights[d] * exp(-resultid/width);
00109         }
00110     
00111         result+=resulti ;
00112     }
00113 
00114     return result;
00115 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation