SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 
18 
19 using namespace shogun;
20 using namespace Eigen;
21 
24 {
25  init();
26 }
27 
30 {
31  init();
32 
33  m_tau=tau;
34  set_labels(lab);
35  set_kernel(k);
36 }
37 
38 void CKernelRidgeRegression::init()
39 {
40  m_tau=1e-6;
41  m_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 
103 bool CKernelRidgeRegression::load(FILE* srcfile)
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:98
SGVector< int32_t > m_svs
void range_fill(T start=0)
Definition: SGVector.cpp:171
#define SG_RESET_LOCALE
Definition: SGIO.h:86
virtual ELabelType get_label_type() const =0
Real Labels are real-valued labels.
int32_t index_t
Definition: common.h:62
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:20
virtual int32_t get_num_vectors() const =0
CLabels * m_labels
Definition: Machine.h:361
#define SG_ERROR(...)
Definition: SGIO.h:129
A generic KernelMachine interface.
Definition: KernelMachine.h:51
SGMatrix< float64_t > get_kernel_matrix()
Definition: Kernel.h:220
#define SG_SET_LOCALE_C
Definition: SGIO.h:85
index_t num_rows
Definition: SGMatrix.h:374
SGVector< float64_t > m_alpha
index_t vlen
Definition: SGVector.h:492
#define ASSERT(x)
Definition: SGIO.h:201
double float64_t
Definition: common.h:50
virtual int32_t get_num_vec_rhs()
Definition: Kernel.h:526
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:68
The Kernel base class.
Definition: Kernel.h:159
void set_kernel(CKernel *k)
#define SG_WARNING(...)
Definition: SGIO.h:128
#define SG_ADD(...)
Definition: SGObject.h:84
virtual bool train_machine(CFeatures *data=NULL)
virtual bool has_features()
Definition: Kernel.h:535
virtual void set_labels(CLabels *lab)
Definition: Machine.cpp:65
virtual bool load(FILE *srcfile)

SHOGUN Machine Learning Toolbox - Documentation