Skip to content

Commit

Permalink
Merge pull request #3027 from tonmoy-saikia/refactor_cgauss
Browse files Browse the repository at this point in the history
Separate out compact computation of gaussian into a subclass
  • Loading branch information
karlnapf committed Mar 4, 2016
2 parents e575d7b + 9b7cbf7 commit e7d2cfa
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 44 deletions.
1 change: 1 addition & 0 deletions src/interfaces/modular/Kernel.i
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ PROTOCOLS_CUSTOMKERNEL(CustomKernel, float32_t, "f\0", NPY_FLOAT32)
%rename(DistanceKernel) CDistanceKernel;
%rename(FixedDegreeStringKernel) CFixedDegreeStringKernel;
%rename(GaussianKernel) CGaussianKernel;
%rename(GaussianCompactKernel) CGaussianCompactKernel;
%rename(DirectorKernel) CDirectorKernel;
%rename(WaveletKernel) CWaveletKernel;
%rename(GaussianShiftKernel) CGaussianShiftKernel;
Expand Down
38 changes: 38 additions & 0 deletions src/shogun/kernel/GaussianCompactKernel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <shogun/kernel/GaussianCompactKernel.h>

using namespace shogun;

CGaussianCompactKernel::CGaussianCompactKernel() : CGaussianKernel()
{
}

CGaussianCompactKernel::CGaussianCompactKernel(int32_t size, float64_t width)
: CGaussianKernel(size, width)
{
}

CGaussianCompactKernel::CGaussianCompactKernel(CDotFeatures* l, CDotFeatures* r,
float64_t width, int32_t size)
: CGaussianKernel(l, r,
width, size)
{
}

CGaussianCompactKernel::~CGaussianCompactKernel()
{
}

float64_t CGaussianCompactKernel::compute(int32_t idx_a, int32_t idx_b)
{
int32_t len_features, power;
len_features=((CDotFeatures*) lhs)->get_dim_feature_space();
power=(len_features%2==0) ? (len_features+1):len_features;

float64_t result=distance(idx_a,idx_b);
float64_t result_multiplier=1-(CMath::sqrt(result))/3;

if(result_multiplier<=0)
return 0;

return CMath::pow(result_multiplier, power)*CMath::exp(-result);
}
79 changes: 79 additions & 0 deletions src/shogun/kernel/GaussianCompactKernel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#ifndef _GAUSSIANCOMPACTKERNEL_H___
#define _GAUSSIANCOMPACTKERNEL_H___

#include <shogun/kernel/GaussianKernel.h>

namespace shogun
{
class CDotFeatures;
/** @brief The compact version as given in Bart Hamers' thesis
* <i>Kernel Models for Large Scale Applications</i>
* (Eq. 4.10) is computed as
*
* \f[
* k({\bf x},{\bf x'})= max(0, (1-\frac{||{\bf x}-{\bf x'}||}{3\tau})^v)) *
* exp(-\frac{||{\bf x}-{\bf x'}||^2}{\tau})
* \f]
*
* where \f$\tau\f$ is the kernel width.
*
*/

class CGaussianCompactKernel: public CGaussianKernel
{
public:
/** default constructor */
CGaussianCompactKernel();

/** constructor
*
* @param size cache size
* @param width width
*/
CGaussianCompactKernel(int32_t size, float64_t width);

/** constructor
*
* @param l features of left-hand side
* @param r features of right-hand side
* @param width width
* @param size cache size
*/
CGaussianCompactKernel(CDotFeatures* l, CDotFeatures* r,
float64_t width, int32_t size=10);

/* destructor */
virtual ~CGaussianCompactKernel();

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

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

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 /* _GAUSSIANCOMPACTKERNEL_H__ */
23 changes: 2 additions & 21 deletions src/shogun/kernel/GaussianKernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,25 +117,8 @@ bool CGaussianKernel::init(CFeatures* l, CFeatures* r)

float64_t CGaussianKernel::compute(int32_t idx_a, int32_t idx_b)
{
if (!m_compact)
{
float64_t result=distance(idx_a,idx_b);
return CMath::exp(-result);
}

int32_t len_features, power;
len_features=((CDotFeatures*) lhs)->get_dim_feature_space();
power=(len_features%2==0) ? (len_features+1):len_features;

float64_t result=distance(idx_a,idx_b);
float64_t result_multiplier=1-(sqrt(result))/3;

if (result_multiplier<=0)
result_multiplier=0;
else
result_multiplier=pow(result_multiplier, power);

return result_multiplier*exp(-result);
float64_t result=distance(idx_a,idx_b);
return CMath::exp(-result);
}

void CGaussianKernel::load_serializable_post() throw (ShogunException)
Expand Down Expand Up @@ -185,11 +168,9 @@ SGMatrix<float64_t> CGaussianKernel::get_parameter_gradient(
void CGaussianKernel::init()
{
set_width(1.0);
set_compact_enabled(false);
sq_lhs=NULL;
sq_rhs=NULL;
SG_ADD(&m_log_width, "log_width", "Kernel width in log domain", MS_AVAILABLE, GRADIENT_AVAILABLE);
SG_ADD(&m_compact, "compact", "Compact enabled option", MS_AVAILABLE);
}

float64_t CGaussianKernel::distance(int32_t idx_a, int32_t idx_b)
Expand Down
23 changes: 0 additions & 23 deletions src/shogun/kernel/GaussianKernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,6 @@ namespace shogun
*
* where \f$\tau\f$ is the kernel width.
*
* The compact version as given in Bart Hamers' thesis <i>Kernel Models for
* Large Scale Applications</i> (Eq. 4.10) is computed as
*
* \f[
* k({\bf x},{\bf x'})= max(0, (1-\frac{||{\bf x}-{\bf x'}||}{3\tau})^v)) *
* exp(-\frac{||{\bf x}-{\bf x'}||^2}{\tau})
* \f]
*
* where \f$\tau\f$ is the kernel width.
*/
class CGaussianKernel: public CDotKernel
{
Expand Down Expand Up @@ -114,18 +105,6 @@ class CGaussianKernel: public CDotKernel
*/
virtual float64_t get_width() const;

/** set the compact option
*
* @param compact value of the compact option
*/
inline void set_compact_enabled(bool compact) { m_compact=compact; }

/** return value of the compact option
*
* @return whether the compact option is enabled
*/
inline bool get_compact_enabled() { return m_compact; }

/** return derivative with respect to specified parameter
*
* @param param the parameter
Expand Down Expand Up @@ -194,8 +173,6 @@ class CGaussianKernel: public CDotKernel
float64_t* sq_lhs;
/** squared right-hand side */
float64_t* sq_rhs;
/** whether compact output enabled */
bool m_compact;
};
}
#endif /* _GAUSSIANKERNEL_H__ */
1 change: 1 addition & 0 deletions src/shogun/kernel/Kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,7 @@ void CKernel::list_kernel()
ENUM_CASE(K_GAUSSIAN)
ENUM_CASE(K_GAUSSIANSHIFT)
ENUM_CASE(K_GAUSSIANMATCH)
ENUM_CASE(K_GAUSSIANCOMPACT)
ENUM_CASE(K_HISTOGRAM)
ENUM_CASE(K_SALZBERG)
ENUM_CASE(K_LOCALITYIMPROVED)
Expand Down
1 change: 1 addition & 0 deletions src/shogun/kernel/Kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ enum EKernelType
K_GAUSSIAN = 30,
K_GAUSSIANSHIFT = 32,
K_GAUSSIANMATCH = 33,
K_GAUSSIANCOMPACT = 34,
K_HISTOGRAM = 40,
K_SALZBERG = 41,
K_LOCALITYIMPROVED = 50,
Expand Down

0 comments on commit e7d2cfa

Please sign in to comment.