SHOGUN  6.1.3
KernelRidgeRegression.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
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
18
19 using namespace shogun;
20 using namespace Eigen;
21
24 {
25  init();
26 }
27
30 {
31  init();
32
33  set_tau(tau);
34  set_labels(lab);
35  set_kernel(k);
36 }
37
38 void CKernelRidgeRegression::init()
39 {
40  set_tau(1e-6);
41  set_epsilon(0.0001);
42  SG_ADD(&m_tau, "tau", "Regularization parameter", MS_AVAILABLE);
43 }
44
46 {
48  int32_t n = kernel_matrix.num_rows;
49  SGVector<float64_t> y = ((CRegressionLabels*)m_labels)->get_labels();
50
51  for(index_t i=0; i<n; i++)
52  kernel_matrix(i,i) += m_tau;
53
54  Map<MatrixXd> eigen_kernel_matrix(kernel_matrix.matrix, n, n);
55  Map<VectorXd> eigen_alphas(m_alpha.vector, n);
56  Map<VectorXd> eigen_y(y.vector, n);
57
58  LLT<MatrixXd> llt;
59  llt.compute(eigen_kernel_matrix);
60  if (llt.info() != Eigen::Success)
61  {
62  SG_WARNING("Features covariance matrix was not positive definite\n");
63  return false;
64  }
65  eigen_alphas = llt.solve(eigen_y);
66  return true;
67 }
68
70 {
71  if (!m_labels)
72  SG_ERROR("No labels set\n")
73
75  SG_ERROR("Real labels needed for kernel ridge regression.\n")
76
77  if (data)
78  {
79  if (m_labels->get_num_labels() != data->get_num_vectors())
80  SG_ERROR("Number of training vectors does not match number of labels\n")
81  kernel->init(data, data);
82  }
84
86  {
87  SG_ERROR("Number of labels does not match number of kernel"
88  " columns (num_labels=%d cols=%d\n", m_labels->get_num_labels(), kernel->get_num_vec_rhs());
89  }
90
91  // allocate alpha vector
93
94  if(!solve_krr_system())
95  return false;
96
97  /* tell kernel machine that all alphas are needed as'support vectors' */
99  m_svs.range_fill();
100  return true;
101 }
102
104 {
107  return false;
108 }
109
110 bool CKernelRidgeRegression::save(FILE* dstfile)
111 {
114  return false;
115 }
virtual bool init(CFeatures *lhs, CFeatures *rhs)
Definition: Kernel.cpp:97
SGVector< int32_t > m_svs
#define SG_RESET_LOCALE
Definition: SGIO.h:85
virtual ELabelType get_label_type() const =0
Real Labels are real-valued labels.
int32_t index_t
Definition: common.h:72
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
virtual int32_t get_num_labels() const =0
real valued labels (e.g. for regression, classifier outputs)
Definition: LabelTypes.h:22
Definition: SGMatrix.h:25
virtual int32_t get_num_vectors() const =0
CLabels * m_labels
Definition: Machine.h:436
#define SG_ERROR(...)
Definition: SGIO.h:128
A generic KernelMachine interface.
Definition: KernelMachine.h:51
SGMatrix< float64_t > get_kernel_matrix()
#define SG_SET_LOCALE_C
Definition: SGIO.h:84
void set_epsilon(float64_t epsilon)
SGVector< float64_t > m_alpha
#define ASSERT(x)
Definition: SGIO.h:176
double float64_t
Definition: common.h:60
void range_fill(T start=0)
Definition: SGVector.cpp:223
index_t num_rows
Definition: SGMatrix.h:495
virtual void set_tau(float64_t tau)
virtual int32_t get_num_vec_rhs()
virtual bool save(FILE *dstfile)
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
void set_alphas(SGVector< float64_t > alphas)
The class Features is the base class of all feature objects.
Definition: Features.h:69
The Kernel base class.
void set_kernel(CKernel *k)
#define SG_WARNING(...)
Definition: SGIO.h:127