SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 
13 
14 
15 using namespace shogun;
16 
19 {
20  init();
21 }
22 
24  CKernelTwoSampleTest* mmd, float64_t lambda) :
26 {
27  /* currently, this method is only developed for the linear time MMD */
28  REQUIRE(dynamic_cast<CLinearTimeMMD*>(mmd), "%s::%s(): Only "
29  "CLinearTimeMMD is currently supported! Provided instance is "
30  "\"%s\"\n", get_name(), get_name(), mmd->get_name());
31 
32  init();
33 
34  m_lambda=lambda;
35 }
36 
38 {
39 }
40 
41 void CMMDKernelSelectionCombOpt::init()
42 {
43  /* set to a sensible standard value that proved to be useful in
44  * experiments, see NIPS paper */
45  m_lambda=1E-5;
46 
47  SG_ADD(&m_lambda, "lambda", "Regularization parameter lambda",
49 }
50 
52 {
53  /* cast is safe due to assertion in constructor */
55  index_t num_kernels=kernel->get_num_subkernels();
56  SG_UNREF(kernel);
57 
58  /* allocate space for MMDs and Q matrix */
59  SGVector<float64_t> mmds(num_kernels);
60 
61  /* free matrix by hand since it is static */
62  SG_FREE(m_Q.matrix);
63  m_Q.matrix=NULL;
64  m_Q.num_rows=0;
65  m_Q.num_cols=0;
66  m_Q=SGMatrix<float64_t>(num_kernels, num_kernels, false);
67 
68  /* online compute mmds and covariance matrix Q of kernels */
69  ((CLinearTimeMMD*)m_estimator)->compute_statistic_and_Q(mmds, m_Q);
70 
71  /* evtl regularize to avoid numerical problems (see NIPS paper) */
72  if (m_lambda)
73  {
74  SG_DEBUG("regularizing matrix Q by adding %f to diagonal\n", m_lambda)
75  for (index_t i=0; i<num_kernels; ++i)
76  m_Q(i,i)+=m_lambda;
77  }
78 
80  {
81  m_Q.display_matrix("(regularized) Q");
82  mmds.display_vector("mmds");
83  }
84 
85  /* solve the generated problem */
87 
88  /* free matrix by hand since it is static (again) */
89  SG_FREE(m_Q.matrix);
90  m_Q.matrix=NULL;
91  m_Q.num_rows=0;
92  m_Q.num_cols=0;
93 
94  return result;
95 }
96 

SHOGUN Machine Learning Toolbox - Documentation