SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
KRR.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
15 #include <shogun/regression/KRR.h>
18 
19 using namespace shogun;
20 
23 {
24  alpha=NULL;
25  tau=1e-6;
26 }
27 
30 {
31  tau=t;
32  set_labels(lab);
33  set_kernel(k);
34  alpha=NULL;
35 }
36 
37 
39 {
40  SG_FREE(alpha);
41 }
42 
44 {
45  SG_FREE(alpha);
46 
47  ASSERT(labels);
48  if (data)
49  {
50  if (labels->get_num_labels() != data->get_num_vectors())
51  SG_ERROR("Number of training vectors does not match number of labels\n");
52  kernel->init(data, data);
53  }
55 
56  // Get kernel matrix
58  int32_t n = kernel_matrix.num_cols;
59  int32_t m = kernel_matrix.num_rows;
60  ASSERT(kernel_matrix.matrix && m>0 && n>0);
61 
62  for(int32_t i=0; i < n; i++)
63  kernel_matrix.matrix[i+i*n]+=tau;
64 
65  // Get labels
66  if (!labels)
67  SG_ERROR("No labels set\n");
68 
70 
71  alpha=CMath::clone_vector(alpha_orig.vector, alpha_orig.vlen);
72 
73  if (alpha_orig.vlen!=n)
74  {
75  SG_ERROR("Number of labels does not match number of kernel"
76  " columns (num_labels=%d cols=%d\n", alpha_orig.vlen, n);
77  }
78 
79  clapack_dposv(CblasRowMajor,CblasUpper, n, 1, kernel_matrix.matrix, n, alpha, n);
80 
81  SG_FREE(kernel_matrix.matrix);
82  return true;
83 }
84 
85 bool CKRR::load(FILE* srcfile)
86 {
89  return false;
90 }
91 
92 bool CKRR::save(FILE* dstfile)
93 {
96  return false;
97 }
98 
100 {
101  ASSERT(kernel);
102 
103  // Get kernel matrix
105  int32_t n = kernel_matrix.num_cols;
106  int32_t m = kernel_matrix.num_rows;
107  ASSERT(kernel_matrix.matrix && m>0 && n>0);
108 
109  SGVector<float64_t> Yh(n);
110 
111  // predict
112  // K is symmetric, CblasColMajor is same as CblasRowMajor
113  // and used that way in the origin call:
114  // dgemv('T', m, n, 1.0, K, m, alpha, 1, 0.0, Yh, 1);
115  int m_int = (int) m;
116  int n_int = (int) n;
117  cblas_dgemv(CblasColMajor, CblasTrans, m_int, n_int, 1.0, (double*) kernel_matrix.matrix,
118  m_int, (double*) alpha, 1, 0.0, (double*) Yh.vector, 1);
119 
120  SG_FREE(kernel_matrix.matrix);
121 
122  return new CLabels(Yh);
123 }
124 
125 float64_t CKRR::apply(int32_t num)
126 {
127  ASSERT(kernel);
128 
129  // Get kernel matrix
130  // TODO: use get_kernel_column instead of computing the whole matrix!
132  int32_t n = kernel_matrix.num_cols;
133  int32_t m = kernel_matrix.num_rows;
134  ASSERT(kernel_matrix.matrix && m>0 && n>0);
135 
136  float64_t Yh;
137 
138  // predict
139  Yh = CMath::dot(kernel_matrix.matrix + m*num, alpha, m);
140 
141  SG_FREE(kernel_matrix.matrix);
142  return Yh;
143 }
144 
145 #endif

SHOGUN Machine Learning Toolbox - Documentation