SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
KernelRidgeRegression.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) 2006 Mikio L. Braun
8  * Written (W) 1999-2009 Soeren Sonnenburg
9  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
10  */
11 
12 #include <shogun/lib/config.h>
13 
14 #ifdef HAVE_LAPACK
19 
20 using namespace shogun;
21 
24 {
25  init();
26 }
27 
30 {
31  init();
32 
33  m_tau=tau;
34  set_labels(lab);
35  set_kernel(k);
36  m_train_func=m;
37 }
38 
39 void CKernelRidgeRegression::init()
40 {
41  m_tau=1e-6;
42  m_epsilon=0.0001;
43  SG_ADD(&m_tau, "tau", "Regularization parameter", MS_AVAILABLE);
44 }
45 
46 bool CKernelRidgeRegression::train_machine_pinv()
47 {
48  // Get kernel matrix
50  int32_t n = kernel_matrix.num_cols;
51  int32_t m = kernel_matrix.num_rows;
52  ASSERT(kernel_matrix.matrix && m>0 && n>0)
53 
54  for(int32_t i=0; i < n; i++)
55  kernel_matrix.matrix[i+i*n]+=m_tau;
56 
57  /* re-set alphas of kernel machine */
58  m_alpha=((CRegressionLabels*) m_labels)->get_labels_copy();
59 
60  /* tell kernel machine that all alphas are needed as'support vectors' */
62  m_svs.range_fill();
63 
64  if (get_alphas().vlen!=n)
65  {
66  SG_ERROR("Number of labels does not match number of kernel"
67  " columns (num_labels=%d cols=%d\n", m_alpha.vlen, n);
68  }
69 
70  clapack_dposv(CblasRowMajor,CblasUpper, n, 1, kernel_matrix.matrix, n,
71  m_alpha.vector, n);
72 
73  return true;
74 }
75 
76 bool CKernelRidgeRegression::train_machine_gs()
77 {
78  int32_t n = kernel->get_num_vec_rhs();
79  int32_t m = kernel->get_num_vec_lhs();
80  ASSERT(m>0 && n>0)
81 
82  // re-set alphas of kernel machine
84  float64_t alpha_old;
85 
86  b=((CRegressionLabels*) m_labels)->get_labels_copy();
87  m_alpha=((CRegressionLabels*) m_labels)->get_labels_copy();
88  m_alpha.zero();
89 
90  // tell kernel machine that all alphas are needed as 'support vectors'
92  m_svs.range_fill();
93 
94  if (get_alphas().vlen!=n)
95  {
96  SG_ERROR("Number of labels does not match number of kernel"
97  " columns (num_labels=%d cols=%d\n", m_alpha.vlen, n);
98  }
99 
100  // Gauss-Seidel iterative method
101  float64_t sigma, err, d;
102  bool flag=true;
103  while(flag)
104  {
105  err=0.0;
106  for(int32_t i=0; i<n; i++)
107  {
108  sigma=b[i];
109  for(int32_t j=0; j<n; j++)
110  if (i!=j)
111  sigma-=kernel->kernel(j, i)*m_alpha[j];
112  alpha_old=m_alpha[i];
113  m_alpha[i]=sigma/(kernel->kernel(i, i)+m_tau);
114  d=fabs(alpha_old-m_alpha[i]);
115  if(d>err)
116  err=d;
117  }
118  if (err<=m_epsilon)
119  flag=false;
120  }
121 
122  return true;
123 }
124 
126 {
127  if (!m_labels)
128  SG_ERROR("No labels set\n")
129 
131  SG_ERROR("Real labels needed for kernel ridge regression.\n")
132 
133  if (data)
134  {
135  if (m_labels->get_num_labels() != data->get_num_vectors())
136  SG_ERROR("Number of training vectors does not match number of labels\n")
137  kernel->init(data, data);
138  }
140 
141  switch (m_train_func)
142  {
143  case PINV:
144  return train_machine_pinv();
145  break;
146  case GS:
147  return train_machine_gs();
148  break;
149  default:
150  return train_machine_pinv();
151  break;
152  }
153 }
154 
155 bool CKernelRidgeRegression::load(FILE* srcfile)
156 {
159  return false;
160 }
161 
162 bool CKernelRidgeRegression::save(FILE* dstfile)
163 {
166  return false;
167 }
168 #endif

SHOGUN Machine Learning Toolbox - Documentation