KRR.cpp

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2006 Mikio L. Braun
00008  * Written (W) 1999-2009 Soeren Sonnenburg
00009  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
00010  */
00011 
00012 #include "lib/config.h"
00013 
00014 #ifdef HAVE_LAPACK
00015 #include "regression/KRR.h"
00016 #include "lib/lapack.h"
00017 #include "lib/Mathematics.h"
00018 
00019 using namespace shogun;
00020 
00021 CKRR::CKRR()
00022 : CKernelMachine()
00023 {
00024     alpha=NULL;
00025     tau=1e-6;
00026 }
00027 
00028 CKRR::CKRR(float64_t t, CKernel* k, CLabels* lab)
00029 : CKernelMachine()
00030 {
00031     tau=t;
00032     set_labels(lab);
00033     set_kernel(k);
00034     alpha=NULL;
00035 }
00036 
00037 
00038 CKRR::~CKRR()
00039 {
00040     delete[] alpha;
00041 }
00042 
00043 bool CKRR::train(CFeatures* data)
00044 {
00045     delete[] alpha;
00046 
00047     ASSERT(labels);
00048     if (data)
00049     {
00050         if (labels->get_num_labels() != data->get_num_vectors())
00051             SG_ERROR("Number of training vectors does not match number of labels\n");
00052         kernel->init(data, data);
00053     }
00054     ASSERT(kernel && kernel->has_features());
00055 
00056     // Get kernel matrix
00057     int32_t m=0;
00058     int32_t n=0;
00059     float64_t *K = kernel->get_kernel_matrix<float64_t>(m, n, NULL);
00060     ASSERT(K && m>0 && n>0);
00061 
00062     for(int32_t i=0; i < n; i++)
00063         K[i+i*n]+=tau;
00064 
00065     // Get labels
00066     int32_t numlabels=0;
00067     alpha=labels->get_labels(numlabels);
00068     ASSERT(alpha && numlabels==n);
00069 
00070     clapack_dposv(CblasRowMajor,CblasUpper, n, 1, K, n, alpha, n);
00071 
00072     delete[] K;
00073     return true;
00074 }
00075 
00076 bool CKRR::load(FILE* srcfile)
00077 {
00078     return false;
00079 }
00080 
00081 bool CKRR::save(FILE* dstfile)
00082 {
00083     return false;
00084 }
00085 
00086 CLabels* CKRR::classify()
00087 {
00088     ASSERT(kernel);
00089 
00090     // Get kernel matrix
00091     int32_t m=0;
00092     int32_t n=0;
00093     float64_t* K=kernel->get_kernel_matrix<float64_t>(m, n, NULL);
00094     ASSERT(K && m>0 && n>0);
00095     float64_t* Yh=new float64_t[n];
00096 
00097     // predict
00098     // K is symmetric, CblasColMajor is same as CblasRowMajor 
00099     // and used that way in the origin call:
00100     // dgemv('T', m, n, 1.0, K, m, alpha, 1, 0.0, Yh, 1);
00101     int m_int = (int) m;
00102     int n_int = (int) n;
00103     cblas_dgemv(CblasColMajor, CblasTrans, m_int, n_int, 1.0, (double*) K,
00104         m_int, (double*) alpha, 1, 0.0, (double*) Yh, 1);
00105 
00106     delete[] K;
00107 
00108     CLabels* output=new CLabels(n);
00109     output->set_labels(Yh, n);
00110 
00111     delete[] Yh;
00112 
00113     return output;
00114 }
00115 
00116 float64_t CKRR::classify_example(int32_t num)
00117 {
00118     ASSERT(kernel);
00119 
00120     // Get kernel matrix
00121     int32_t m=0;
00122     int32_t n=0;
00123     // TODO: use get_kernel_column instead of computing the whole matrix!
00124     float64_t* K=kernel->get_kernel_matrix<float64_t>(m, n, NULL);
00125     ASSERT(K && m>0 && n>0);
00126     float64_t Yh;
00127 
00128     // predict
00129     Yh = CMath::dot(K + m*num, alpha, m);
00130 
00131     delete[] K;
00132     return Yh;
00133 }
00134 
00135 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation