SHOGUN  5.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
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>
14 #include <shogun/io/SGIO.h>
15 
16 using namespace shogun;
17 
19 : CDotKernel(), width(1), degree(1), weights(0)
20 {
21  register_params();
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  register_params();
30 }
31 
33  CDenseFeatures<float64_t>* l, CDenseFeatures<float64_t>* r, float64_t w, int32_t d, int32_t nof_prop, int32_t size)
34 : CDotKernel(size), width(w), degree(d), nof_properties(nof_prop), weights(0)
35 {
37  register_params();
38  init(l,r);
39 }
40 
42 {
43  SG_FREE(weights);
44  weights=NULL;
45 }
46 
47 bool CWeightedDegreeRBFKernel::init(CFeatures* l, CFeatures* r)
48 {
49  CDotKernel::init(l, r);
50  SG_DEBUG("Initialized WeightedDegreeRBFKernel (%p).\n", this)
51  return init_normalizer();
52 }
53 
55 {
56  ASSERT(degree>0)
57 
58  if (weights!=0) SG_FREE(weights);
59  weights=SG_MALLOC(float64_t, degree);
60  if (weights)
61  {
62  int32_t i;
63  float64_t sum=0;
64  for (i=0; i<degree; i++)
65  {
66  weights[i]=degree-i;
67  sum+=weights[i];
68  }
69  for (i=0; i<degree; i++)
70  weights[i]/=sum;
71 
72  SG_DEBUG("Initialized weights for WeightedDegreeRBFKernel (%p).\n", this)
73  return true;
74  }
75  else
76  return false;
77 }
78 
79 
80 float64_t CWeightedDegreeRBFKernel::compute(int32_t idx_a, int32_t idx_b)
81 {
82  int32_t alen, blen;
83  bool afree, bfree;
84 
85  float64_t* avec=((CDenseFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
86  float64_t* bvec=((CDenseFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
87  ASSERT(alen==blen)
88  ASSERT(alen%nof_properties == 0)
89 
90  float64_t result=0;
91 
92  for (int32_t i=0; i<alen; i+=nof_properties)
93  {
94  float64_t resulti = 0.0;
95 
96  for (int32_t d=0; (i+(d*nof_properties)<alen) && (d<degree); d++)
97  {
98  float64_t resultid = 0.0;
99  int32_t limit = (d + 1 ) * nof_properties;
100  for (int32_t k=0; k < limit; k++)
101  {
102  resultid+=CMath::sq(avec[i+k]-bvec[i+k]);
103  }
104 
105  resulti += weights[d] * exp(-resultid/width);
106  }
107 
108  result+=resulti ;
109  }
110 
111  return result;
112 }
113 
114 void CWeightedDegreeRBFKernel::register_params()
115 {
116  SG_ADD(&width, "width", "Kernel width", MS_AVAILABLE);
117  SG_ADD(&degree, "degree", "Kernel degree", MS_AVAILABLE);
118 }
static T sq(T x)
Definition: Math.h:450
Template class DotKernel is the base class for kernels working on DotFeatures.
Definition: DotKernel.h:31
virtual float64_t compute(int32_t idx_a, int32_t idx_b)
#define ASSERT(x)
Definition: SGIO.h:201
double float64_t
Definition: common.h:50
virtual bool init_normalizer()
Definition: Kernel.cpp:168
CFeatures * rhs
feature vectors to occur on right hand side
Definition: Kernel.h:1062
#define SG_DEBUG(...)
Definition: SGIO.h:107
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
CFeatures * lhs
feature vectors to occur on left hand side
Definition: Kernel.h:1060
The class Features is the base class of all feature objects.
Definition: Features.h:68
virtual bool init(CFeatures *l, CFeatures *r)
#define SG_ADD(...)
Definition: SGObject.h:84

SHOGUN Machine Learning Toolbox - Documentation