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

SHOGUN Machine Learning Toolbox - Documentation