-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3027 from tonmoy-saikia/refactor_cgauss
Separate out compact computation of gaussian into a subclass
- Loading branch information
Showing
7 changed files
with
122 additions
and
44 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
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); | ||
} |
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,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__ */ |
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
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