Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Gradient checking in the GradientModelSelection model. Added
ARD versions of Linear and Gaussian Kernels
- Loading branch information
puffin444
committed
Jul 12, 2012
1 parent
a46a5cf
commit 8246e94
Showing
8 changed files
with
614 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
* 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. | ||
* | ||
* (W) 2012 Jacob Walker | ||
* | ||
* Adapted from WeightedDegreeRBFKernel.cpp | ||
* | ||
*/ | ||
|
||
#include <shogun/lib/common.h> | ||
#include <shogun/kernel/GaussianARDKernel.h> | ||
#include <shogun/features/Features.h> | ||
#include <shogun/io/SGIO.h> | ||
|
||
using namespace shogun; | ||
|
||
CGaussianARDKernel::CGaussianARDKernel() | ||
: CDotKernel(), m_dimension(1), m_weights(0) | ||
{ | ||
init(); | ||
} | ||
|
||
|
||
CGaussianARDKernel::CGaussianARDKernel(int32_t size, float64_t width) | ||
: CDotKernel(size), m_dimension(0), m_weights(0), m_width(width) | ||
{ | ||
init(); | ||
} | ||
|
||
CGaussianARDKernel::CGaussianARDKernel(CDenseFeatures<float64_t>* l, | ||
CDenseFeatures<float64_t>* r, | ||
int32_t size, float64_t width) | ||
: CDotKernel(size), m_weights(0), m_width(width) | ||
{ | ||
init(); | ||
init(l,r); | ||
} | ||
|
||
void CGaussianARDKernel::init() | ||
{ | ||
m_weights = NULL; | ||
m_dimension = 0; | ||
m_width = 2.0; | ||
|
||
m_parameters->add_vector(&m_weights, &m_dimension, "weights"); | ||
SG_ADD(&m_width, "width", "Kernel Width", MS_AVAILABLE); | ||
} | ||
|
||
CGaussianARDKernel::~CGaussianARDKernel() | ||
{ | ||
SG_FREE(m_weights); | ||
m_weights=NULL; | ||
CKernel::cleanup(); | ||
} | ||
|
||
bool CGaussianARDKernel::init(CFeatures* l, CFeatures* r) | ||
{ | ||
CDotKernel::init(l, r); | ||
|
||
int32_t alen, blen; | ||
|
||
alen = ((CDenseFeatures<float64_t>*) lhs)->get_num_features(); | ||
blen = ((CDenseFeatures<float64_t>*) rhs)->get_num_features(); | ||
|
||
ASSERT(alen==blen); | ||
|
||
m_dimension = alen; | ||
|
||
SG_DEBUG("Initialized GaussianARDKernel (%p).\n", this); | ||
|
||
return (init_normalizer() && init_ft_weights()); | ||
} | ||
|
||
bool CGaussianARDKernel::init_ft_weights() | ||
{ | ||
ASSERT(m_dimension>0); | ||
|
||
if (m_weights!=0) | ||
SG_FREE(m_weights); | ||
|
||
m_weights=SG_MALLOC(float64_t, m_dimension); | ||
|
||
if (m_weights) | ||
{ | ||
for (index_t i=0; i<m_dimension; i++) | ||
m_weights[i]=1.0; | ||
|
||
SG_DEBUG("Initialized weights for GaussianARDKernel (%p).\n", this); | ||
return true; | ||
} | ||
|
||
else | ||
return false; | ||
} | ||
|
||
void CGaussianARDKernel::set_weight(float64_t w, index_t i) | ||
{ | ||
if (i > m_dimension-1) | ||
SG_ERROR("Index %i out of range for GaussianARDKernel."\ | ||
"Number of features is %i.\n", i, m_dimension); | ||
m_weights[i]=w; | ||
} | ||
|
||
float64_t CGaussianARDKernel::get_weight(index_t i) | ||
{ | ||
if (i > m_dimension-1) | ||
SG_ERROR("Index %i out of range for GaussianARDKernel."\ | ||
"Number of features is %i.\n", i, m_dimension); | ||
return m_weights[i]; | ||
} | ||
|
||
float64_t CGaussianARDKernel::compute(int32_t idx_a, int32_t idx_b) | ||
{ | ||
int32_t alen, blen; | ||
bool afree, bfree; | ||
|
||
float64_t* avec=((CDenseFeatures<float64_t>*) lhs)-> | ||
get_feature_vector(idx_a, alen, afree); | ||
float64_t* bvec=((CDenseFeatures<float64_t>*) rhs)-> | ||
get_feature_vector(idx_b, blen, bfree); | ||
ASSERT(alen==blen); | ||
|
||
float64_t result=0; | ||
|
||
for (index_t i = 0; i < m_dimension; i++) | ||
result += CMath::pow((avec[i]-bvec[i])*m_weights[i], 2); | ||
|
||
return CMath::exp(-result/m_width); | ||
} |
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,148 @@ | ||
/* | ||
* 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. | ||
* | ||
* (W) 2012 Jacob Walker | ||
* | ||
* Adapted from WeightedDegreeRBFKernel.h | ||
* | ||
*/ | ||
|
||
#ifndef GAUSSIANARDKERNEL_H_ | ||
#define GAUSSIANARDKERNEL_H_ | ||
|
||
#include <shogun/lib/common.h> | ||
#include <shogun/kernel/DotKernel.h> | ||
#include <shogun/features/DenseFeatures.h> | ||
|
||
namespace shogun { | ||
|
||
class CGaussianARDKernel: public CDotKernel { | ||
|
||
public: | ||
/** default constructor | ||
* | ||
*/ | ||
CGaussianARDKernel(); | ||
|
||
/** constructor | ||
* | ||
* @param size cache size | ||
*/ | ||
CGaussianARDKernel(int32_t size, float64_t width); | ||
|
||
/** constructor | ||
* | ||
* @param l features of left-hand side | ||
* @param r features of right-hand side | ||
* @param size cache size | ||
*/ | ||
CGaussianARDKernel(CDenseFeatures<float64_t>* l, CDenseFeatures<float64_t>* r, | ||
int32_t size=10, float64_t width = 2.0); | ||
|
||
virtual ~CGaussianARDKernel(); | ||
|
||
/** 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 LINEARARD | ||
*/ | ||
virtual EKernelType get_kernel_type() { return K_GAUSSIANARD; } | ||
|
||
/** return the kernel's name | ||
* | ||
* @return name LinearARDKernel | ||
*/ | ||
inline virtual const char* get_name() const { return "GaussianARDKernel"; } | ||
|
||
|
||
/** return feature class the kernel can deal with | ||
* | ||
* @return feature class SIMPLE | ||
*/ | ||
inline virtual EFeatureClass get_feature_class() { return C_DENSE; } | ||
|
||
/** return feature type the kernel can deal with | ||
* | ||
* @return float64_t feature type | ||
*/ | ||
virtual EFeatureType get_feature_type() { return F_DREAL; } | ||
|
||
/*Set weight of particular feature | ||
* | ||
* @param w weight to set | ||
* @param i index of feature | ||
*/ | ||
virtual void set_weight(float64_t w, index_t i); | ||
|
||
/*Get weight of particular feature | ||
* | ||
* @param i index of feature | ||
* | ||
* @return weight of feature | ||
*/ | ||
virtual float64_t get_weight(index_t i); | ||
|
||
/** set the kernel's width | ||
* | ||
* @param w kernel width | ||
*/ | ||
inline virtual void set_width(float64_t w) | ||
{ | ||
m_width = w; | ||
} | ||
|
||
/** return the kernel's width | ||
* | ||
* @return kernel width | ||
*/ | ||
inline virtual float64_t get_width() const | ||
{ | ||
return m_width; | ||
} | ||
|
||
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); | ||
|
||
/** init feature weights | ||
* | ||
* @return if initialization was successful | ||
*/ | ||
bool init_ft_weights(); | ||
|
||
private: | ||
|
||
void init(); | ||
|
||
protected: | ||
|
||
/** dimension */ | ||
index_t m_dimension; | ||
|
||
/** weights */ | ||
float64_t* m_weights; | ||
|
||
/* kernel width */ | ||
float64_t m_width; | ||
}; | ||
|
||
} /* namespace shogun */ | ||
#endif /* GAUSSIANARDKERNEL_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
Oops, something went wrong.