Skip to content

Commit

Permalink
minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
yorkerlin committed Feb 26, 2015
1 parent 1b01037 commit 23faae1
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 50 deletions.
14 changes: 9 additions & 5 deletions src/shogun/kernel/GaussianARDKernel.cpp
Expand Up @@ -12,37 +12,40 @@

#include <shogun/kernel/GaussianARDKernel.h>

#ifdef HAVE_LINALG_LIB
#include <shogun/mathematics/linalg/linalg.h>
#endif

using namespace shogun;

CGaussianARDKernel::CGaussianARDKernel() : CLinearARDKernel()
{
init();
initialize();
}

CGaussianARDKernel::~CGaussianARDKernel()
{
}

void CGaussianARDKernel::init()
void CGaussianARDKernel::initialize()
{
set_width(1.0);
SG_ADD(&m_width, "width", "Kernel width", MS_AVAILABLE, GRADIENT_AVAILABLE);
}

#ifdef HAVE_LINALG_LIB
#include <shogun/mathematics/linalg/linalg.h>
CGaussianARDKernel::CGaussianARDKernel(int32_t size, float64_t width)
: CLinearARDKernel(size)
{
init();
initialize();
set_width(width);
}

CGaussianARDKernel::CGaussianARDKernel(CDotFeatures* l,
CDotFeatures* r, int32_t size, float64_t width)
: CLinearARDKernel(size)
{
init();
initialize();
set_width(width);
}

Expand Down Expand Up @@ -115,6 +118,7 @@ SGMatrix<float64_t> CGaussianARDKernel::get_parameter_gradient(
float64_t CGaussianARDKernel::distance(int32_t idx_a, int32_t idx_b)
{
REQUIRE(rhs, "Right features (rhs) not set!\n")
REQUIRE(lhs, "Left features (lhs) not set!\n")

SGVector<float64_t> avec=((CDotFeatures *)lhs)->get_computed_dot_feature_vector(idx_a);
SGVector<float64_t> bvec=((CDotFeatures *)rhs)->get_computed_dot_feature_vector(idx_b);
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/kernel/GaussianARDKernel.h
Expand Up @@ -85,7 +85,7 @@ class CGaussianARDKernel: public CLinearARDKernel
*/
virtual const char* get_name() const { return "GaussianARDKernel"; }
private:
void init();
void initialize();

protected:
/** kernel width */
Expand Down
23 changes: 12 additions & 11 deletions src/shogun/kernel/LinearARDKernel.cpp
Expand Up @@ -11,19 +11,24 @@
*/

#include <shogun/kernel/LinearARDKernel.h>

#ifdef HAVE_LINALG_LIB
#include <shogun/mathematics/linalg/linalg.h>
#endif

using namespace shogun;

CLinearARDKernel::CLinearARDKernel() : CDotKernel()
{
init();
initialize();
}

CLinearARDKernel::~CLinearARDKernel()
{
CKernel::cleanup();
}

void CLinearARDKernel::init()
void CLinearARDKernel::initialize()
{
m_ARD_type=KT_SCALAR;
m_weights=SGMatrix<float64_t>(1,1);
Expand All @@ -33,22 +38,18 @@ void CLinearARDKernel::init()
}

#ifdef HAVE_LINALG_LIB
#include <shogun/mathematics/linalg/linalg.h>

CLinearARDKernel::CLinearARDKernel(int32_t size) : CDotKernel(size)
{
init();
initialize();
}

CLinearARDKernel::CLinearARDKernel(CDotFeatures* l,
CDotFeatures* r, int32_t size) : CDotKernel(size)
{
init();
initialize();
init(l,r);
}



bool CLinearARDKernel::init(CFeatures* l, CFeatures* r)
{
cleanup();
Expand Down Expand Up @@ -89,7 +90,7 @@ SGMatrix<float64_t> CLinearARDKernel::compute_right_product(SGVector<float64_t>r
else if(m_ARD_type==KT_FULL)
linalg::matrix_product(m_weights, rtmp, right);
else
SG_ERROR("Unsupported ARD kernel\n");
SG_ERROR("Unsupported ARD type\n");
}
return right;
}
Expand All @@ -116,7 +117,7 @@ float64_t CLinearARDKernel::compute_helper(SGVector<float64_t> avec, SGVector<fl
else if(m_ARD_type==KT_FULL)
linalg::matrix_product(m_weights, ltmp, left_transpose);
else
SG_ERROR("Unsupported ARD kernel\n");
SG_ERROR("Unsupported ARD type\n");
}

SGMatrix<float64_t> res(1,1);
Expand Down Expand Up @@ -174,7 +175,7 @@ float64_t CLinearARDKernel::compute_gradient_helper(SGVector<float64_t> avec,
}
else
{
SG_ERROR("Unsupported ARD kernel\n");
SG_ERROR("Unsupported ARD type\n");
}

}
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/kernel/LinearARDKernel.h
Expand Up @@ -96,7 +96,7 @@ class CLinearARDKernel: public CDotKernel
virtual EFeatureType get_feature_type() { return F_DREAL; }

private:
void init();
void initialize();

protected:
/** ARD weights */
Expand Down
45 changes: 18 additions & 27 deletions src/shogun/machine/gp/GaussianARDFITCKernel.cpp
Expand Up @@ -31,33 +31,36 @@

#include <shogun/machine/gp/GaussianARDFITCKernel.h>

#ifdef HAVE_LINALG_LIB
#include <shogun/mathematics/linalg/linalg.h>
#endif

using namespace shogun;

CGaussianARDFITCKernel::CGaussianARDFITCKernel() : CGaussianARDKernel()
{
init();
initialize();
}
void CGaussianARDFITCKernel::init()
void CGaussianARDFITCKernel::initialize()
{
}

CGaussianARDFITCKernel::~CGaussianARDFITCKernel()
{
}

#ifdef HAVE_CXX11
#include <shogun/mathematics/linalg/linalg.h>
#ifdef HAVE_LINALG_LIB
CGaussianARDFITCKernel::CGaussianARDFITCKernel(int32_t size, float64_t width)
: CGaussianARDKernel(size,width)
{
init();
initialize();
}

CGaussianARDFITCKernel::CGaussianARDFITCKernel(CDotFeatures* l,
CDotFeatures* r, int32_t size, float64_t width)
: CGaussianARDKernel(l, r, size, width)
{
init();
initialize();
}

CGaussianARDFITCKernel* CGaussianARDFITCKernel::obtain_from_generic(CKernel* kernel)
Expand All @@ -72,29 +75,18 @@ CGaussianARDFITCKernel* CGaussianARDFITCKernel::obtain_from_generic(CKernel* ker
return (CGaussianARDFITCKernel*)kernel;
}

#include <typeinfo>
CSGObject *CGaussianARDFITCKernel::shallow_copy() const
{
// TODO: remove this after all the classes get shallow_copy properly implemented
// this assert is to avoid any subclass of CGaussianARDFITCKernel accidentally called
// with the implement here
ASSERT(typeid(*this) == typeid(CGaussianARDFITCKernel))
CGaussianARDFITCKernel *ker = new CGaussianARDFITCKernel(cache_size, m_width);
if (lhs)
{
ker->CGaussianARDKernel::init(lhs, rhs);
}
return ker;
}

SGMatrix<float64_t> CGaussianARDFITCKernel::get_parameter_gradient(
const TParameter* param, index_t index)
{
if (!strcmp(param->m_name, "latent_features"))
{
REQUIRE(lhs && rhs, "Features not set!\n")
REQUIRE(index>=0 && index<num_lhs,"")
REQUIRE(lhs, "Left features not set!\n");
REQUIRE(rhs, "Right features not set!\n");
REQUIRE(index>=0 && index<num_lhs,"Index (%d) is out of bound (%d)\n",
index, num_rhs);
int32_t idx_l=index;
//Note that CDotKernel requires lhs and rhs are CDotFeatures pointers
//This Kernel is a subclass of CDotKernel
SGVector<float64_t> left_vec=((CDotFeatures *)lhs)->get_computed_dot_feature_vector(idx_l);
SGMatrix<float64_t> res(left_vec.vlen, num_rhs);

Expand Down Expand Up @@ -123,12 +115,10 @@ SGMatrix<float64_t> CGaussianARDFITCKernel::get_parameter_gradient(
linalg::elementwise_product(right_transpose, weights, res_transpose);
}
else if(m_ARD_type==KT_FULL)
{
linalg::matrix_product(right_transpose, m_weights, res_transpose);
}
else
{
SG_ERROR("Unsupported ARD kernel\n");
SG_ERROR("Unsupported ARD type\n");
}

}
Expand All @@ -143,4 +133,5 @@ SGMatrix<float64_t> CGaussianARDFITCKernel::get_parameter_gradient(
return CGaussianARDKernel::get_parameter_gradient(param, index);
}
}
#endif /* HAVE_CXX11 */
#endif /* HAVE_LINALG_LIB */

6 changes: 1 addition & 5 deletions src/shogun/machine/gp/GaussianARDFITCKernel.h
Expand Up @@ -69,7 +69,7 @@ class CGaussianARDFITCKernel: public CGaussianARDKernel
virtual ~CGaussianARDFITCKernel();

private:
void init();
void initialize();

#ifdef HAVE_CXX11
public:
Expand Down Expand Up @@ -97,10 +97,6 @@ class CGaussianARDFITCKernel: public CGaussianARDKernel
*/
static CGaussianARDFITCKernel* obtain_from_generic(CKernel* kernel);


/** Make a shallow copy of the kernel */
virtual CSGObject* shallow_copy() const;

/** return derivative with respect to specified parameter
*
* @param param the parameter
Expand Down

0 comments on commit 23faae1

Please sign in to comment.