From d120e78565b06e7c722d447169360a336bf3bc32 Mon Sep 17 00:00:00 2001 From: lambday Date: Wed, 4 May 2016 18:49:14 +0100 Subject: [PATCH] added weighted kernel learning methods --- src/shogun/statistical_testing/MMD.cpp | 34 ++-- src/shogun/statistical_testing/MMD.h | 9 +- .../internals/KernelSelection.cpp | 26 +-- .../internals/KernelSelection.h | 4 +- .../internals/MaxMeasure.cpp | 21 ++- .../internals/MaxMeasure.h | 5 +- .../internals/MaxTestPower.cpp | 15 +- .../internals/MaxTestPower.h | 5 +- .../internals/OptimizationSolver.cpp | 158 ++++++++++++++++++ .../internals/OptimizationSolver.h | 64 +++++++ .../internals/WeightedMaxMeasure.cpp | 73 ++++++++ .../internals/WeightedMaxMeasure.h | 61 +++++++ .../internals/WeightedMaxTestPower.cpp | 76 +++++++++ .../internals/WeightedMaxTestPower.h | 62 +++++++ 14 files changed, 557 insertions(+), 56 deletions(-) create mode 100644 src/shogun/statistical_testing/internals/OptimizationSolver.cpp create mode 100644 src/shogun/statistical_testing/internals/OptimizationSolver.h create mode 100644 src/shogun/statistical_testing/internals/WeightedMaxMeasure.cpp create mode 100644 src/shogun/statistical_testing/internals/WeightedMaxMeasure.h create mode 100644 src/shogun/statistical_testing/internals/WeightedMaxTestPower.cpp create mode 100644 src/shogun/statistical_testing/internals/WeightedMaxTestPower.h diff --git a/src/shogun/statistical_testing/MMD.cpp b/src/shogun/statistical_testing/MMD.cpp index e01063f1988..99fd8cba563 100644 --- a/src/shogun/statistical_testing/MMD.cpp +++ b/src/shogun/statistical_testing/MMD.cpp @@ -46,6 +46,8 @@ #include #include #include +#include +#include #include #include #include @@ -388,31 +390,36 @@ void CMMD::add_kernel(CKernel* kernel) self->kernel_selection_mgr.push_back(kernel); } -void CMMD::select_kernel(EKernelSelectionMethod kmethod) +void CMMD::select_kernel(EKernelSelectionMethod kmethod, bool weighted_kernel) { SG_DEBUG("Entering!\n"); SG_DEBUG("Selecting kernels from a total of %d kernels!\n", self->kernel_selection_mgr.num_kernels()); + std::shared_ptr policy=nullptr; switch (kmethod) { case EKernelSelectionMethod::MAXIMIZE_MMD: - { - MaxMeasure policy(self->kernel_selection_mgr, this); - get_kernel_manager().kernel_at(0)=policy.select_kernel(); - get_kernel_manager().restore_kernel_at(0); + if (weighted_kernel) + policy=std::shared_ptr(new WeightedMaxMeasure(self->kernel_selection_mgr, this)); + else + policy=std::shared_ptr(new MaxMeasure(self->kernel_selection_mgr, this)); break; - } case EKernelSelectionMethod::MAXIMIZE_POWER: - { - MaxTestPower policy(self->kernel_selection_mgr, this); - get_kernel_manager().kernel_at(0)=policy.select_kernel(); - get_kernel_manager().restore_kernel_at(0); + if (weighted_kernel) + policy=std::shared_ptr(new WeightedMaxTestPower(self->kernel_selection_mgr, this)); + else + policy=std::shared_ptr(new MaxTestPower(self->kernel_selection_mgr, this)); break; - } default: SG_ERROR("Unsupported kernel selection method specified! " "Presently only accepted values are MAXIMIZE_MMD, MAXIMIZE_POWER!\n"); break; } + if (policy!=nullptr) + { + auto& km=get_kernel_manager(); + km.kernel_at(0)=policy->select_kernel(); + km.restore_kernel_at(0); + } SG_DEBUG("Leaving!\n"); } @@ -431,6 +438,11 @@ std::pair CMMD::compute_statistic_variance() return self->compute_statistic_variance(); } +std::pair, SGMatrix> CMMD::compute_statistic_and_Q() +{ + return self->compute_statistic_and_Q(); +} + SGVector CMMD::sample_null() { return self->sample_null(); diff --git a/src/shogun/statistical_testing/MMD.h b/src/shogun/statistical_testing/MMD.h index 0a90d12b6f1..5e24a698892 100644 --- a/src/shogun/statistical_testing/MMD.h +++ b/src/shogun/statistical_testing/MMD.h @@ -47,6 +47,7 @@ namespace internal { class MaxTestPower; +class WeightedMaxTestPower; } @@ -75,19 +76,22 @@ enum class EKernelSelectionMethod { MEDIAN_HEURISRIC, MAXIMIZE_MMD, - MAXIMIZE_POWER + MAXIMIZE_POWER, + MAXIMIZE_XVALIDATION, + AUTO }; class CMMD : public CTwoSampleTest { using operation=std::function)>; friend class internal::MaxTestPower; + friend class internal::WeightedMaxTestPower; public: CMMD(); virtual ~CMMD(); void add_kernel(CKernel *kernel); - void select_kernel(EKernelSelectionMethod kmethod); + void select_kernel(EKernelSelectionMethod kmethod=EKernelSelectionMethod::AUTO, bool weighted_kernel=false); virtual float64_t compute_statistic() override; virtual float64_t compute_variance(); @@ -119,6 +123,7 @@ class CMMD : public CTwoSampleTest struct Self; std::unique_ptr self; virtual std::pair compute_statistic_variance(); + virtual std::pair, SGMatrix> compute_statistic_and_Q(); }; } diff --git a/src/shogun/statistical_testing/internals/KernelSelection.cpp b/src/shogun/statistical_testing/internals/KernelSelection.cpp index 90d90f5e1bf..6daea828d98 100644 --- a/src/shogun/statistical_testing/internals/KernelSelection.cpp +++ b/src/shogun/statistical_testing/internals/KernelSelection.cpp @@ -29,34 +29,16 @@ * either expressed or implied, of the Shogun Development Team. */ -#include -#include #include #include -#include -#include -namespace shogun -{ - -namespace internal -{ +using namespace shogun; +using namespace internal; -template -KernelSelection::KernelSelection(KernelManager& km) : kernel_mgr(km) +KernelSelection::KernelSelection(KernelManager& km) : kernel_mgr(km) { - SG_SDEBUG("Kernel selection instance initialized!\n"); } -template -CKernel* KernelSelection::select_kernel() +KernelSelection::~KernelSelection() { - return static_cast(this)->select_kernel(); -} - -template class KernelSelection; -template class KernelSelection; - -} - } diff --git a/src/shogun/statistical_testing/internals/KernelSelection.h b/src/shogun/statistical_testing/internals/KernelSelection.h index 3f68b055e15..9a3ae3a4230 100644 --- a/src/shogun/statistical_testing/internals/KernelSelection.h +++ b/src/shogun/statistical_testing/internals/KernelSelection.h @@ -44,14 +44,14 @@ namespace internal class KernelManager; -template class KernelSelection { public: explicit KernelSelection(KernelManager&); KernelSelection(const KernelSelection& other)=delete; + virtual ~KernelSelection(); KernelSelection& operator=(const KernelSelection& other)=delete; - CKernel* select_kernel(); + virtual CKernel* select_kernel()=0; protected: const KernelManager& kernel_mgr; }; diff --git a/src/shogun/statistical_testing/internals/MaxMeasure.cpp b/src/shogun/statistical_testing/internals/MaxMeasure.cpp index c1061ff037f..4409a98cc86 100644 --- a/src/shogun/statistical_testing/internals/MaxMeasure.cpp +++ b/src/shogun/statistical_testing/internals/MaxMeasure.cpp @@ -39,16 +39,24 @@ using namespace shogun; using namespace internal; -MaxMeasure::MaxMeasure(KernelManager& km, CMMD* est) -: KernelSelection(km), estimator(est) +MaxMeasure::MaxMeasure(KernelManager& km, CMMD* est) : KernelSelection(km), estimator(est) +{ +} + +MaxMeasure::~MaxMeasure() { } SGVector MaxMeasure::compute_measures() { - SGVector result(kernel_mgr.num_kernels()); + REQUIRE(estimator!=nullptr, "Estimator is not set!\n"); + + const size_t num_kernels=kernel_mgr.num_kernels(); + REQUIRE(num_kernels>0, "Number of kernels is %d!\n", kernel_mgr.num_kernels()); + + SGVector result(num_kernels); auto existing_kernel=estimator->get_kernel(); - for (size_t i=0; iset_kernel(kernel); @@ -61,14 +69,9 @@ SGVector MaxMeasure::compute_measures() CKernel* MaxMeasure::select_kernel() { - REQUIRE(estimator!=nullptr, "Estimator is not set!\n"); - REQUIRE(kernel_mgr.num_kernels()>0, "Number of kernels is %d!\n", kernel_mgr.num_kernels()); - SGVector measures=compute_measures(); auto max_element=std::max_element(measures.vector, measures.vector+measures.vlen); auto max_idx=std::distance(measures.vector, max_element); - SG_SDEBUG("Selected kernel at %d position!\n", max_idx); - return kernel_mgr.kernel_at(max_idx); } diff --git a/src/shogun/statistical_testing/internals/MaxMeasure.h b/src/shogun/statistical_testing/internals/MaxMeasure.h index 67bcb6befd1..36652e5455c 100644 --- a/src/shogun/statistical_testing/internals/MaxMeasure.h +++ b/src/shogun/statistical_testing/internals/MaxMeasure.h @@ -45,13 +45,14 @@ template class SGVector; namespace internal { -class MaxMeasure : public KernelSelection +class MaxMeasure : public KernelSelection { public: MaxMeasure(KernelManager&, CMMD*); MaxMeasure(const MaxMeasure& other)=delete; + ~MaxMeasure(); MaxMeasure& operator=(const MaxMeasure& other)=delete; - CKernel* select_kernel(); + virtual CKernel* select_kernel() override; protected: SGVector compute_measures(); CMMD* estimator; diff --git a/src/shogun/statistical_testing/internals/MaxTestPower.cpp b/src/shogun/statistical_testing/internals/MaxTestPower.cpp index df4562777f4..8510807c50b 100644 --- a/src/shogun/statistical_testing/internals/MaxTestPower.cpp +++ b/src/shogun/statistical_testing/internals/MaxTestPower.cpp @@ -40,13 +40,19 @@ using namespace shogun; using namespace internal; -MaxTestPower::MaxTestPower(KernelManager& km, CMMD* est) -: KernelSelection(km), estimator(est), lambda(1E-5) +MaxTestPower::MaxTestPower(KernelManager& km, CMMD* est) : KernelSelection(km), estimator(est), lambda(1E-5) +{ +} + +MaxTestPower::~MaxTestPower() { } SGVector MaxTestPower::compute_measures() { + REQUIRE(estimator!=nullptr, "Estimator is not set!\n"); + REQUIRE(kernel_mgr.num_kernels()>0, "Number of kernels is %d!\n", kernel_mgr.num_kernels()); + SGVector result(kernel_mgr.num_kernels()); for (size_t i=0; i MaxTestPower::compute_measures() CKernel* MaxTestPower::select_kernel() { - REQUIRE(estimator!=nullptr, "Estimator is not set!\n"); - REQUIRE(kernel_mgr.num_kernels()>0, "Number of kernels is %d!\n", kernel_mgr.num_kernels()); - SGVector measures=compute_measures(); auto max_element=std::max_element(measures.vector, measures.vector+measures.vlen); auto max_idx=std::distance(measures.vector, max_element); - + SG_SDEBUG("Selected kernel at %d position!\n", max_idx); return kernel_mgr.kernel_at(max_idx); } diff --git a/src/shogun/statistical_testing/internals/MaxTestPower.h b/src/shogun/statistical_testing/internals/MaxTestPower.h index 1c894f8b401..0487ae1f7d0 100644 --- a/src/shogun/statistical_testing/internals/MaxTestPower.h +++ b/src/shogun/statistical_testing/internals/MaxTestPower.h @@ -45,13 +45,14 @@ template class SGVector; namespace internal { -class MaxTestPower : public KernelSelection +class MaxTestPower : public KernelSelection { public: MaxTestPower(KernelManager&, CMMD*); MaxTestPower(const MaxTestPower& other)=delete; + ~MaxTestPower(); MaxTestPower& operator=(const MaxTestPower& other)=delete; - CKernel* select_kernel(); + virtual CKernel* select_kernel() override; protected: SGVector compute_measures(); CMMD* estimator; diff --git a/src/shogun/statistical_testing/internals/OptimizationSolver.cpp b/src/shogun/statistical_testing/internals/OptimizationSolver.cpp new file mode 100644 index 00000000000..1aee9360f23 --- /dev/null +++ b/src/shogun/statistical_testing/internals/OptimizationSolver.cpp @@ -0,0 +1,158 @@ +/* + * 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. + * + * Written (W) 2011-2013 Heiko Strathmann + * Written (W) 2016 Soumyajit De + */ + +#include +#include +#include +#include +#include +#include +#include + +#ifdef USE_GPL_SHOGUN +#include +#endif // USE_GPL_SHOGUN + +using namespace shogun; +using namespace internal; + +struct OptimizationSolver::Self +{ + Self(SGVector mmds, SGMatrix Q); +#ifdef USE_GPL_SHOGUN + SGVector solve() const; + void init(); + static const float64_t* get_Q_col(uint32_t i); + static void print_state(libqp_state_T state); + + index_t opt_max_iterations; + float64_t opt_epsilon; + float64_t opt_low_cut; + SGVector m_mmds; + static SGMatrix m_Q; +#endif // USE_GPL_SHOGUN +}; + +OptimizationSolver::Self::Self(SGVector mmds, SGMatrix Q) +{ +#ifdef USE_GPL_SHOGUN + m_Q=Q; + m_mmds=mmds; + init(); +#endif // USE_GPL_SHOGUN +} + +#ifdef USE_GPL_SHOGUN +void OptimizationSolver::Self::init() +{ + opt_max_iterations=10000; + opt_epsilon=1E-14; + opt_low_cut=1E-6; +} + +const float64_t* OptimizationSolver::Self::get_Q_col(uint32_t i) +{ + return &m_Q[m_Q.num_rows*i]; +} + +void OptimizationSolver::Self::print_state(libqp_state_T state) +{ + SG_SDEBUG("libqp state: primal=%f\n", state.QP); +} + +SGVector OptimizationSolver::Self::solve() const +{ + const index_t num_kernels=m_mmds.size(); + float64_t sum_m_mmds=std::accumulate(m_mmds.data(), m_mmds.data()+m_mmds.size(), 0); + SGVector weights(num_kernels); + if (std::any_of(m_mmds.data(), m_mmds.data()+m_mmds.size(), [](float64_t& value) { return value > 0; })) + { + SG_SDEBUG("At least one MMD entry is positive, performing optimisation\n") + + std::vector Q_diag(num_kernels); + std::vector f(num_kernels, 0); + std::vector lb(num_kernels, 0); + std::vector ub(num_kernels, CMath::INFTY); + + // initial point has to be feasible, i.e. m_mmds'*x = b + std::fill(weights.data(), weights.data()+weights.size(), 1.0/sum_m_mmds); + + for (index_t i=0; i(); + + // set really small entries to zero and sum up for normalization + float64_t sum_weights=0; + for (index_t i=0; i& mmds, const SGMatrix& Q) +{ + self=std::unique_ptr(new Self(mmds, Q)); +} + +OptimizationSolver::~OptimizationSolver() +{ +} + +SGVector OptimizationSolver::solve() const +{ +#ifdef USE_GPL_SHOGUN + return self->solve(); +#else // USE_GPL_SHOGUN + SG_SWARNING("Presently this feature is only available with GNU GPLv3 license!"); + return SGVector(); +#endif // USE_GPL_SHOGUN +} diff --git a/src/shogun/statistical_testing/internals/OptimizationSolver.h b/src/shogun/statistical_testing/internals/OptimizationSolver.h new file mode 100644 index 00000000000..c36f53e26f5 --- /dev/null +++ b/src/shogun/statistical_testing/internals/OptimizationSolver.h @@ -0,0 +1,64 @@ +/* + * Copyright (c) The Shogun Machine Learning Toolbox + * Written (W) 2013 Heiko Strathmann + * Written (w) 2014 - 2016 Soumyajit De + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those + * of the authors and should not be interpreted as representing official policies, + * either expressed or implied, of the Shogun Development Team. + */ + +#ifndef OPTIMIZATION_SOLVER_H__ +#define OPTIMIZATION_SOLVER_H__ + +#include +#include + +namespace shogun +{ + +template class SGVector; +template class SGMatrix; + +namespace internal +{ + +class OptimizationSolver +{ +public: + OptimizationSolver(const SGVector& mmds, const SGMatrix& Q); + OptimizationSolver(const OptimizationSolver& other)=delete; + OptimizationSolver& operator=(const OptimizationSolver& other)=delete; + ~OptimizationSolver(); + SGVector solve() const; +private: + struct Self; + std::unique_ptr self; +}; + +} + +} + +#endif // OPTIMIZATION_SOLVER_H__ diff --git a/src/shogun/statistical_testing/internals/WeightedMaxMeasure.cpp b/src/shogun/statistical_testing/internals/WeightedMaxMeasure.cpp new file mode 100644 index 00000000000..a6f550fe27f --- /dev/null +++ b/src/shogun/statistical_testing/internals/WeightedMaxMeasure.cpp @@ -0,0 +1,73 @@ +/* + * Copyright (c) The Shogun Machine Learning Toolbox + * Written (W) 2013 Heiko Strathmann + * Written (w) 2014 - 2016 Soumyajit De + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those + * of the authors and should not be interpreted as representing official policies, + * either expressed or implied, of the Shogun Development Team. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace shogun; +using namespace internal; + +WeightedMaxMeasure::WeightedMaxMeasure(KernelManager& km, CMMD* est) : MaxMeasure(km, est) +{ +} + +WeightedMaxMeasure::~WeightedMaxMeasure() +{ +} + +CKernel* WeightedMaxMeasure::select_kernel() +{ + const size_t num_kernels=kernel_mgr.num_kernels(); + SGVector measures=compute_measures(); + SGMatrix Q(num_kernels, num_kernels); + std::fill(Q.data(), Q.data()+Q.size(), 0); + for (size_t i=0; i weights=solver.solve(); + + CCombinedKernel* kernel=new CCombinedKernel(); + for (size_t i=0; iappend_kernel(kernel_mgr.kernel_at(i))) + SG_SERROR("Error while creating a combined kernel! Please contact Shogun developers!\n"); + } + kernel->set_subkernel_weights(weights); + SG_SDEBUG("Created a weighted kernel!\n"); + return kernel; +} diff --git a/src/shogun/statistical_testing/internals/WeightedMaxMeasure.h b/src/shogun/statistical_testing/internals/WeightedMaxMeasure.h new file mode 100644 index 00000000000..be17efe38dd --- /dev/null +++ b/src/shogun/statistical_testing/internals/WeightedMaxMeasure.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) The Shogun Machine Learning Toolbox + * Written (W) 2013 Heiko Strathmann + * Written (w) 2014 - 2016 Soumyajit De + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those + * of the authors and should not be interpreted as representing official policies, + * either expressed or implied, of the Shogun Development Team. + */ + +#ifndef WEIGHTED_MAX_MEASURE_H__ +#define WEIGHTED_MAX_MEASURE_H__ + +#include +#include + +namespace shogun +{ + +class CKernel; +class CMMD; + +namespace internal +{ + +class WeightedMaxMeasure : public MaxMeasure +{ +public: + WeightedMaxMeasure(KernelManager&, CMMD*); + WeightedMaxMeasure(const WeightedMaxMeasure& other)=delete; + ~WeightedMaxMeasure(); + WeightedMaxMeasure& operator=(const WeightedMaxMeasure& other)=delete; + virtual CKernel* select_kernel() override; +}; + +} + +} + +#endif // WEIGHTED_MAX_MEASURE_H__ diff --git a/src/shogun/statistical_testing/internals/WeightedMaxTestPower.cpp b/src/shogun/statistical_testing/internals/WeightedMaxTestPower.cpp new file mode 100644 index 00000000000..271555bf390 --- /dev/null +++ b/src/shogun/statistical_testing/internals/WeightedMaxTestPower.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (c) The Shogun Machine Learning Toolbox + * Written (W) 2013 Heiko Strathmann + * Written (w) 2014 - 2016 Soumyajit De + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those + * of the authors and should not be interpreted as representing official policies, + * either expressed or implied, of the Shogun Development Team. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace shogun; +using namespace internal; + +WeightedMaxTestPower::WeightedMaxTestPower(KernelManager& km, CMMD* est) : MaxTestPower(km, est) +{ +} + +WeightedMaxTestPower::~WeightedMaxTestPower() +{ +} + +CKernel* WeightedMaxTestPower::select_kernel() +{ + REQUIRE(estimator!=nullptr, "Estimator is not set!\n"); + REQUIRE(kernel_mgr.num_kernels()>0, "Number of kernels is %d!\n", kernel_mgr.num_kernels()); + + auto estimates=estimator->compute_statistic_and_Q(); + SGVector measures=estimates.first; + SGMatrix Q=estimates.second; + + for (index_t i=0; i weights=solver.solve(); + + CCombinedKernel* kernel=new CCombinedKernel(); + for (size_t i=0; iappend_kernel(kernel_mgr.kernel_at(i))) + SG_SERROR("Error while creating a combined kernel! Please contact Shogun developers!\n"); + } + kernel->set_subkernel_weights(weights); + SG_SDEBUG("Created a weighted kernel!\n"); + return kernel; +} diff --git a/src/shogun/statistical_testing/internals/WeightedMaxTestPower.h b/src/shogun/statistical_testing/internals/WeightedMaxTestPower.h new file mode 100644 index 00000000000..df54320d062 --- /dev/null +++ b/src/shogun/statistical_testing/internals/WeightedMaxTestPower.h @@ -0,0 +1,62 @@ +/* + * Copyright (c) The Shogun Machine Learning Toolbox + * Written (W) 2013 Heiko Strathmann + * Written (w) 2014 - 2016 Soumyajit De + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those + * of the authors and should not be interpreted as representing official policies, + * either expressed or implied, of the Shogun Development Team. + */ + +#ifndef WEIGHTED_MAX_TEST_POWER_H__ +#define WEIGHTED_MAX_TEST_POWER_H__ + +#include +#include + +namespace shogun +{ + +class CKernel; +class CMMD; +template class SGVector; + +namespace internal +{ + +class WeightedMaxTestPower : public MaxTestPower +{ +public: + WeightedMaxTestPower(KernelManager&, CMMD*); + WeightedMaxTestPower(const WeightedMaxTestPower& other)=delete; + ~WeightedMaxTestPower(); + WeightedMaxTestPower& operator=(const WeightedMaxTestPower& other)=delete; + virtual CKernel* select_kernel() override; +}; + +} + +} + +#endif // WEIGHTED_MAX_TEST_POWER_H__