From 0e9806ffa735a0fadb9bce99cdf76ae7a928fbeb Mon Sep 17 00:00:00 2001 From: Sergey Lisitsyn Date: Wed, 24 Oct 2012 01:06:57 +0400 Subject: [PATCH] Updated linear kernel API --- src/shogun/kernel/LinearKernel.cpp | 25 +++------------ src/shogun/kernel/LinearKernel.h | 51 ++++++------------------------ src/shogun/ui/SGInterface.cpp | 5 ++- 3 files changed, 17 insertions(+), 64 deletions(-) diff --git a/src/shogun/kernel/LinearKernel.cpp b/src/shogun/kernel/LinearKernel.cpp index 2dd221e1ff2..144d3554104 100644 --- a/src/shogun/kernel/LinearKernel.cpp +++ b/src/shogun/kernel/LinearKernel.cpp @@ -18,13 +18,13 @@ using namespace shogun; CLinearKernel::CLinearKernel() -: CDotKernel(0), normal(NULL), normal_length(0) +: CDotKernel(0) { properties |= KP_LINADD; } CLinearKernel::CLinearKernel(CDotFeatures* l, CDotFeatures* r) -: CDotKernel(0), normal(NULL), normal_length(0) +: CDotKernel(0) { properties |= KP_LINADD; init(l,r); @@ -49,24 +49,10 @@ void CLinearKernel::cleanup() CKernel::cleanup(); } -void CLinearKernel::clear_normal() -{ - int32_t num = ((CDotFeatures*) lhs)->get_dim_feature_space(); - if (normal==NULL) - { - normal = SG_MALLOC(float64_t, num); - normal_length=num; - } - - memset(normal, 0, sizeof(float64_t)*normal_length); - - set_is_initialized(true); -} - void CLinearKernel::add_to_normal(int32_t idx, float64_t weight) { ((CDotFeatures*) lhs)->add_to_dense_vec( - normalizer->normalize_lhs(weight, idx), idx, normal, normal_length); + normalizer->normalize_lhs(weight, idx), idx, normal.vector, normal.size()); set_is_initialized(true); } @@ -98,8 +84,7 @@ bool CLinearKernel::init_optimization(CKernelMachine* km) bool CLinearKernel::delete_optimization() { SG_FREE(normal); - normal_length=0; - normal=NULL; + normal = SGVector(); set_is_initialized(false); return true; @@ -109,6 +94,6 @@ float64_t CLinearKernel::compute_optimized(int32_t idx) { ASSERT(get_is_initialized()); float64_t result = ((CDotFeatures*) rhs)-> - dense_dot(idx, normal, normal_length); + dense_dot(idx, normal.vector, normal.size()); return normalizer->normalize_rhs(result, idx); } diff --git a/src/shogun/kernel/LinearKernel.h b/src/shogun/kernel/LinearKernel.h index 8c4d73bd414..2b46e49eb72 100644 --- a/src/shogun/kernel/LinearKernel.h +++ b/src/shogun/kernel/LinearKernel.h @@ -98,9 +98,6 @@ class CLinearKernel: public CDotKernel */ virtual float64_t compute_optimized(int32_t idx); - /** clear normal vector */ - virtual void clear_normal(); - /** add to normal vector * * @param idx where to add @@ -108,58 +105,30 @@ class CLinearKernel: public CDotKernel */ virtual void add_to_normal(int32_t idx, float64_t weight); - /** get normal - * - * @param len where length of normal vector will be stored - * @return normal vector - */ - inline const float64_t* get_normal(int32_t& len) - { - if (lhs && normal) - { - len = ((CDotFeatures*) lhs)->get_dim_feature_space(); - return normal; - } - else - { - len = 0; - return NULL; - } - } - - /** get normal vector (swig compatible) + /** get normal vector * * @param dst_w store w in this argument * @param dst_dims dimension of w */ - inline void get_w(float64_t** dst_w, int32_t* dst_dims) + SGVector get_w() const { - ASSERT(lhs && normal); - int32_t len = ((CDotFeatures*) lhs)->get_dim_feature_space(); - ASSERT(dst_w && dst_dims); - *dst_dims=len; - *dst_w=SG_MALLOC(float64_t, *dst_dims); - ASSERT(*dst_w); - memcpy(*dst_w, normal, sizeof(float64_t) * (*dst_dims)); + ASSERT(lhs); + return normal; } - /** set normal vector (swig compatible) + /** set normal vector * - * @param src_w new w - * @param src_w_dim dimension of new w - must fit dim of lhs + * @param w new normal */ - inline void set_w(float64_t* src_w, int32_t src_w_dim) + void set_w(SGVector w) { - ASSERT(lhs && src_w_dim==((CDotFeatures*) lhs)->get_dim_feature_space()); - clear_normal(); - memcpy(normal, src_w, sizeof(float64_t) * src_w_dim); + ASSERT(lhs && w.size()==((CDotFeatures*) lhs)->get_dim_feature_space()); + this->normal = w; } protected: /** normal vector (used in case of optimized kernel) */ - float64_t* normal; - /** length of normal vector */ - int32_t normal_length; + SGVector normal; }; } #endif /* _LINEARKERNEL_H__ */ diff --git a/src/shogun/ui/SGInterface.cpp b/src/shogun/ui/SGInterface.cpp index 3b7aad63659..c3dfeb695e1 100644 --- a/src/shogun/ui/SGInterface.cpp +++ b/src/shogun/ui/SGInterface.cpp @@ -4080,10 +4080,9 @@ bool CSGInterface::cmd_get_kernel_optimization() case K_LINEAR: { CLinearKernel* k=(CLinearKernel*) kernel; - int32_t len=0; - const float64_t* weights=k->get_normal(len); + SGVector weights=k->get_w(); - set_vector(weights, len); + set_vector(weights.vector, weights.size()); return true; } default: