diff --git a/src/shogun/kernel/ANOVAKernel.cpp b/src/shogun/kernel/ANOVAKernel.cpp index b0f597ef6d4..8f84bec3c02 100644 --- a/src/shogun/kernel/ANOVAKernel.cpp +++ b/src/shogun/kernel/ANOVAKernel.cpp @@ -7,26 +7,27 @@ #include #include -#include using namespace shogun; -ANOVAKernel::ANOVAKernel(): DotKernel(0), cardinality(1.0) +ANOVAKernel::ANOVAKernel(): DotKernel(0) { - register_params(); + SG_ADD( + &m_cardinality, "cardinality", "Kernel cardinality.", + ParameterProperties::HYPER); } -ANOVAKernel::ANOVAKernel(int32_t cache, int32_t d) -: DotKernel(cache), cardinality(d) +ANOVAKernel::ANOVAKernel(int32_t size, int32_t d) +: ANOVAKernel() { - register_params(); + set_cache_size(size); + m_cardinality = d; } ANOVAKernel::ANOVAKernel( const std::shared_ptr>& l, const std::shared_ptr>& r, int32_t d, int32_t cache) - : DotKernel(cache), cardinality(d) + : ANOVAKernel(cache, d) { - register_params(); init(l, r); } @@ -88,20 +89,14 @@ float64_t ANOVAKernel::compute_rec2(int32_t idx_a, int32_t idx_b) return result; } -void ANOVAKernel::register_params() -{ - SG_ADD(&cardinality, "cardinality", "Kernel cardinality.", ParameterProperties::HYPER); -} - - float64_t ANOVAKernel::compute_recursive1(float64_t* avec, float64_t* bvec, int32_t len) { - int32_t DP_len=(cardinality+1)*(len+1); + int32_t DP_len=(m_cardinality+1)*(len+1); float64_t* DP = SG_MALLOC(float64_t, DP_len); ASSERT(DP) - int32_t d=cardinality; - int32_t offs=cardinality+1; + int32_t d=m_cardinality; + int32_t offs=m_cardinality+1; ASSERT(DP_len==(len+1)*offs) @@ -128,15 +123,15 @@ float64_t ANOVAKernel::compute_recursive1(float64_t* avec, float64_t* bvec, int3 float64_t ANOVAKernel::compute_recursive2(float64_t* avec, float64_t* bvec, int32_t len) { - float64_t* KD = SG_MALLOC(float64_t, cardinality+1); - float64_t* KS = SG_MALLOC(float64_t, cardinality+1); + float64_t* KD = SG_MALLOC(float64_t, m_cardinality+1); + float64_t* KS = SG_MALLOC(float64_t, m_cardinality+1); float64_t* vec_pow = SG_MALLOC(float64_t, len); ASSERT(vec_pow) ASSERT(KS) ASSERT(KD) - int32_t d=cardinality; + int32_t d=m_cardinality; for (int32_t i=0; i < len; i++) vec_pow[i] = 1; @@ -171,18 +166,4 @@ float64_t ANOVAKernel::compute_recursive2(float64_t* avec, float64_t* bvec, int3 SG_FREE(KD); return result; -} - -std::shared_ptr ANOVAKernel::obtain_from_generic(const std::shared_ptr& kernel) -{ - if (!kernel) - return NULL; - - require(kernel->get_kernel_type()==K_ANOVA, "Provided kernel is " - "not of type CANOVAKernel, but type {}!", - kernel->get_kernel_type()); - - /* since an additional reference is returned */ - - return std::static_pointer_cast(kernel); -} +} \ No newline at end of file diff --git a/src/shogun/kernel/ANOVAKernel.h b/src/shogun/kernel/ANOVAKernel.h index 98c450daac5..1411f1c5e0b 100644 --- a/src/shogun/kernel/ANOVAKernel.h +++ b/src/shogun/kernel/ANOVAKernel.h @@ -34,7 +34,7 @@ class Distance; class ANOVAKernel: public DotKernel { public: - /** default constructor */ + ANOVAKernel(); /** constructor @@ -54,42 +54,25 @@ class ANOVAKernel: public DotKernel virtual ~ANOVAKernel(); - /** initialize kernel with features - * @param l features left-side - * @param r features right-side - * @return true if successful - */ virtual bool init(std::shared_ptr l, std::shared_ptr r); - /** - * @return kernel type - */ virtual EKernelType get_kernel_type() { return K_ANOVA; } - /** - * @return type of features - */ virtual EFeatureType get_feature_type() { return F_DREAL; } - /** - * @return class of features - */ virtual EFeatureClass get_feature_class() { return C_DENSE; } - /** - * @return name of kernel - */ virtual const char* get_name() const { return "ANOVAKernel"; } /** getter for degree parameter * @return kernel parameter cardinality */ - inline int32_t get_cardinality() { return this->cardinality; } + int32_t get_cardinality() { return this->m_cardinality; } /** setter for degree parameter * @param value kernel parameter cardinality */ - inline void set_cardinality(int32_t value) { this->cardinality = value; } + void set_cardinality(int32_t value) { this->m_cardinality = value; } /** compute rec 1 * @param idx_a @@ -105,32 +88,16 @@ class ANOVAKernel: public DotKernel */ float64_t compute_rec2(int32_t idx_a, int32_t idx_b); - /** Casts the given kernel to CANOVAKernel. - * @param kernel Kernel to cast. Must be CANOVAKernel. Might be NULL - * @return casted CANOVAKernel object, NULL if input was NULL - */ - static std::shared_ptr obtain_from_generic(const std::shared_ptr& kernel); -protected: - - /** - * compute kernel for specific feature vectors - * corresponding to [idx_a] of left-side and [idx_b] of right-side - * @param idx_a left-side index - * @param idx_b right-side index - * @return kernel value - */ - virtual float64_t compute(int32_t idx_a, int32_t idx_b); - - /** register params */ - void register_params(); - private: float64_t compute_recursive1(float64_t* avec, float64_t* bvec, int32_t len); float64_t compute_recursive2(float64_t* avec, float64_t* bvec, int32_t len); +protected: + virtual float64_t compute(int32_t idx_a, int32_t idx_b); + protected: /// degree parameter of kernel - int32_t cardinality; + int32_t m_cardinality = 1; }; } diff --git a/src/shogun/kernel/AUCKernel.cpp b/src/shogun/kernel/AUCKernel.cpp index fb0ec2a7e43..05d14bfe87e 100644 --- a/src/shogun/kernel/AUCKernel.cpp +++ b/src/shogun/kernel/AUCKernel.cpp @@ -15,23 +15,20 @@ using namespace shogun; -void AUCKernel::init() +AUCKernel::AUCKernel() : DotKernel(0) { SG_ADD( - &subkernel, "subkernel", "The subkernel.", ParameterProperties::HYPER); - SG_ADD(&labels, "labels", "The labels."); + &m_subkernel, "subkernel", "The subkernel.", ParameterProperties::HYPER); + SG_ADD(&m_labels, "labels", "The labels."); watch_method("setup_auc_maximization", &AUCKernel::setup_auc_maximization); } -AUCKernel::AUCKernel() : DotKernel(0), subkernel(nullptr), labels(nullptr) -{ - init(); -} - AUCKernel::AUCKernel(int32_t size, std::shared_ptr s, std::shared_ptr l) - : DotKernel(size), subkernel(std::move(s)), labels(std::move(l)) + : AUCKernel() { - init(); + set_cache_size(size); + m_subkernel = std::move(s); + m_labels = std::move(l); } AUCKernel::~AUCKernel() @@ -42,14 +39,14 @@ AUCKernel::~AUCKernel() bool AUCKernel::setup_auc_maximization() { io::info("setting up AUC maximization"); - ASSERT(labels) - ASSERT(labels->get_label_type() == LT_BINARY) - labels->ensure_valid(); + ASSERT(m_labels) + ASSERT(m_labels->get_label_type() == LT_BINARY) + m_labels->ensure_valid(); // get the original labels - SGVector int_labels = binary_labels(labels)->get_int_labels(); + SGVector int_labels = binary_labels(m_labels)->get_int_labels(); - ASSERT(subkernel->get_num_vec_rhs() == int_labels.vlen) + ASSERT(m_subkernel->get_num_vec_rhs() == int_labels.vlen) // count positive and negative int32_t num_pos = 0; @@ -135,16 +132,16 @@ float64_t AUCKernel::compute(int32_t idx_a, int32_t idx_b) ASSERT(alen == 2) ASSERT(blen == 2) - ASSERT(subkernel && subkernel->has_features()) + ASSERT(m_subkernel && m_subkernel->has_features()) float64_t k11, k12, k21, k22; int32_t idx_a1 = avec[0], idx_a2 = avec[1], idx_b1 = bvec[0], idx_b2 = bvec[1]; - k11 = subkernel->kernel(idx_a1, idx_b1); - k12 = subkernel->kernel(idx_a1, idx_b2); - k21 = subkernel->kernel(idx_a2, idx_b1); - k22 = subkernel->kernel(idx_a2, idx_b2); + k11 = m_subkernel->kernel(idx_a1, idx_b1); + k12 = m_subkernel->kernel(idx_a1, idx_b2); + k21 = m_subkernel->kernel(idx_a2, idx_b1); + k22 = m_subkernel->kernel(idx_a2, idx_b2); float64_t result = k11 + k22 - k21 - k12; lhs->as>()->free_feature_vector(avec, idx_a, afree); diff --git a/src/shogun/kernel/AUCKernel.h b/src/shogun/kernel/AUCKernel.h index ce4c2eb6766..2f8341caa9a 100644 --- a/src/shogun/kernel/AUCKernel.h +++ b/src/shogun/kernel/AUCKernel.h @@ -31,9 +31,8 @@ namespace shogun */ class AUCKernel : public DotKernel { - void init(); public: - /** default constructor */ + AUCKernel(); /** constructor @@ -44,7 +43,6 @@ namespace shogun */ AUCKernel(int32_t size, std::shared_ptr subkernel, std::shared_ptr labels); - /** destructor */ virtual ~AUCKernel(); /** initialize kernel based on current labeling and subkernel @@ -54,66 +52,21 @@ namespace shogun */ bool setup_auc_maximization(); - /** initialize kernel - * - * @param l features of left-hand side - * @param r features of right-hand side - * @return if initializing was successful - */ + virtual EKernelType get_kernel_type() { return K_AUC; } virtual bool init(std::shared_ptr l, std::shared_ptr r); - /** return what type of kernel we are - * - * @return kernel type AUC - */ - virtual EKernelType get_kernel_type() - { - return K_AUC; - } - - /** return the kernel's name - * - * @return name AUC - */ - virtual const char* get_name() const - { - return "AUCKernel"; - } - - /** return feature class the kernel can deal with - * - * @return feature class SIMPLE - */ - virtual EFeatureClass get_feature_class() - { - return C_DENSE; - } + virtual const char* get_name() const { return "AUCKernel"; } - /** return feature type the kernel can deal with - * - * @return word feature type - */ - virtual EFeatureType get_feature_type() - { - return F_WORD; - } + virtual EFeatureClass get_feature_class() { return C_DENSE; } + + virtual EFeatureType get_feature_type() { return F_WORD; } 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); protected: - /** the subkernel */ - std::shared_ptr subkernel; - /** the labels */ - std::shared_ptr labels; + std::shared_ptr m_subkernel; + std::shared_ptr m_labels; }; } // namespace shogun #endif /* _AUCKERNEL_H__ */ diff --git a/src/shogun/kernel/ConstKernel.cpp b/src/shogun/kernel/ConstKernel.cpp index 5dff069c1b1..b60362977c4 100644 --- a/src/shogun/kernel/ConstKernel.cpp +++ b/src/shogun/kernel/ConstKernel.cpp @@ -17,22 +17,21 @@ using namespace shogun; ConstKernel::ConstKernel() : Kernel() { - init(); + SG_ADD(&m_const_val, "const_value", "Value for kernel elements.", + ParameterProperties::HYPER); } ConstKernel::ConstKernel(float64_t c) -: Kernel() +: ConstKernel() { - init(); - const_value=c; + m_const_val = c; } ConstKernel::ConstKernel(std::shared_ptr l, std::shared_ptr r, float64_t c) -: Kernel() +: ConstKernel(c) { - init(); - const_value=c; - init(std::move(l), std::move(r)); + Kernel::init(l, r); + ASSERT(init_normalizer()); } ConstKernel::~ConstKernel() @@ -45,9 +44,3 @@ bool ConstKernel::init(std::shared_ptr l, std::shared_ptr r) return init_normalizer(); } -void ConstKernel::init() -{ - const_value=1.0; - SG_ADD(&const_value, "const_value", "Value for kernel elements.", - ParameterProperties::HYPER); -} diff --git a/src/shogun/kernel/ConstKernel.h b/src/shogun/kernel/ConstKernel.h index 717e0c81522..9b98593286b 100644 --- a/src/shogun/kernel/ConstKernel.h +++ b/src/shogun/kernel/ConstKernel.h @@ -25,7 +25,7 @@ namespace shogun class ConstKernel: public Kernel { public: - /** default constructor */ + ConstKernel(); /** constructor @@ -44,62 +44,21 @@ class ConstKernel: public Kernel virtual ~ConstKernel(); - /** initialize kernel - * - * @param l features of left-hand side - * @param r features of right-hand side - * @return if initializing was successful - */ virtual bool init(std::shared_ptr l, std::shared_ptr r); - /** return what type of kernel we are - * - * @return kernel type CONST - */ virtual EKernelType get_kernel_type() { return K_CONST; } - /** return feature type the kernel can deal with - * - * @return feature type ANY - */ - virtual EFeatureType get_feature_type() - { - return F_ANY; - } + virtual EFeatureType get_feature_type() { return F_ANY; } - /** return feature class the kernel can deal with - * - * @return feature class ANY - */ - virtual EFeatureClass get_feature_class() - { - return C_ANY; - } + virtual EFeatureClass get_feature_class() { return C_ANY; } - /** return the kernel's name - * - * @return name Const - */ virtual const char* get_name() const { return "ConstKernel"; } protected: - /** compute kernel function for features a and b - * - * @param row dummy row - * @param col dummy col - * @return computed kernel function (const value) - */ - virtual float64_t compute(int32_t row, int32_t col) - { - return const_value; - } - - private: - void init(); + virtual float64_t compute(int32_t row, int32_t col) { return m_const_val; } protected: - /** const value */ - float64_t const_value; + float64_t m_const_val = 1.0; }; } #endif /* _CONSTKERNEL_H__ */ diff --git a/src/shogun/kernel/DiagKernel.cpp b/src/shogun/kernel/DiagKernel.cpp index 835c9a1f6af..6b7f18d555b 100644 --- a/src/shogun/kernel/DiagKernel.cpp +++ b/src/shogun/kernel/DiagKernel.cpp @@ -17,22 +17,23 @@ using namespace shogun; DiagKernel::DiagKernel() : Kernel() { - init(); + m_diag=1.0; + SG_ADD(&m_diag, "diag", "Value on kernel diagonal.", ParameterProperties::HYPER); } DiagKernel::DiagKernel(int32_t size, float64_t d) -: Kernel(size) +: DiagKernel() { - init(); - diag=d; + set_cache_size(size); + m_diag = d; } DiagKernel::DiagKernel(std::shared_ptr l, std::shared_ptr r, float64_t d) -: Kernel() +: DiagKernel() { - init(); - diag=d; - init(std::move(l), std::move(r)); + m_diag=d; + ASSERT(Kernel::init(std::move(l), std::move(r))); + init_normalizer(); } DiagKernel::~DiagKernel() @@ -45,8 +46,3 @@ bool DiagKernel::init(std::shared_ptr l, std::shared_ptr r) return init_normalizer(); } -void DiagKernel::init() -{ - diag=1.0; - SG_ADD(&diag, "diag", "Value on kernel diagonal.", ParameterProperties::HYPER); -} diff --git a/src/shogun/kernel/DiagKernel.h b/src/shogun/kernel/DiagKernel.h index f1b871e8442..c7fec5cecce 100644 --- a/src/shogun/kernel/DiagKernel.h +++ b/src/shogun/kernel/DiagKernel.h @@ -24,7 +24,7 @@ namespace shogun class DiagKernel: public Kernel { public: - /** default constructor */ + DiagKernel(); /** constructor @@ -44,66 +44,24 @@ class DiagKernel: public Kernel virtual ~DiagKernel(); - /** initialize kernel - * - * @param l features of left-hand side - * @param r features of right-hand side - * @return if initializing was successful - */ + virtual EFeatureType get_feature_type(){ return F_ANY; } + virtual bool init(std::shared_ptr l, std::shared_ptr r); - /** return feature type the kernel can deal with - * - * @return feature type ANY - */ - virtual EFeatureType get_feature_type() - { - return F_ANY; - } + virtual EFeatureClass get_feature_class(){ return C_ANY; } - /** return feature class the kernel can deal with - * - * @return feature class ANY - */ - virtual EFeatureClass get_feature_class() - { - return C_ANY; - } - - /** return what type of kernel we are - * - * @return kernel type CUSTOM - */ virtual EKernelType get_kernel_type() { return K_DIAG; } - /** return the kernel's name - * - * @return name Custom - */ virtual const char* get_name() const { return "DiagKernel"; } 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) { - if (idx_a==idx_b) - return diag; - else - return 0; + return idx_a == idx_b ? m_diag : 0; } - private: - void init(); protected: - /** diagonal */ - float64_t diag; + float64_t m_diag = 1.0; }; } #endif /* _DIAGKERNEL_H__ */ diff --git a/src/shogun/kernel/LogKernel.cpp b/src/shogun/kernel/LogKernel.cpp index ec828955431..714b74bde9a 100644 --- a/src/shogun/kernel/LogKernel.cpp +++ b/src/shogun/kernel/LogKernel.cpp @@ -11,32 +11,32 @@ using namespace shogun; -LogKernel::LogKernel(): Kernel(0), m_distance(NULL), m_degree(1.8) +LogKernel::LogKernel(): Kernel(0) { - init(); + SG_ADD(&m_degree, "degree", "Degree kernel parameter.", ParameterProperties::HYPER); + SG_ADD(&m_distance, "distance", "Distance to be used.", ParameterProperties::HYPER); } LogKernel::LogKernel(int32_t cache, float64_t degree, std::shared_ptr dist) -: Kernel(cache), m_distance(std::move(dist)), m_degree(degree) +: LogKernel() { - init(); - ASSERT(m_distance) - + set_cache_size(cache); + m_degree = degree; + m_distance = std::move(dist); + ASSERT(m_distance); } LogKernel::LogKernel(std::shared_ptrl, std::shared_ptrr, float64_t degree, std::shared_ptr dist) -: Kernel(10), m_distance(std::move(dist)), m_degree(degree) -{ - init(); - ASSERT(m_distance) - - init(std::move(l), std::move(r)); +: LogKernel(10, degree, std::move(dist)) +{ + Kernel::init(l,r); + ASSERT(m_distance->init(l,r)); + init_normalizer(); } LogKernel::~LogKernel() { cleanup(); - } bool LogKernel::init(std::shared_ptr l, std::shared_ptr r) @@ -47,11 +47,7 @@ bool LogKernel::init(std::shared_ptr l, std::shared_ptr r) return init_normalizer(); } -void LogKernel::init() -{ - SG_ADD(&m_degree, "degree", "Degree kernel parameter.", ParameterProperties::HYPER); - SG_ADD(&m_distance, "distance", "Distance to be used.", ParameterProperties::HYPER); -} + float64_t LogKernel::compute(int32_t idx_a, int32_t idx_b) { diff --git a/src/shogun/kernel/LogKernel.h b/src/shogun/kernel/LogKernel.h index 7bc4eb5d052..a4e913d0695 100644 --- a/src/shogun/kernel/LogKernel.h +++ b/src/shogun/kernel/LogKernel.h @@ -31,7 +31,7 @@ class Distance; class LogKernel: public Kernel { public: - /** default constructor */ + LogKernel(); /** constructor @@ -49,54 +49,24 @@ class LogKernel: public Kernel */ LogKernel(std::shared_ptrl, std::shared_ptrr, float64_t degree, std::shared_ptr dist); - /** initialize kernel with features - * @param l features left-side - * @param r features right-side - * @return true if successful - */ virtual bool init(std::shared_ptr l, std::shared_ptr r); - /** - * @return kernel type - */ virtual EKernelType get_kernel_type() { return K_POWER; } - /** - * @return type of features - */ virtual EFeatureType get_feature_type() { return m_distance->get_feature_type(); } - /** - * @return class of features - */ virtual EFeatureClass get_feature_class() { return m_distance->get_feature_class(); } - /** - * @return name of kernel - */ virtual const char* get_name() const { return "LogKernel"; } virtual ~LogKernel(); protected: - /** - * compute kernel for specific feature vectors - * corresponding to [idx_a] of left-side and [idx_b] of right-side - * @param idx_a left-side index - * @param idx_b right-side index - * @return kernel value - */ virtual float64_t compute(int32_t idx_a, int32_t idx_b); -private: - void init(); - protected: - /// distance to be used std::shared_ptr m_distance; - - /// degree parameter of kernel - float64_t m_degree; + float64_t m_degree = 1.8; }; }