SHOGUN  5.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
KernelMeanMatching.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  * Copyright (W) 2012 Sergey Lisitsyn
8  */
9 
10 #include <shogun/lib/config.h>
11 #ifdef USE_GPL_SHOGUN
12 
14 #include <shogun/lib/external/libqp.h>
15 
16 
17 static float64_t* kmm_K = NULL;
18 static int32_t kmm_K_ld = 0;
19 
20 static const float64_t* kmm_get_col(uint32_t i)
21 {
22  return kmm_K + kmm_K_ld*i;
23 }
24 
25 namespace shogun
26 {
27 CKernelMeanMatching::CKernelMeanMatching() :
28  CSGObject(), m_kernel(NULL)
29 {
30 }
31 
32 CKernelMeanMatching::CKernelMeanMatching(CKernel* kernel, SGVector<index_t> training_indices,
33  SGVector<index_t> test_indices) :
34  CSGObject(), m_kernel(NULL)
35 {
36  set_kernel(kernel);
37  set_training_indices(training_indices);
38  set_test_indices(test_indices);
39 }
40 
41 SGVector<float64_t> CKernelMeanMatching::compute_weights()
42 {
43  int32_t i,j;
44  ASSERT(m_kernel)
45  ASSERT(m_training_indices.vlen)
46  ASSERT(m_test_indices.vlen)
47 
48  int32_t n_tr = m_training_indices.vlen;
49  int32_t n_te = m_test_indices.vlen;
50 
51  SGVector<float64_t> weights(n_tr);
52  weights.zero();
53 
54  kmm_K = SG_MALLOC(float64_t, n_tr*n_tr);
55  kmm_K_ld = n_tr;
56  float64_t* diag_K = SG_MALLOC(float64_t, n_tr);
57  for (i=0; i<n_tr; i++)
58  {
59  float64_t d = m_kernel->kernel(m_training_indices[i], m_training_indices[i]);
60  diag_K[i] = d;
61  kmm_K[i*n_tr+i] = d;
62  for (j=i+1; j<n_tr; j++)
63  {
64  d = m_kernel->kernel(m_training_indices[i],m_training_indices[j]);
65  kmm_K[i*n_tr+j] = d;
66  kmm_K[j*n_tr+i] = d;
67  }
68  }
69  float64_t* kappa = SG_MALLOC(float64_t, n_tr);
70  for (i=0; i<n_tr; i++)
71  {
72  float64_t avg = 0.0;
73  for (j=0; j<n_te; j++)
74  avg+= m_kernel->kernel(m_training_indices[i],m_test_indices[j]);
75 
76  avg *= float64_t(n_tr)/n_te;
77  kappa[i] = -avg;
78  }
79  float64_t* a = SG_MALLOC(float64_t, n_tr);
80  for (i=0; i<n_tr; i++) a[i] = 1.0;
81  float64_t* LB = SG_MALLOC(float64_t, n_tr);
82  float64_t* UB = SG_MALLOC(float64_t, n_tr);
83  float64_t B = 2.0;
84  for (i=0; i<n_tr; i++)
85  {
86  LB[i] = 0.0;
87  UB[i] = B;
88  }
89  for (i=0; i<n_tr; i++)
90  weights[i] = 1.0/float64_t(n_tr);
91 
92  libqp_state_T result =
93  libqp_gsmo_solver(&kmm_get_col,diag_K,kappa,a,1.0,LB,UB,weights,n_tr,1000,1e-9,NULL);
94 
95  SG_DEBUG("libqp exitflag=%d, %d iterations passed, primal objective=%f\n",
96  result.exitflag,result.nIter,result.QP);
97 
98  SG_FREE(kappa);
99  SG_FREE(a);
100  SG_FREE(LB);
101  SG_FREE(UB);
102  SG_FREE(diag_K);
103  SG_FREE(kmm_K);
104 
105  return weights;
106 }
107 
108 }
109 #endif //USE_GPL_SHOGUN
#define ASSERT(x)
Definition: SGIO.h:201
double float64_t
Definition: common.h:50
#define SG_DEBUG(...)
Definition: SGIO.h:107
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
void set_kernel(CKernel *k)

SHOGUN Machine Learning Toolbox - Documentation