SHOGUN  4.1.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
KernelRidgeRegression.cpp
浏览该文件的文档.
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
virtual bool init(CFeatures *lhs, CFeatures *rhs)
Definition: Kernel.cpp:98
SGVector< int32_t > m_svs
#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
via pseudo inverse
virtual int32_t get_num_vectors() const =0
CLabels * m_labels
Definition: Machine.h:361
#define SG_ERROR(...)
Definition: SGIO.h:129
index_t num_cols
Definition: SGMatrix.h:378
float64_t kernel(int32_t idx_a, int32_t idx_b)
Definition: Kernel.h:206
A generic KernelMachine interface.
Definition: KernelMachine.h:51
virtual int32_t get_num_vec_lhs()
Definition: Kernel.h:516
SGMatrix< float64_t > get_kernel_matrix()
Definition: Kernel.h:219
#define SG_SET_LOCALE_C
Definition: SGIO.h:85
index_t num_rows
Definition: SGMatrix.h:376
SGVector< float64_t > m_alpha
index_t vlen
Definition: SGVector.h:494
#define ASSERT(x)
Definition: SGIO.h:201
shogun vector
double float64_t
Definition: common.h:50
virtual int32_t get_num_vec_rhs()
Definition: Kernel.h:525
or gauss-seidel iterative method
virtual bool save(FILE *dstfile)
SGVector< float64_t > get_alphas()
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
The class Features is the base class of all feature objects.
Definition: Features.h:68
The Kernel base class.
Definition: Kernel.h:158
void set_kernel(CKernel *k)
#define SG_ADD(...)
Definition: SGObject.h:81
virtual bool train_machine(CFeatures *data=NULL)
virtual bool has_features()
Definition: Kernel.h:534
virtual void set_labels(CLabels *lab)
Definition: Machine.cpp:65
virtual bool load(FILE *srcfile)

SHOGUN 机器学习工具包 - 项目文档