SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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>
15 #include <shogun/io/SGIO.h>
16 
17 using namespace shogun;
18 
20 : CDotKernel(0), m_beta(1.0)
21 {
23 }
24 
26 : CDotKernel(size), m_beta(1.0)
27 {
29 }
30 
33  float64_t beta, int32_t size)
34 : CDotKernel(size), m_beta(1.0)
35 {
36  init(l,r);
38 }
39 
41 {
42  cleanup();
43 }
44 
45 bool CHistogramIntersectionKernel::init(CFeatures* l, CFeatures* r)
46 {
47  bool result=CDotKernel::init(l,r);
49  return result;
50 }
51 
52 float64_t CHistogramIntersectionKernel::compute(int32_t idx_a, int32_t idx_b)
53 {
54  int32_t alen, blen;
55  bool afree, bfree;
56 
57  float64_t* avec=
58  ((CSimpleFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
59  float64_t* bvec=
60  ((CSimpleFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
61  ASSERT(alen==blen);
62 
63  float64_t result=0;
64 
65  // checking if beta is default or not
66  if (m_beta == 1.0)
67  {
68  // compute standard histogram intersection kernel
69  for (int32_t i=0; i<alen; i++)
70  result += (avec[i] < bvec[i]) ? avec[i] : bvec[i];
71  }
72  else
73  {
74  //compute generalized histogram intersection kernel
75  for (int32_t i=0; i<alen; i++)
76  result += CMath::min(CMath::pow(avec[i],m_beta), CMath::pow(bvec[i],m_beta));
77  }
78  ((CSimpleFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
79  ((CSimpleFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);
80 
81  return result;
82 }
83 
85 {
86  m_parameters->add(&m_beta, "beta", "the beta parameter of the kernel");
87 }

SHOGUN Machine Learning Toolbox - Documentation