![]() |
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_RLSRANDFEATS_H_ 00044 #define _GURLS_RLSRANDFEATS_H_ 00045 00046 #include <cmath> 00047 00048 #include "gurls++/optimization.h" 00049 00050 #include "gurls++/gmath.h" 00051 #include "gurls++/gmat2d.h" 00052 #include "gurls++/options.h" 00053 #include "gurls++/optlist.h" 00054 #include "gurls++/utils.h" 00055 00056 namespace gurls { 00057 00069 template <typename T> 00070 class RLSRandFeats: public Optimizer<T>{ 00071 00072 public: 00089 GurlsOptionsList *execute(const gMat2D<T>& X, const gMat2D<T>& Y, const GurlsOptionsList &opt); 00090 }; 00091 00092 00093 template <typename T> 00094 GurlsOptionsList* RLSRandFeats<T>::execute(const gMat2D<T>& X, const gMat2D<T>& Y, const GurlsOptionsList& opt) 00095 { 00096 // lambda = opt.singlelambda(opt.paramsel.lambdas); 00097 const gMat2D<T> &ll = opt.getOptValue<OptMatrix<gMat2D<T> > >("paramsel.lambdas"); 00098 T lambda = opt.getOptAs<OptFunction>("singlelambda")->getValue(ll.getData(), ll.getSize()); 00099 00100 const unsigned long sampleSize = opt.getOptAsNumber("randfeats.samplesize"); 00101 const unsigned long D = opt.getOptAsNumber("randfeats.D"); 00102 00103 // n = size(X,1); 00104 const unsigned long n = X.rows(); 00105 // const unsigned long d = X.cols(); 00106 const unsigned long t = Y.cols(); 00107 00108 // if or(opt.randfeats.samplesize < 0, opt.randfeats.samplesize > n) 00109 // ni = n; 00110 // else 00111 // ni = opt.randfeats.samplesize; 00112 // end 00113 00114 const unsigned long ni = (sampleSize < 0 || sampleSize > n)? n : sampleSize; 00115 00116 const unsigned long D2 = 2*D; 00117 00118 T *XtX = new T[D2*D2]; 00119 T *Xty = new T[D2*t]; 00120 00121 // [XtX,Xty,rls.proj] = rp_factorize_large_real(X,y,opt.randfeats.D,ni); 00122 gMat2D<T> *rls_proj = rp_factorize_large_real(X, Y, D, ni, XtX, Xty); 00123 00124 // rls.W = rls_primal_driver( XtX, Xty, n, lambda ); 00125 gMat2D<T> *W = rls_primal_driver(XtX, Xty, D2, D2, t, lambda); 00126 00127 delete [] XtX; 00128 delete [] Xty; 00129 00130 GurlsOptionsList *optimizer = new GurlsOptionsList("optimizer"); 00131 00132 00133 optimizer->addOpt("proj", new OptMatrix<gMat2D<T> >(*rls_proj)); 00134 00135 // rls.W = rls_primal_driver( XtX, Xty, n, lambda ); 00136 optimizer->addOpt("W", new OptMatrix<gMat2D<T> >(*W)); 00137 00138 // rls.C = []; 00139 gMat2D<T>* emptyC = new gMat2D<T>(); 00140 optimizer->addOpt("C", new OptMatrix<gMat2D<T> >(*emptyC)); 00141 00142 // rls.X = []; 00143 gMat2D<T>* emptyX = new gMat2D<T>(); 00144 optimizer->addOpt("X", new OptMatrix<gMat2D<T> >(*emptyX)); 00145 00146 00147 return optimizer; 00148 } 00149 00150 } 00151 #endif // _GURLS_RLSRANDFEATS_H_ 00152