Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Jensen-Shanon kernel This patch adds a CDotKernel based Jensen-Sh…
…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
Showing
6 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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___ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters