Skip to content

Commit

Permalink
Kernel Constructor Cleanup #5030 (#5136)
Browse files Browse the repository at this point in the history
* Kernel Constructor Cleanup #5030
  • Loading branch information
GuoxinYin committed Dec 8, 2020
1 parent 550d5f9 commit dd4b73e
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 90 deletions.
26 changes: 9 additions & 17 deletions src/shogun/kernel/DistanceKernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,26 @@ using namespace shogun;
DistanceKernel::DistanceKernel()
: Kernel(0), distance(NULL), width(0.0)
{
register_params();
SG_ADD(&width, "width", "Kernel width.", ParameterProperties::HYPER);
SG_ADD(&distance, "distance", "Distance to be used.",
ParameterProperties::HYPER);
}

DistanceKernel::DistanceKernel(int32_t size, float64_t w, std::shared_ptr<Distance> d)
: Kernel(size), distance(std::move(d))
{
: DistanceKernel()
{
set_cache_size(size);
distance = std::move(d);
ASSERT(distance)
set_width(w);

register_params();
}

DistanceKernel::DistanceKernel(
std::shared_ptr<Features >l, std::shared_ptr<Features >r, float64_t w , std::shared_ptr<Distance> d)
: Kernel(10), distance(std::move(d))
{
set_width(w);
: DistanceKernel(10, w, std::move(d))
{
ASSERT(distance)

init(std::move(l), std::move(r));
register_params();
}

DistanceKernel::~DistanceKernel()
Expand All @@ -62,10 +61,3 @@ float64_t DistanceKernel::compute(int32_t idx_a, int32_t idx_b)
float64_t result=distance->distance(idx_a, idx_b);
return exp(-result/width);
}

void DistanceKernel::register_params()
{
SG_ADD(&width, "width", "Kernel width.", ParameterProperties::HYPER);
SG_ADD(&distance, "distance", "Distance to be used.",
ParameterProperties::HYPER);
}
4 changes: 0 additions & 4 deletions src/shogun/kernel/DistanceKernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ class DistanceKernel: public Kernel
*/
bool init(std::shared_ptr<Features> l, std::shared_ptr<Features> r) override;

/** register the parameters (serialization support)
*
*/
void register_params() override;

/** return what type of kernel we are
*
Expand Down
15 changes: 5 additions & 10 deletions src/shogun/kernel/GaussianShortRealKernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ using namespace shogun;
GaussianShortRealKernel::GaussianShortRealKernel()
: DotKernel(0), width(0.0)
{
register_params();
SG_ADD(&width, "width", "kernel width", ParameterProperties::HYPER);
}

GaussianShortRealKernel::GaussianShortRealKernel(int32_t size, float64_t w)
: DotKernel(size), width(w)
: GaussianShortRealKernel()
{
register_params();
set_cache_size(size);
width = w;
}

GaussianShortRealKernel::GaussianShortRealKernel(
const std::shared_ptr<DenseFeatures<float32_t>>& l, const std::shared_ptr<DenseFeatures<float32_t>>& r, float64_t w, int32_t size)
: DotKernel(size), width(w)
: GaussianShortRealKernel(size, w)
{
init(l,r);
register_params();
}

GaussianShortRealKernel::~GaussianShortRealKernel()
Expand Down Expand Up @@ -61,8 +61,3 @@ float64_t GaussianShortRealKernel::compute(int32_t idx_a, int32_t idx_b)

return result;
}

void GaussianShortRealKernel::register_params()
{
SG_ADD(&width, "width", "kernel width", ParameterProperties::HYPER);
}
3 changes: 0 additions & 3 deletions src/shogun/kernel/GaussianShortRealKernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ class GaussianShortRealKernel: public DotKernel
* @return name GaussianShortReal
*/
const char* get_name() const override { return "GaussianShortRealKernel"; }
/** register the parameters
*/
void register_params() override;

protected:
/** compute kernel function for features a and b
Expand Down
15 changes: 5 additions & 10 deletions src/shogun/kernel/HistogramIntersectionKernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ using namespace shogun;
HistogramIntersectionKernel::HistogramIntersectionKernel()
: DotKernel(0), m_beta(1.0)
{
register_params();
SG_ADD(&m_beta, "beta", "the beta parameter of the kernel", ParameterProperties::HYPER);
}

HistogramIntersectionKernel::HistogramIntersectionKernel(int32_t size)
: DotKernel(size), m_beta(1.0)
: HistogramIntersectionKernel()
{
register_params();
set_cache_size(size);
}

HistogramIntersectionKernel::HistogramIntersectionKernel(
const std::shared_ptr<DenseFeatures<float64_t>>& l, const std::shared_ptr<DenseFeatures<float64_t>>& r,
float64_t beta, int32_t size)
: DotKernel(size), m_beta(beta)
: HistogramIntersectionKernel(size)
{
init(l,r);
register_params();
m_beta = beta;
}

HistogramIntersectionKernel::~HistogramIntersectionKernel()
Expand Down Expand Up @@ -75,8 +75,3 @@ float64_t HistogramIntersectionKernel::compute(int32_t idx_a, int32_t idx_b)

return result;
}

void HistogramIntersectionKernel::register_params()
{
SG_ADD(&m_beta, "beta", "the beta parameter of the kernel", ParameterProperties::HYPER);
}
3 changes: 0 additions & 3 deletions src/shogun/kernel/HistogramIntersectionKernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ class HistogramIntersectionKernel: public DotKernel
* @return if initializing was successful
*/
bool init(std::shared_ptr<Features> l, std::shared_ptr<Features> r) override;
/* register the parameters
*/
void register_params() override;

/** return what type of kernel we are
*
Expand Down
22 changes: 7 additions & 15 deletions src/shogun/kernel/SphericalKernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,26 @@ using namespace shogun;

SphericalKernel::SphericalKernel(): Kernel(0), distance(NULL)
{
register_params();
SG_ADD(&distance, "distance", "Distance to be used.",
ParameterProperties::HYPER);
SG_ADD(&sigma, "sigma", "Sigma kernel parameter.", ParameterProperties::HYPER);
set_sigma(1.0);
}

SphericalKernel::SphericalKernel(int32_t size, float64_t sig, std::shared_ptr<Distance> dist)
: Kernel(size), distance(std::move(dist))
: SphericalKernel()
{
set_cache_size(size);
distance = std::move(dist);
ASSERT(distance)

register_params();
set_sigma(sig);
}

SphericalKernel::SphericalKernel(
std::shared_ptr<Features >l, std::shared_ptr<Features >r, float64_t sig, std::shared_ptr<Distance> dist)
: Kernel(10), distance(std::move(dist))
: SphericalKernel(10, sig, std::move(dist))
{
ASSERT(distance)

register_params();
set_sigma(sig);
init(std::move(l), std::move(r));
}

Expand All @@ -51,13 +50,6 @@ bool SphericalKernel::init(std::shared_ptr<Features> l, std::shared_ptr<Features
return init_normalizer();
}

void SphericalKernel::register_params()
{
SG_ADD(&distance, "distance", "Distance to be used.",
ParameterProperties::HYPER);
SG_ADD(&sigma, "sigma", "Sigma kernel parameter.", ParameterProperties::HYPER);
}

float64_t SphericalKernel::compute(int32_t idx_a, int32_t idx_b)
{
float64_t dist=distance->distance(idx_a, idx_b);
Expand Down
2 changes: 0 additions & 2 deletions src/shogun/kernel/SphericalKernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ class SphericalKernel: public Kernel
~SphericalKernel() override;

private:
/** register parameters */
void register_params() override;

protected:

Expand Down
16 changes: 5 additions & 11 deletions src/shogun/kernel/TensorProductPairKernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,20 @@ using namespace shogun;
TensorProductPairKernel::TensorProductPairKernel()
: DotKernel(0), subkernel(NULL)
{
register_params();
SG_ADD((std::shared_ptr<SGObject>*)&subkernel, "subkernel", "the subkernel", ParameterProperties::HYPER);
}

TensorProductPairKernel::TensorProductPairKernel(int32_t size, std::shared_ptr<Kernel> s)
: DotKernel(size), subkernel(std::move(s))
: TensorProductPairKernel()
{

register_params();
set_cache_size(size);
subkernel = std::move(s);
}

TensorProductPairKernel::TensorProductPairKernel(const std::shared_ptr<DenseFeatures<int32_t>>& l, const std::shared_ptr<DenseFeatures<int32_t>>& r, std::shared_ptr<Kernel> s)
: DotKernel(10), subkernel(std::move(s))
: TensorProductPairKernel(10, std::move(s))
{

init(l, r);
register_params();
}

TensorProductPairKernel::~TensorProductPairKernel()
Expand Down Expand Up @@ -74,7 +72,3 @@ float64_t TensorProductPairKernel::compute(int32_t idx_a, int32_t idx_b)
return result;
}

void TensorProductPairKernel::register_params()
{
SG_ADD((std::shared_ptr<SGObject>*)&subkernel, "subkernel", "the subkernel", ParameterProperties::HYPER);
}
3 changes: 0 additions & 3 deletions src/shogun/kernel/TensorProductPairKernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ class TensorProductPairKernel: public DotKernel
*/
EKernelType get_kernel_type() override { return K_TPPK; }

/* register the parameters
*/
void register_params() override;

/** return the kernel's name
*
Expand Down
18 changes: 8 additions & 10 deletions src/shogun/kernel/WeightedDegreeRBFKernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,26 @@ using namespace shogun;
WeightedDegreeRBFKernel::WeightedDegreeRBFKernel()
: DotKernel(), width(1), degree(1), weights(0)
{
register_params();
SG_ADD(&width, "width", "Kernel width", ParameterProperties::HYPER);
SG_ADD(&degree, "degree", "Kernel degree", ParameterProperties::HYPER);
}


WeightedDegreeRBFKernel::WeightedDegreeRBFKernel(int32_t size, float64_t w, int32_t d, int32_t nof_prop)
: DotKernel(size), width(w), degree(d), nof_properties(nof_prop), weights(0)
: WeightedDegreeRBFKernel()
{
set_cache_size(size);
width = w;
degree = d;
nof_properties = nof_prop;
init_wd_weights();
register_params();
}

WeightedDegreeRBFKernel::WeightedDegreeRBFKernel(
const std::shared_ptr<DenseFeatures<float64_t>>& l, const std::shared_ptr<DenseFeatures<float64_t>>& r, float64_t w, int32_t d, int32_t nof_prop, int32_t size)
: DotKernel(size), width(w), degree(d), nof_properties(nof_prop), weights(0)
: WeightedDegreeRBFKernel(size, w, d, nof_prop)
{
init_wd_weights();
register_params();
init(l,r);
}

Expand Down Expand Up @@ -108,8 +111,3 @@ float64_t WeightedDegreeRBFKernel::compute(int32_t idx_a, int32_t idx_b)
return result;
}

void WeightedDegreeRBFKernel::register_params()
{
SG_ADD(&width, "width", "Kernel width", ParameterProperties::HYPER);
SG_ADD(&degree, "degree", "Kernel degree", ParameterProperties::HYPER);
}
2 changes: 0 additions & 2 deletions src/shogun/kernel/WeightedDegreeRBFKernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ class WeightedDegreeRBFKernel: public DotKernel
float64_t* weights;

private:
/** register parameters */
void register_params() override;

};
}
Expand Down

0 comments on commit dd4b73e

Please sign in to comment.