SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
WeightedDegreeRBFKernel.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 1999-2009 Soeren Sonnenburg
8  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
11 #include <shogun/lib/common.h>
15 #include <shogun/io/SGIO.h>
16 
17 using namespace shogun;
18 
20 : CDotKernel(), width(1), degree(1), weights(0)
21 {
22 }
23 
24 
25 CWeightedDegreeRBFKernel::CWeightedDegreeRBFKernel(int32_t size, float64_t w, int32_t d, int32_t nof_prop)
26 : CDotKernel(size), width(w), degree(d), nof_properties(nof_prop), weights(0)
27 {
29 }
30 
32  CSimpleFeatures<float64_t>* l, CSimpleFeatures<float64_t>* r, float64_t w, int32_t d, int32_t nof_prop, int32_t size)
33 : CDotKernel(size), width(w), degree(d), nof_properties(nof_prop), weights(0)
34 {
36  init(l,r);
37 }
38 
40 {
42  weights=NULL;
43 }
44 
45 bool CWeightedDegreeRBFKernel::init(CFeatures* l, CFeatures* r)
46 {
47  CDotKernel::init(l, r);
48  SG_DEBUG("Initialized WeightedDegreeRBFKernel (%p).\n", this);
49  return init_normalizer();
50 }
51 
53 {
54  ASSERT(degree>0);
55 
56  if (weights!=0) SG_FREE(weights);
58  if (weights)
59  {
60  int32_t i;
61  float64_t sum=0;
62  for (i=0; i<degree; i++)
63  {
64  weights[i]=degree-i;
65  sum+=weights[i];
66  }
67  for (i=0; i<degree; i++)
68  weights[i]/=sum;
69 
70  SG_DEBUG("Initialized weights for WeightedDegreeRBFKernel (%p).\n", this);
71  return true;
72  }
73  else
74  return false;
75 }
76 
77 
78 float64_t CWeightedDegreeRBFKernel::compute(int32_t idx_a, int32_t idx_b)
79 {
80  int32_t alen, blen;
81  bool afree, bfree;
82 
83  float64_t* avec=((CSimpleFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
84  float64_t* bvec=((CSimpleFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
85  ASSERT(alen==blen);
86  ASSERT(alen%nof_properties == 0);
87 
88  float64_t result=0;
89 
90  for (int32_t i=0; i<alen; i+=nof_properties)
91  {
92  float64_t resulti = 0.0;
93 
94  for (int32_t d=0; (i+(d*nof_properties)<alen) && (d<degree); d++)
95  {
96  float64_t resultid = 0.0;
97  int32_t limit = (d + 1 ) * nof_properties;
98  for (int32_t k=0; k < limit; k++)
99  {
100  resultid+=CMath::sq(avec[i+k]-bvec[i+k]);
101  }
102 
103  resulti += weights[d] * exp(-resultid/width);
104  }
105 
106  result+=resulti ;
107  }
108 
109  return result;
110 }

SHOGUN Machine Learning Toolbox - Documentation