SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HistogramIntersectionKernel.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 Koen van de Sande
8  * Copyright (C) 2010 Koen van de Sande / University of Amsterdam
9  */
10 
11 #include <shogun/lib/common.h>
14 #include <shogun/io/SGIO.h>
15 
16 using namespace shogun;
17 
19 : CDotKernel(0), m_beta(1.0)
20 {
22 }
23 
25 : CDotKernel(size), m_beta(1.0)
26 {
28 }
29 
32  float64_t beta, int32_t size)
33 : CDotKernel(size), m_beta(beta)
34 {
35  init(l,r);
37 }
38 
40 {
41  cleanup();
42 }
43 
44 bool CHistogramIntersectionKernel::init(CFeatures* l, CFeatures* r)
45 {
46  bool result=CDotKernel::init(l,r);
48  return result;
49 }
50 
51 float64_t CHistogramIntersectionKernel::compute(int32_t idx_a, int32_t idx_b)
52 {
53  int32_t alen, blen;
54  bool afree, bfree;
55 
56  float64_t* avec=
57  ((CDenseFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
58  float64_t* bvec=
59  ((CDenseFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
60  ASSERT(alen==blen)
61 
62  float64_t result=0;
63 
64  // checking if beta is default or not
65  if (m_beta == 1.0)
66  {
67  // compute standard histogram intersection kernel
68  for (int32_t i=0; i<alen; i++)
69  result += (avec[i] < bvec[i]) ? avec[i] : bvec[i];
70  }
71  else
72  {
73  //compute generalized histogram intersection kernel
74  for (int32_t i=0; i<alen; i++)
75  result += CMath::min(CMath::pow(avec[i],m_beta), CMath::pow(bvec[i],m_beta));
76  }
77  ((CDenseFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
78  ((CDenseFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
79 
80  return result;
81 }
82 
84 {
85  SG_ADD(&m_beta, "beta", "the beta parameter of the kernel", MS_AVAILABLE);
86 }

SHOGUN Machine Learning Toolbox - Documentation