Skip to content

Commit

Permalink
refactored multi-kernel MMD (removed the distance instancen from there)
Browse files Browse the repository at this point in the history
  • Loading branch information
lambday committed Jul 4, 2016
1 parent 938f82b commit 02de0bf
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 40 deletions.
6 changes: 4 additions & 2 deletions src/shogun/statistical_testing/QuadraticTimeMMD.cpp
Expand Up @@ -558,18 +558,20 @@ SGVector<float64_t> CQuadraticTimeMMD::compute_statistic(const internal::KernelM

CDistance* distance=kernel_mgr.get_distance_instance();
SG_REF(distance);
kernel_mgr.set_precomputed_distance(compute_joint_distance(distance));
SG_UNREF(distance);

const index_t nx=get_num_samples_p();
const index_t ny=get_num_samples_q();

MultiKernelMMD compute(nx, ny, get_statistic_type());
compute.set_distance(compute_joint_distance(distance));
SGVector<float64_t> result=compute(kernel_mgr);

kernel_mgr.unset_precomputed_distance();

for (auto i=0; i<result.vlen; ++i)
result[i]=normalize_statistic(result[i]);

SG_UNREF(distance);
SG_DEBUG("Leaving");
return result;
}
Expand Down
12 changes: 1 addition & 11 deletions src/shogun/statistical_testing/internals/mmd/MultiKernelMMD.cpp
Expand Up @@ -29,10 +29,8 @@
*/

#include <shogun/io/SGIO.h>
#include <shogun/lib/SGMatrix.h>
#include <shogun/kernel/Kernel.h>
#include <shogun/mathematics/Math.h>
#include <shogun/kernel/ShiftInvariantKernel.h>
#include <shogun/distance/CustomDistance.h>
#include <shogun/statistical_testing/MMD.h>
#include <shogun/statistical_testing/internals/KernelManager.h>
#include <shogun/statistical_testing/internals/mmd/MultiKernelMMD.h>
Expand All @@ -52,11 +50,6 @@ MultiKernelMMD::MultiKernelMMD(index_t nx, index_t ny, EStatisticType stype) : n
SG_SDEBUG("number of samples are %d and %d!\n", n_x, n_y);
}

void MultiKernelMMD::set_distance(CCustomDistance* distance)
{
m_distance=std::shared_ptr<CCustomDistance>(distance);
}

void MultiKernelMMD::add_term(terms_t& t, float32_t val, index_t i, index_t j) const
{
if (i<n_x && j<n_x && i>=j)
Expand Down Expand Up @@ -124,8 +117,6 @@ float64_t MultiKernelMMD::compute_mmd(terms_t& t) const
SGVector<float64_t> MultiKernelMMD::operator()(const KernelManager& kernel_mgr) const
{
SG_SDEBUG("Entering!\n");
REQUIRE(m_distance, "Distance instace is not set!\n");
kernel_mgr.set_precomputed_distance(m_distance.get());

SGVector<float64_t> result(kernel_mgr.num_kernels());
std::vector<terms_t> terms(result.size());
Expand All @@ -149,7 +140,6 @@ SGVector<float64_t> MultiKernelMMD::operator()(const KernelManager& kernel_mgr)
}
terms.resize(0);

kernel_mgr.unset_precomputed_distance();
SG_SDEBUG("Leaving!\n");
return result;
}
8 changes: 3 additions & 5 deletions src/shogun/statistical_testing/internals/mmd/MultiKernelMMD.h
Expand Up @@ -38,7 +38,6 @@ namespace shogun
{

template <typename T> class SGVector;
class CCustomDistance;

namespace internal
{
Expand All @@ -51,15 +50,14 @@ class MultiKernelMMD
public:
MultiKernelMMD(index_t nx, index_t ny, EStatisticType stype);
SGVector<float64_t> operator()(const KernelManager& kernel_mgr) const;
void set_distance(CCustomDistance* distance);
private:
struct terms_t;
void add_term(terms_t&, float32_t, index_t, index_t) const;
float64_t compute_mmd(terms_t& t) const;

const index_t n_x;
const index_t n_y;
const EStatisticType s_type;
std::shared_ptr<CCustomDistance> m_distance;
void add_term(terms_t&, float32_t, index_t, index_t) const;
float64_t compute_mmd(terms_t& t) const;
};

}
Expand Down
44 changes: 22 additions & 22 deletions tests/unit/statistical_testing/internals/MultiKernelMMD_unittest.cc
Expand Up @@ -78,19 +78,19 @@ TEST(MultiKernelMMD, biased_full)
auto feats_p=gen_p->get_streamed_features(m);
auto feats_q=gen_q->get_streamed_features(n);

KernelManager kernel_mgr(num_kernels);
for (auto i=0, sigma=-5; i<num_kernels; ++i, sigma+=1)
kernel_mgr.kernel_at(i)=new CGaussianKernel(10, pow(2, sigma));

auto test=some<CTwoDistributionTestMock>();
test->set_p(feats_p);
test->set_q(feats_q);

KernelManager kernel_mgr(num_kernels);
for (auto i=0, sigma=-5; i<num_kernels; ++i, sigma+=1)
kernel_mgr.kernel_at(i)=new CGaussianKernel(10, pow(2, sigma));
auto distance=kernel_mgr.get_distance_instance();
kernel_mgr.set_precomputed_distance(test->compute_joint_distance(distance));

MultiKernelMMD tester(m, n, stype);
auto distance=some<CEuclideanDistance>();
distance->set_disable_sqrt(true);
tester.set_distance(test->compute_joint_distance(distance));
SGVector<float64_t> values=tester(kernel_mgr);
kernel_mgr.unset_precomputed_distance();

auto data_p=static_cast<CDenseFeatures<float64_t>*>(feats_p)->get_feature_matrix();
auto data_q=static_cast<CDenseFeatures<float64_t>*>(feats_q)->get_feature_matrix();
Expand Down Expand Up @@ -140,19 +140,19 @@ TEST(MultiKernelMMD, unbiased_full)
auto feats_p=gen_p->get_streamed_features(m);
auto feats_q=gen_q->get_streamed_features(n);

KernelManager kernel_mgr(num_kernels);
for (auto i=0, sigma=-5; i<num_kernels; ++i, sigma+=1)
kernel_mgr.kernel_at(i)=new CGaussianKernel(10, pow(2, sigma));

auto test=some<CTwoDistributionTestMock>();
test->set_p(feats_p);
test->set_q(feats_q);

KernelManager kernel_mgr(num_kernels);
for (auto i=0, sigma=-5; i<num_kernels; ++i, sigma+=1)
kernel_mgr.kernel_at(i)=new CGaussianKernel(10, pow(2, sigma));
auto distance=kernel_mgr.get_distance_instance();
kernel_mgr.set_precomputed_distance(test->compute_joint_distance(distance));

MultiKernelMMD tester(m, n, stype);
auto distance=some<CEuclideanDistance>();
distance->set_disable_sqrt(true);
tester.set_distance(test->compute_joint_distance(distance));
SGVector<float64_t> values=tester(kernel_mgr);
kernel_mgr.unset_precomputed_distance();

auto data_p=static_cast<CDenseFeatures<float64_t>*>(feats_p)->get_feature_matrix();
auto data_q=static_cast<CDenseFeatures<float64_t>*>(feats_q)->get_feature_matrix();
Expand Down Expand Up @@ -193,7 +193,7 @@ TEST(MultiKernelMMD, unbiased_incomplete)
const index_t n=8;
const index_t dim=1;
const float64_t difference=0.5;
const index_t num_kernels=1;
const index_t num_kernels=10;
const EStatisticType stype=ST_UNBIASED_INCOMPLETE;

auto gen_p=some<CMeanShiftDataGenerator>(0, dim, 0);
Expand All @@ -202,19 +202,19 @@ TEST(MultiKernelMMD, unbiased_incomplete)
auto feats_p=gen_p->get_streamed_features(m);
auto feats_q=gen_q->get_streamed_features(n);

KernelManager kernel_mgr(num_kernels);
for (auto i=0, sigma=-5; i<num_kernels; ++i, sigma+=1)
kernel_mgr.kernel_at(i)=new CGaussianKernel(10, pow(2, sigma));

auto test=some<CTwoDistributionTestMock>();
test->set_p(feats_p);
test->set_q(feats_q);

KernelManager kernel_mgr(num_kernels);
for (auto i=0, sigma=-5; i<num_kernels; ++i, sigma+=1)
kernel_mgr.kernel_at(i)=new CGaussianKernel(10, pow(2, sigma));
auto distance=kernel_mgr.get_distance_instance();
kernel_mgr.set_precomputed_distance(test->compute_joint_distance(distance));

MultiKernelMMD tester(m, n, stype);
auto distance=some<CEuclideanDistance>();
distance->set_disable_sqrt(true);
tester.set_distance(test->compute_joint_distance(distance));
SGVector<float64_t> values=tester(kernel_mgr);
kernel_mgr.unset_precomputed_distance();

auto data_p=static_cast<CDenseFeatures<float64_t>*>(feats_p)->get_feature_matrix();
auto data_q=static_cast<CDenseFeatures<float64_t>*>(feats_q)->get_feature_matrix();
Expand Down

0 comments on commit 02de0bf

Please sign in to comment.