![]() |
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_RBFKERNEL_H_ 00044 #define _GURLS_RBFKERNEL_H_ 00045 00046 00047 #include "gurls++/kernel.h" 00048 #include "gurls++/gmath.h" 00049 #include "gurls++/utils.h" 00050 00051 namespace gurls { 00052 00058 template <typename T> 00059 class KernelRBF: public Kernel<T> 00060 { 00061 public: 00074 GurlsOptionsList* execute(const gMat2D<T>& X, const gMat2D<T>& Y, const GurlsOptionsList& opt) throw(gException); 00075 }; 00076 00077 template<typename T> 00078 GurlsOptionsList *KernelRBF<T>::execute(const gMat2D<T>& X, const gMat2D<T>& /*Y*/, const GurlsOptionsList &opt) throw(gException) 00079 { 00080 const int xr = X.rows(); 00081 const int xc = X.cols(); 00082 00083 00084 // if ~isfield(opt.kernel,'distance') 00085 // opt.kernel.distance = distance(X',X'); 00086 // kernel.distance = opt.kernel.distance; 00087 // end 00088 00089 GurlsOptionsList* kernel = new GurlsOptionsList("kernel"); 00090 gMat2D<T> *dist; 00091 00092 bool oldDistance = false; 00093 00094 if(opt.hasOpt("kernel")) 00095 { 00096 const GurlsOptionsList* opt_kernel = opt.getOptAs<GurlsOptionsList>("kernel"); 00097 00098 if(opt_kernel->hasOpt("distance")) 00099 oldDistance = true; 00100 } 00101 00102 if(oldDistance) 00103 { 00104 const gMat2D<T> &opt_dist = opt.getOptValue<OptMatrix<gMat2D<T> > >("kernel.distance"); 00105 00106 dist = new gMat2D<T>(opt_dist); 00107 } 00108 else 00109 { 00110 dist = new gMat2D<T>(xr, xr); 00111 00112 distance_transposed(X.getData(), X.getData(), xc, xr, xr, dist->getData()); 00113 } 00114 00115 00116 kernel->addOpt("distance", new OptMatrix<gMat2D<T> >(*dist)); 00117 00118 double sigma = opt.getOptValue<OptNumber>("paramsel.sigma"); 00119 00120 const int len = xr*xr; 00121 gMat2D<T> *K = new gMat2D<T>(dist->getData(), xr, xr, true); 00122 00123 // D = -(opt.kernel.distance); 00124 // K = exp(D/(opt.paramsel.sigma^2)); 00125 scal(len, (T)(-1.0/pow(sigma, 2)), K->getData(), 1); 00126 exp(K->getData(), len); 00127 00128 // kernel.type = 'rbf'; 00129 kernel->addOpt("type", "rbf"); 00130 00131 kernel->addOpt("K", new OptMatrix<gMat2D<T> >(*K)); 00132 00133 return kernel; 00134 } 00135 00136 } 00137 00138 #endif //_GURLS_RBFKERNEL_H_