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

SHOGUN Machine Learning Toolbox - Documentation