SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
KernelDistance.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) 2010 Christian Widmer
8  * Copyright (C) 2010 Max-Planck-Society
9  */
10 
11 #include <shogun/lib/config.h>
12 
13 #include <shogun/lib/common.h>
14 #include <shogun/io/SGIO.h>
17 
18 using namespace shogun;
19 
21 {
22  init();
23 }
24 
26 : CDistance()
27 {
28  init();
29 
30  kernel=k;
31  width=w;
32  ASSERT(kernel)
33  SG_REF(kernel);
34 }
35 
37  CFeatures *l, CFeatures *r, float64_t w , CKernel* k)
38 : CDistance()
39 {
40  init();
41 
42  kernel=k;
43  width=w;
44  ASSERT(kernel)
45  SG_REF(kernel);
46 
47  init(l, r);
48 }
49 
51 {
52  // important to have the cleanup of CDistance first, it calls get_name which
53  // uses the distance
54  cleanup();
55  SG_UNREF(kernel);
56 }
57 
58 bool CKernelDistance::init(CFeatures* l, CFeatures* r)
59 {
60  ASSERT(kernel)
61  kernel->init(l,r);
62  return CDistance::init(l,r);
63 }
64 
65 float64_t CKernelDistance::compute(int32_t idx_a, int32_t idx_b)
66 {
67  float64_t result=kernel->kernel(idx_a, idx_b);
68  return exp(-result/width);
69 }
70 
71 void CKernelDistance::init()
72 {
73  kernel = NULL;
74  width = 0.0;
75 
76  m_parameters->add(&width, "width", "Width of RBF Kernel");
77  m_parameters->add((CSGObject**) &kernel, "kernel",
78  "Kernel.");
79 }
virtual bool init(CFeatures *lhs, CFeatures *rhs)
Definition: Kernel.cpp:98
Class Distance, a base class for all the distances used in the Shogun toolbox.
Definition: Distance.h:87
Parameter * m_parameters
Definition: SGObject.h:546
float64_t kernel(int32_t idx_a, int32_t idx_b)
Definition: Kernel.h:207
virtual void cleanup()
#define SG_REF(x)
Definition: SGObject.h:54
float64_t compute(int32_t idx_a, int32_t idx_b)
void add(bool *param, const char *name, const char *description="")
Definition: Parameter.cpp:37
#define ASSERT(x)
Definition: SGIO.h:201
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:115
double float64_t
Definition: common.h:50
#define SG_UNREF(x)
Definition: SGObject.h:55
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
The class Features is the base class of all feature objects.
Definition: Features.h:68
The Kernel base class.
Definition: Kernel.h:159
virtual bool init(CFeatures *l, CFeatures *r)

SHOGUN Machine Learning Toolbox - Documentation