SHOGUN  6.0.0
MedianHeuristic.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (W) 2012 - 2013 Heiko Strathmann
4  * Written (w) 2014 - 2017 Soumyajit De
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * The views and conclusions contained in the software and documentation are those
28  * of the authors and should not be interpreted as representing official policies,
29  * either expressed or implied, of the Shogun Development Team.
30  */
31 
32 #include <vector>
33 #include <algorithm>
34 #include <shogun/io/SGIO.h>
35 #include <shogun/kernel/Kernel.h>
42 
43 using namespace shogun;
44 using namespace internal;
45 
46 MedianHeuristic::MedianHeuristic(KernelManager& km, CMMD* est) : KernelSelection(km, est), distance(nullptr)
47 {
48  for (auto i=0; i<kernel_mgr.num_kernels(); ++i)
49  {
50  REQUIRE(kernel_mgr.kernel_at(i)->get_kernel_type()==K_GAUSSIAN,
51  "The underlying kernel has to be a GaussianKernel (was %s)!\n",
52  kernel_mgr.kernel_at(i)->get_name());
53  }
54 }
55 
56 MedianHeuristic::~MedianHeuristic()
57 {
58 }
59 
60 void MedianHeuristic::init_measures()
61 {
63 }
64 
65 void MedianHeuristic::compute_measures()
66 {
67  auto tmp=new CEuclideanDistance();
68  tmp->set_disable_sqrt(false);
69  SG_REF(tmp);
70  distance=std::shared_ptr<CCustomDistance>(estimator->compute_joint_distance(tmp));
71  SG_UNREF(tmp);
72 
73  n=distance->get_num_vec_lhs();
74  REQUIRE(distance->get_num_vec_lhs()==distance->get_num_vec_rhs(),
75  "Distance matrix is supposed to be a square matrix (was of dimension %dX%d)!\n",
76  distance->get_num_vec_lhs(), distance->get_num_vec_rhs());
77  measures=SGVector<float64_t>((n*(n-1))/2);
78  index_t write_idx=0;
79  for (auto j=0; j<n; ++j)
80  {
81  for (auto i=j+1; i<n; ++i)
82  measures[write_idx++]=distance->distance(i, j);
83  }
84  std::sort(measures.data(), measures.data()+measures.size());
85 }
86 
87 SGVector<float64_t> MedianHeuristic::get_measure_vector()
88 {
89  return measures;
90 }
91 
92 SGMatrix<float64_t> MedianHeuristic::get_measure_matrix()
93 {
94  REQUIRE(distance!=nullptr, "Distance is not initialized!\n");
95  return distance->get_distance_matrix();
96 }
97 
98 CKernel* MedianHeuristic::select_kernel()
99 {
100  compute_measures();
101  auto median_distance=measures[measures.size()/2];
102  SG_SDEBUG("kernel width (shogun): %f\n", median_distance);
103 
104  const auto num_kernels=kernel_mgr.num_kernels();
105  measures=SGVector<float64_t>(num_kernels);
106  for (auto i=0; i<num_kernels; ++i)
107  {
108  CGaussianKernel *kernel=static_cast<CGaussianKernel*>(kernel_mgr.kernel_at(i));
109  measures[i]=CMath::abs(kernel->get_width()-median_distance);
110  }
111 
112  auto kernel_idx=(int64_t)std::distance(measures.data(), std::min_element(measures.data(), measures.data()+measures.size()));
113  SG_SDEBUG("Selected kernel at %d position!\n", kernel_idx);
114  return kernel_mgr.kernel_at(kernel_idx);
115 }
float distance(CJLCoverTreePoint p1, CJLCoverTreePoint p2, float64_t upper_bound)
int32_t index_t
Definition: common.h:72
virtual float64_t get_width() const
#define REQUIRE(x,...)
Definition: SGIO.h:205
#define SG_SNOTIMPLEMENTED
Definition: SGIO.h:197
#define SG_REF(x)
Definition: SGObject.h:52
The well known Gaussian kernel (swiss army knife for SVMs) computed on CDotFeatures.
#define SG_UNREF(x)
Definition: SGObject.h:53
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
#define SG_SDEBUG(...)
Definition: SGIO.h:167
Abstract base class that provides an interface for performing kernel two-sample test using Maximum Me...
Definition: MMD.h:120
The Kernel base class.
class EuclideanDistance
static T abs(T a)
Definition: Math.h:175

SHOGUN Machine Learning Toolbox - Documentation