Skip to content

Commit

Permalink
Add Jensen-Shanon kernel This patch adds a CDotKernel based Jensen-Sh…
Browse files Browse the repository at this point in the history
…annon kernel to shogun-toolbox. Sources of modular interface has been changed so it can be used via the modular interfaces as well.
  • Loading branch information
vigsterkr committed Feb 10, 2012
1 parent 1778466 commit e0a1155
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/interfaces/modular/Kernel.i
Expand Up @@ -86,6 +86,7 @@
%rename(DotKernel) CDotKernel;
%rename(RationalQuadraticKernel) CRationalQuadraticKernel;
%rename(MultiquadricKernel) CMultiquadricKernel;
%rename(JensenShannonKernel) CJensenShannonKernel;

/* Include Class Headers to make them visible from within the target language */
%include <shogun/kernel/Kernel.h>
Expand Down Expand Up @@ -200,3 +201,4 @@ namespace shogun
%include <shogun/kernel/ZeroMeanCenterKernelNormalizer.h>
%include <shogun/kernel/MultiquadricKernel.h>
%include <shogun/kernel/RationalQuadraticKernel.h>
%include <shogun/kernel/JensenShannonKernel.h>
1 change: 1 addition & 0 deletions src/interfaces/modular/Kernel_includes.i
Expand Up @@ -74,4 +74,5 @@
#include <shogun/kernel/ZeroMeanCenterKernelNormalizer.h>
#include <shogun/kernel/RationalQuadraticKernel.h>
#include <shogun/kernel/CircularKernel.h>
#include <shogun/kernel/JensenShannonKernel.h>
%}
78 changes: 78 additions & 0 deletions src/shogun/kernel/JensenShannonKernel.cpp
@@ -0,0 +1,78 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2012 Viktor Gal
* Copyright (C) 2012 Viktor Gal
*/

#include <shogun/lib/common.h>
#include <shogun/kernel/JensenShannonKernel.h>
#include <shogun/features/Features.h>
#include <shogun/features/SimpleFeatures.h>
#include <shogun/io/SGIO.h>

using namespace shogun;

CJensenShannonKernel::CJensenShannonKernel()
: CDotKernel(0)
{
}

CJensenShannonKernel::CJensenShannonKernel(int32_t size)
: CDotKernel(size)
{
}

CJensenShannonKernel::CJensenShannonKernel(
CSimpleFeatures<float64_t>* l, CSimpleFeatures<float64_t>* r, int32_t size)
: CDotKernel(size)
{
init(l,r);
}

CJensenShannonKernel::~CJensenShannonKernel()
{
cleanup();
}

bool CJensenShannonKernel::init(CFeatures* l, CFeatures* r)
{
bool result=CDotKernel::init(l,r);
init_normalizer();
return result;
}

float64_t CJensenShannonKernel::compute(int32_t idx_a, int32_t idx_b)
{
int32_t alen, blen;
bool afree, bfree;

float64_t* avec=
((CSimpleFeatures<float64_t>*) lhs)->get_feature_vector(idx_a, alen, afree);
float64_t* bvec=
((CSimpleFeatures<float64_t>*) rhs)->get_feature_vector(idx_b, blen, bfree);
ASSERT(alen==blen);

float64_t result=0;

/* calcualte Jensen-Shannon kernel */
for (int32_t i=0; i<alen; i++) {
float64_t a_i = 0, b_i = 0;
float64_t ab = avec[i]+bvec[i];
if (avec[i] != 0)
a_i = avec[i]/2 * log2(ab/avec[i]);
if (bvec[i] != 0)
b_i = bvec[i]/2 * log2(ab/bvec[i]);

result += a_i + b_i;
}

((CSimpleFeatures<float64_t>*) lhs)->free_feature_vector(avec, idx_a, afree);
((CSimpleFeatures<float64_t>*) rhs)->free_feature_vector(bvec, idx_b, bfree);

return result;
}

86 changes: 86 additions & 0 deletions src/shogun/kernel/JensenShannonKernel.h
@@ -0,0 +1,86 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2012 Viktor Gal
* Copyright (C) 2012 Viktor Gal
*/

#ifndef _JENSENSHANNONKERNEL_H___
#define _JENSENSHANNONKERNEL_H___

#include <shogun/lib/common.h>
#include <shogun/kernel/DotKernel.h>
#include <shogun/features/SimpleFeatures.h>

namespace shogun
{
/** @brief The Jensen-Shannon kernel operating on real-valued vectors computes
* the Jensen-Shannon distance between the features. Often used in computer vision.
*
* It is defined as
* \f[
* k({\bf x},({\bf x'})= \sum_{i=0}^{l} \frac{x_i}{2} \log_2\frac{x_i+x'_i}{x_i} + \frac{x'_i}{2} \log_2\frac{x_i+x'_i}{x'_i}
* \f]
* */
class CJensenShannonKernel: public CDotKernel
{
public:
/** default constructor */
CJensenShannonKernel();

/** constructor
*
* @param size cache size
*/
CJensenShannonKernel(int32_t size);

/** constructor
*
* @param l features of left-hand side
* @param r features of right-hand side
* @param size cache size
*/
CJensenShannonKernel(
CSimpleFeatures<float64_t>* l, CSimpleFeatures<float64_t>* r,
int32_t size=10);

virtual ~CJensenShannonKernel();

/** initialize kernel
*
* @param l features of left-hand side
* @param r features of right-hand side
* @return if initializing was successful
*/
virtual bool init(CFeatures* l, CFeatures* r);

/** return what type of kernel we are
*
* @return kernel type JENSENSHANNON
*/
virtual EKernelType get_kernel_type() { return K_JENSENSHANNON; }

/** return the kernel's name
*
* @return name JensenShannonKernel
*/
virtual const char* get_name() const { return "JensenShannonKernel"; }

protected:

/** compute kernel function for features a and b
* idx_{a,b} denote the index of the feature vectors
* in the corresponding feature object
*
* @param idx_a index a
* @param idx_b index b
* @return computed kernel function at indices a,b
*/
virtual float64_t compute(int32_t idx_a, int32_t idx_b);

};
}
#endif /* _JENSENSHANNONKERNEL_H___ */
1 change: 1 addition & 0 deletions src/shogun/kernel/Kernel.cpp
Expand Up @@ -751,6 +751,7 @@ void CKernel::list_kernel()
ENUM_CASE(K_SPECTRUMMISMATCHRBF)
ENUM_CASE(K_DISTANTSEGMENTS)
ENUM_CASE(K_BESSEL)
ENUM_CASE(K_JENSENSHANNON)
}

switch (get_feature_class())
Expand Down
1 change: 1 addition & 0 deletions src/shogun/kernel/Kernel.h
Expand Up @@ -107,6 +107,7 @@ enum EKernelType
K_INVERSEMULTIQUADRIC = 440,
K_DISTANTSEGMENTS = 450,
K_BESSEL = 460,
K_JENSENSHANNON = 470
};

/** kernel property */
Expand Down

0 comments on commit e0a1155

Please sign in to comment.