SHOGUN
v2.0.0
|
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 * Copyright (C) 2012 Soeren Sonnenburg 00008 */ 00009 00010 #include <shogun/lib/config.h> 00011 00012 #ifdef HAVE_LAPACK 00013 #include <shogun/regression/LinearRidgeRegression.h> 00014 #include <shogun/mathematics/lapack.h> 00015 #include <shogun/mathematics/Math.h> 00016 #include <shogun/labels/RegressionLabels.h> 00017 00018 using namespace shogun; 00019 00020 CLinearRidgeRegression::CLinearRidgeRegression() 00021 : CLinearMachine() 00022 { 00023 init(); 00024 } 00025 00026 CLinearRidgeRegression::CLinearRidgeRegression(float64_t tau, CDenseFeatures<float64_t>* data, CLabels* lab) 00027 : CLinearMachine() 00028 { 00029 init(); 00030 00031 m_tau=tau; 00032 set_labels(lab); 00033 set_features(data); 00034 } 00035 00036 void CLinearRidgeRegression::init() 00037 { 00038 m_tau=1e-6; 00039 00040 SG_ADD(&m_tau, "tau", "Regularization parameter", MS_AVAILABLE); 00041 } 00042 00043 bool CLinearRidgeRegression::train_machine(CFeatures* data) 00044 { 00045 if (!m_labels) 00046 SG_ERROR("No labels set\n"); 00047 00048 if (!data) 00049 data=features; 00050 00051 if (!data) 00052 SG_ERROR("No features set\n"); 00053 00054 if (m_labels->get_num_labels() != data->get_num_vectors()) 00055 SG_ERROR("Number of training vectors does not match number of labels\n"); 00056 00057 if (data->get_feature_class() != C_DENSE) 00058 SG_ERROR("Expected Dense Features\n"); 00059 00060 if (data->get_feature_type() != F_DREAL) 00061 SG_ERROR("Expected Real Features\n"); 00062 00063 CDenseFeatures<float64_t>* feats=(CDenseFeatures<float64_t>*) data; 00064 int32_t num_feat=feats->get_num_features(); 00065 int32_t num_vec=feats->get_num_vectors(); 00066 00067 // Get kernel matrix 00068 SGMatrix<float64_t> kernel_matrix(num_feat,num_feat); 00069 SGVector<float64_t> y(num_feat); 00070 00071 // init 00072 kernel_matrix.zero(); 00073 y.zero(); 00074 00075 for (int32_t i=0; i<num_feat; i++) 00076 kernel_matrix.matrix[i+i*num_feat]+=m_tau; 00077 00078 for (int32_t i=0; i<num_vec; i++) 00079 { 00080 SGVector<float64_t> v = feats->get_feature_vector(i); 00081 ASSERT(v.vlen==num_feat); 00082 00083 cblas_dger(CblasColMajor, num_feat,num_feat, 1.0, v.vector,1, 00084 v.vector,1, kernel_matrix.matrix, num_feat); 00085 00086 cblas_daxpy(num_feat, ((CRegressionLabels*) m_labels)->get_label(i), v.vector, 1, y.vector, 1); 00087 00088 feats->free_feature_vector(v, i); 00089 } 00090 00091 clapack_dposv(CblasRowMajor,CblasUpper, num_feat, 1, kernel_matrix.matrix, num_feat, 00092 y.vector, num_feat); 00093 00094 set_w(y); 00095 00096 return true; 00097 } 00098 00099 bool CLinearRidgeRegression::load(FILE* srcfile) 00100 { 00101 SG_SET_LOCALE_C; 00102 SG_RESET_LOCALE; 00103 return false; 00104 } 00105 00106 bool CLinearRidgeRegression::save(FILE* dstfile) 00107 { 00108 SG_SET_LOCALE_C; 00109 SG_RESET_LOCALE; 00110 return false; 00111 } 00112 #endif