SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MMDKernelSelectionCombOpt.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2012-2013 Heiko Strathmann
8  */
9 
10 #include <shogun/lib/config.h>
11 #ifdef USE_GPL_SHOGUN
12 
16 
17 
18 using namespace shogun;
19 
20 CMMDKernelSelectionCombOpt::CMMDKernelSelectionCombOpt() :
21  CMMDKernelSelectionComb()
22 {
23  init();
24 }
25 
26 CMMDKernelSelectionCombOpt::CMMDKernelSelectionCombOpt(
27  CKernelTwoSampleTest* mmd, float64_t lambda) :
28  CMMDKernelSelectionComb(mmd)
29 {
30  /* currently, this method is only developed for the linear time MMD */
31  REQUIRE(dynamic_cast<CLinearTimeMMD*>(mmd), "%s::%s(): Only "
32  "CLinearTimeMMD is currently supported! Provided instance is "
33  "\"%s\"\n", get_name(), get_name(), mmd->get_name());
34 
35  init();
36 
37  m_lambda=lambda;
38 }
39 
40 CMMDKernelSelectionCombOpt::~CMMDKernelSelectionCombOpt()
41 {
42 }
43 
44 void CMMDKernelSelectionCombOpt::init()
45 {
46  /* set to a sensible standard value that proved to be useful in
47  * experiments, see NIPS paper */
48  m_lambda=1E-5;
49 
50  SG_ADD(&m_lambda, "lambda", "Regularization parameter lambda",
52 }
53 
54 SGVector<float64_t> CMMDKernelSelectionCombOpt::compute_measures()
55 {
56  /* cast is safe due to assertion in constructor */
57  CCombinedKernel* kernel=(CCombinedKernel*)m_estimator->get_kernel();
58  index_t num_kernels=kernel->get_num_subkernels();
59  SG_UNREF(kernel);
60 
61  /* allocate space for MMDs and Q matrix */
62  SGVector<float64_t> mmds(num_kernels);
63 
64  /* free matrix by hand since it is static */
65  SG_FREE(m_Q.matrix);
66  m_Q.matrix=NULL;
67  m_Q.num_rows=0;
68  m_Q.num_cols=0;
69  m_Q=SGMatrix<float64_t>(num_kernels, num_kernels, false);
70 
71  /* online compute mmds and covariance matrix Q of kernels */
72  ((CLinearTimeMMD*)m_estimator)->compute_statistic_and_Q(mmds, m_Q);
73 
74  /* evtl regularize to avoid numerical problems (see NIPS paper) */
75  if (m_lambda)
76  {
77  SG_DEBUG("regularizing matrix Q by adding %f to diagonal\n", m_lambda)
78  for (index_t i=0; i<num_kernels; ++i)
79  m_Q(i,i)+=m_lambda;
80  }
81 
82  if (sg_io->get_loglevel()==MSG_DEBUG)
83  {
84  m_Q.display_matrix("(regularized) Q");
85  mmds.display_vector("mmds");
86  }
87 
88  /* solve the generated problem */
89  SGVector<float64_t> result=CMMDKernelSelectionComb::solve_optimization(mmds);
90 
91  /* free matrix by hand since it is static (again) */
92  SG_FREE(m_Q.matrix);
93  m_Q.matrix=NULL;
94  m_Q.num_rows=0;
95  m_Q.num_cols=0;
96 
97  return result;
98 }
99 #endif //USE_GPL_SHOGUN
int32_t index_t
Definition: common.h:62
#define REQUIRE(x,...)
Definition: SGIO.h:206
Kernel two sample test base class. Provides an interface for performing a two-sample test using a ker...
SGIO * sg_io
Definition: init.cpp:36
CKernel * get_kernel(int32_t idx)
double float64_t
Definition: common.h:50
The Combined kernel is used to combine a number of kernels into a single CombinedKernel object by lin...
virtual const char * get_name() const =0
#define SG_UNREF(x)
Definition: SGObject.h:52
#define SG_DEBUG(...)
Definition: SGIO.h:107
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
This class implements the linear time Maximum Mean Statistic as described in [1] for streaming data (...
Definition: LinearTimeMMD.h:66
virtual const char * get_name() const
#define SG_ADD(...)
Definition: SGObject.h:81

SHOGUN Machine Learning Toolbox - Documentation