📄 svmfusvmtest.cpp
字号:
// This is a part of the SvmFu library, a library for training// Support Vector Machines.// Copyright (C) 2000 rif and MIT//// Contact: rif@mit.edu// This program is free software; you can redistribute it and/or// modify it under the terms of the GNU General Public License as// published by the Free Software Foundation; either version 2 of the// License, or (at your option) any later version.// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU// General Public License for more details.// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA// 02111-1307 USA#include "SvmFuSvmTest.h"#ifdef CallTemplate#undef CallTemplate#endif#define CallTemplate(rettype, funcname) \template<class DataPt, class KernVal> \rettype SvmTest<DataPt, KernVal>::funcnametemplate<class DataPt, class KernVal> SvmTest<DataPt, KernVal>::SvmTest(int svmSize, double *alphas, DataPt *trnSetPtr, double b, int *y, const KernVal (*kernProdFuncPtr) (const DataPt &pt1, const DataPt &pt2)) : svmSize_(svmSize), alphas_(alphas), trnSetPtr_(trnSetPtr), b_(b), y_(y), kernProdFuncPtr_(kernProdFuncPtr){ // vking: svm loading code moved out of library}template<class DataPt, class KernVal> SvmTest<DataPt, KernVal>::~SvmTest() { /* these are all externally allocated now delete[] alphas_; delete[] y_; for (int i = 0; i < svmSize_; i++) { delete trnSetPtr_[i]; } delete[] trnSetPtr_; */} CallTemplate(int, classifyDataPt)(const DataPt &pt) const { return (outputAtDataPt(pt) > 0 ? 1 : -1);} CallTemplate(double, outputAtDataPt)(const DataPt &pt) const { double result = 0.0; for (int i = 0; i < svmSize_; i++) result += y_[i]*alphas_[i]*(double)kernProdFuncPtr_(trnSetPtr_[i], pt); result += b_; return result;} CallTemplate(double, computeDataSetPerformance) (int dataSetSize, DataPt *dataSet, int *y, bool printInfo) const{ int correct = 0; for (int i = 0; i < dataSetSize; i++) { if (classifyDataPt(dataSet[i]) == y[i]) { correct++; } if (!(i % 500) && (i > 0) && printInfo) { cout << i << ": " << correct << endl; } } double perf = correct/(double)dataSetSize; if (printInfo) { cout << "Training Set Performance: " << perf << " (" << correct << "/" << dataSetSize << ")" << endl; } return perf;}#include "SvmFuSvmDataPoint.h"#define IterateTypes(datatype, kerntype) \ template class SvmTest<DataPoint<datatype>, kerntype>;#include "SvmFuSvmTypes.h"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -