SHOGUN
v3.2.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 2012-2013 Heiko Strathmann 00008 */ 00009 00010 #include <shogun/statistics/MMDKernelSelectionCombOpt.h> 00011 #include <shogun/statistics/LinearTimeMMD.h> 00012 #include <shogun/kernel/CombinedKernel.h> 00013 00014 00015 using namespace shogun; 00016 00017 CMMDKernelSelectionCombOpt::CMMDKernelSelectionCombOpt() : 00018 CMMDKernelSelectionComb() 00019 { 00020 init(); 00021 } 00022 00023 CMMDKernelSelectionCombOpt::CMMDKernelSelectionCombOpt( 00024 CKernelTwoSampleTestStatistic* mmd, float64_t lambda) : 00025 CMMDKernelSelectionComb(mmd) 00026 { 00027 /* currently, this method is only developed for the linear time MMD */ 00028 REQUIRE(dynamic_cast<CLinearTimeMMD*>(mmd), "%s::%s(): Only " 00029 "CLinearTimeMMD is currently supported! Provided instance is " 00030 "\"%s\"\n", get_name(), get_name(), mmd->get_name()); 00031 00032 init(); 00033 00034 m_lambda=lambda; 00035 } 00036 00037 CMMDKernelSelectionCombOpt::~CMMDKernelSelectionCombOpt() 00038 { 00039 } 00040 00041 void CMMDKernelSelectionCombOpt::init() 00042 { 00043 /* set to a sensible standard value that proved to be useful in 00044 * experiments, see NIPS paper */ 00045 m_lambda=1E-5; 00046 00047 SG_ADD(&m_lambda, "lambda", "Regularization parameter lambda", 00048 MS_NOT_AVAILABLE); 00049 } 00050 00051 #ifdef HAVE_LAPACK 00052 SGVector<float64_t> CMMDKernelSelectionCombOpt::compute_measures() 00053 { 00054 /* cast is safe due to assertion in constructor */ 00055 CCombinedKernel* kernel=(CCombinedKernel*)m_mmd->get_kernel(); 00056 index_t num_kernels=kernel->get_num_subkernels(); 00057 SG_UNREF(kernel); 00058 00059 /* allocate space for MMDs and Q matrix */ 00060 SGVector<float64_t> mmds(num_kernels); 00061 00062 /* free matrix by hand since it is static */ 00063 SG_FREE(m_Q.matrix); 00064 m_Q.matrix=NULL; 00065 m_Q.num_rows=0; 00066 m_Q.num_cols=0; 00067 m_Q=SGMatrix<float64_t>(num_kernels, num_kernels, false); 00068 00069 /* online compute mmds and covariance matrix Q of kernels */ 00070 ((CLinearTimeMMD*)m_mmd)->compute_statistic_and_Q(mmds, m_Q); 00071 00072 /* evtl regularize to avoid numerical problems (see NIPS paper) */ 00073 if (m_lambda) 00074 { 00075 SG_DEBUG("regularizing matrix Q by adding %f to diagonal\n", m_lambda) 00076 for (index_t i=0; i<num_kernels; ++i) 00077 m_Q(i,i)+=m_lambda; 00078 } 00079 00080 if (sg_io->get_loglevel()==MSG_DEBUG) 00081 { 00082 m_Q.display_matrix("(regularized) Q"); 00083 mmds.display_vector("mmds"); 00084 } 00085 00086 /* solve the generated problem */ 00087 SGVector<float64_t> result=CMMDKernelSelectionComb::solve_optimization(mmds); 00088 00089 /* free matrix by hand since it is static (again) */ 00090 SG_FREE(m_Q.matrix); 00091 m_Q.matrix=NULL; 00092 m_Q.num_rows=0; 00093 m_Q.num_cols=0; 00094 00095 return result; 00096 } 00097 00098 #endif