![]() |
GURLS++
2.0.00
C++ Implementation of GURLS Matlab Toolbox
|
00001 /* 00002 * The GURLS Package in C++ 00003 * 00004 * Copyright (C) 2011-1013, IIT@MIT Lab 00005 * All rights reserved. 00006 * 00007 * authors: M. Santoro 00008 * email: msantoro@mit.edu 00009 * website: http://cbcl.mit.edu/IIT@MIT/IIT@MIT.html 00010 * 00011 * Redistribution and use in source and binary forms, with or without 00012 * modification, are permitted provided that the following conditions 00013 * are met: 00014 * 00015 * * Redistributions of source code must retain the above 00016 * copyright notice, this list of conditions and the following 00017 * disclaimer. 00018 * * Redistributions in binary form must reproduce the above 00019 * copyright notice, this list of conditions and the following 00020 * disclaimer in the documentation and/or other materials 00021 * provided with the distribution. 00022 * * Neither the name(s) of the copyright holders nor the names 00023 * of its contributors or of the Massacusetts Institute of 00024 * Technology or of the Italian Institute of Technology may be 00025 * used to endorse or promote products derived from this software 00026 * without specific prior written permission. 00027 * 00028 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00029 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00030 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00031 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00032 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00033 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00034 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00035 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00036 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00037 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00038 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00039 * POSSIBILITY OF SUCH DAMAGE. 00040 */ 00041 00042 00043 #ifndef _GURLS_RLSDUALR_H_ 00044 #define _GURLS_RLSDUALR_H_ 00045 00046 #include "gurls++/optimization.h" 00047 #include "gurls++/utils.h" 00048 #include "gurls++/gmath.h" 00049 00050 #include <set> 00051 00052 namespace gurls { 00053 00059 template <typename T> 00060 class RLSDualr: public Optimizer<T>{ 00061 00062 public: 00080 GurlsOptionsList* execute(const gMat2D<T>& X, const gMat2D<T>& Y, const GurlsOptionsList& opt); 00081 }; 00082 00083 00084 template <typename T> 00085 GurlsOptionsList* RLSDualr<T>::execute(const gMat2D<T>& X, const gMat2D<T>& Y, const GurlsOptionsList& opt) 00086 { 00087 // lambda = opt.singlelambda(opt.paramsel.lambdas); 00088 const gMat2D<T> &ll = opt.getOptValue<OptMatrix<gMat2D<T> > >("paramsel.lambdas"); 00089 T lambda = opt.getOptAs<OptFunction>("singlelambda")->getValue(ll.getData(), ll.getSize()); 00090 00091 const GurlsOptionsList* kernel = opt.getOptAs<GurlsOptionsList>("kernel"); 00092 const gMat2D<T>& K_mat = kernel->getOptValue<OptMatrix<gMat2D<T> > >("K"); 00093 00094 T* K = new T[K_mat.getSize()]; 00095 copy(K, K_mat.getData(), K_mat.getSize()); 00096 00097 //n = size(opt.kernel.K,1); 00098 const unsigned long n = K_mat.rows(); 00099 00100 //T = size(y,2); 00101 const unsigned long t = Y.cols(); 00102 00103 const T coeff = n*lambda; 00104 unsigned long i=0; 00105 for(T* it = K; i<n; ++i, it+=n+1) 00106 *it += coeff; 00107 00108 00109 // [Q,L,V] = tygert_svd(K,n); 00110 // Q = double(Q); 00111 // L = double(diag(L)); 00112 T *Q = new T[n*n]; 00113 T *L = new T[n]; 00114 T *V = NULL; 00115 00116 // k = round(opt.eig_percentage*n/100); 00117 unsigned long k = static_cast<unsigned long>(gurls::round((opt.getOptAsNumber("eig_percentage")*n)/100.0)); 00118 random_svd(K, n, n, Q, L, V, k); 00119 00120 00121 gMat2D<T> *retC = new gMat2D<T>(n,t); 00122 T* work = new T[n*(n+1)]; 00123 00124 T* Qty = new T[n*Y.cols()]; 00125 dot(Q, Y.getData(), Qty, n, n, Y.rows(), Y.cols(), n, Y.cols(), CblasTrans, CblasNoTrans, CblasColMajor); 00126 00127 rls_eigen(Q, L, Qty, retC->getData(), lambda, n, n, n, n, n, t, work); 00128 00129 delete [] Qty; 00130 delete [] work; 00131 delete [] Q; 00132 delete [] L; 00133 00134 GurlsOptionsList* optimizer = new GurlsOptionsList("optimizer"); 00135 00136 00137 // if strcmp(opt.kernel.type, 'linear') 00138 if(kernel->getOptAsString("type") == "linear") 00139 { 00140 // cfr.W = X'*cfr.C; 00141 gMat2D<T>* W = new gMat2D<T>(X.cols(), t); 00142 dot(X.getData(), retC->getData(), W->getData(), X.rows(), X.cols(), n, t, X.cols(), t, CblasTrans, CblasNoTrans, CblasColMajor); 00143 00144 optimizer->addOpt("W", new OptMatrix<gMat2D<T> >(*W)); 00145 00146 // cfr.C = []; 00147 gMat2D<T>* emptyC = new gMat2D<T>(); 00148 optimizer->addOpt("C", new OptMatrix<gMat2D<T> >(*emptyC)); 00149 00150 // cfr.X = []; 00151 gMat2D<T>* emptyX = new gMat2D<T>(); 00152 optimizer->addOpt("X", new OptMatrix<gMat2D<T> >(*emptyX)); 00153 00154 delete retC; 00155 } 00156 else 00157 { 00158 // cfr.W = []; 00159 gMat2D<T>* emptyW = new gMat2D<T>(); 00160 optimizer->addOpt("W", new OptMatrix<gMat2D<T> >(*emptyW)); 00161 00162 // cfr.C = retC; 00163 optimizer->addOpt("C", new OptMatrix<gMat2D<T> >(*retC)); 00164 00165 // cfr.X = X; 00166 gMat2D<T>* optX = new gMat2D<T>(X); 00167 optimizer->addOpt("X", new OptMatrix<gMat2D<T> >(*optX)); 00168 } 00169 00170 return optimizer; 00171 } 00172 00173 } 00174 #endif // _GURLS_RLSDUALR_H_