Changeset 463
- Timestamp:
- Dec 16, 2005, 6:59:15 PM (17 years ago)
- Location:
- trunk
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/classifier/KernelView.cc
r461 r463 8 8 namespace classifier { 9 9 10 KernelView::KernelView(const Kernel& kernel) 11 : MatrixView(), kernel_(&kernel) 12 { 13 for(size_t i=0;i<(*kernel_).size();i++) 14 column_index_.push_back(i); 15 row_index_=column_index_; 16 } 17 18 KernelView::KernelView(const Kernel& kernel, 19 const std::vector<size_t>& index) 20 : MatrixView(index,index), kernel_(&kernel) 21 { 22 } 23 10 24 KernelView::KernelView(const Kernel& kernel, 11 25 const std::vector<size_t>& row, -
trunk/lib/classifier/KernelView.h
r461 r463 21 21 public: 22 22 23 /// 24 /// Constructor 25 /// 26 KernelView(const Kernel&); 27 28 /// 29 /// Contructor taking the Kernel to view into, index 30 /// vector. Equivalent to KernelView(kernel, index, index). 31 /// 32 /// @note For training usage row index shall always be equal to 33 /// column index. 34 /// 35 KernelView(const Kernel& kernel, const std::vector<size_t>& index); 36 23 37 /// 24 38 /// Contructor taking the Kernel to view into, row index vector, 25 /// and column vector.39 /// and column index vector. 26 40 /// 27 41 /// @note For training usage row index shall always be equal to -
trunk/lib/classifier/SVM.cc
r461 r463 10 10 #include <c++_tools/random/random.h> 11 11 12 #include <iostream>13 12 #include <algorithm> 13 #include <cassert> 14 14 #include <cmath> 15 15 #include <limits> -
trunk/test/crossvalidation_test.cc
r453 r463 1 1 // $Id$ 2 2 3 #include <c++_tools/classifier/Cross Validation.h>3 #include <c++_tools/classifier/CrossSplitting.h> 4 4 #include <c++_tools/gslapi/vector.h> 5 5 6 #include <cstdlib> 7 #include <fstream> 8 #include <iostream> 6 9 #include <vector> 7 #include <cstdlib>8 #include <iostream>9 10 10 using namespace theplu; 11 12 int main() 11 int main(const int argc,const char* argv[]) 13 12 { 14 13 14 using namespace theplu; 15 16 std::ostream* error; 17 if (argc>1 && argv[1]==std::string("-v")) 18 error = &std::cerr; 19 else { 20 error = new std::ofstream("/dev/null"); 21 if (argc>1) 22 std::cout << "crossvalidation_test -v : for printing extra information\n"; 23 } 24 *error << "testing crosssplitting" << std::endl; 25 bool ok = true; 15 26 gslapi::vector target(10,1); 16 27 for (size_t i=0; i<5; i++) 17 28 target(i)=-1; 18 29 19 classifier::Cross Validationcv(target,3);30 classifier::CrossSplitting cv(target,3); 20 31 21 32 … … 36 47 37 48 for (unsigned int i=0; i<10 ; i++) 38 if (count[i]!=2) 39 return -1; 49 ok = ok && (count[i]==2); 40 50 51 if (!ok) 52 *error << "crossvalidation failed" << std::endl; 53 54 if (error!=&std::cerr) 55 delete error; 41 56 return 0; 42 57 } -
trunk/test/kernel_test.cc
r453 r463 22 22 23 23 bool test_MEV(const gslapi::matrix& data, const classifier::KernelFunction* kf, 24 const gslapi::matrix& control, const double error_bound) 24 const gslapi::matrix& control, const double error_bound, 25 std::ostream* error) 25 26 { 26 27 classifier::Kernel_MEV kernel(data,*kf); … … 35 36 index[1]=2; 36 37 index[2]=3; 37 classifier::KernelView(kernel,index); 38 classifier::KernelView kv(kernel,index); 39 if (kv.rows()!=index.size()){ 40 *error << "Error: KernelView(kernel, index)\n" << std::endl 41 << "Size of KernelView is " << kv.rows() << std::endl 42 << "expected " << index.size() << std::endl; 43 44 return false; 45 } 46 classifier::KernelView kv2(kernel); 47 if (kv2.rows()!=kernel.size()){ 48 *error << "Error: KernelView(kernel)\n" << std::endl 49 << "Size of KernelView is " << kv.rows() << std::endl 50 << "expected " << kernel.size() << std::endl; 51 52 return false; 53 } 38 54 39 55 return true; … … 41 57 42 58 bool test_SEV(const gslapi::matrix& data, const classifier::KernelFunction* kf, 43 const gslapi::matrix& control, const double error_bound) 59 const gslapi::matrix& control, const double error_bound, 60 std::ostream* error) 44 61 { 45 62 classifier::Kernel_SEV kernel(data,*kf); … … 54 71 index[1]=2; 55 72 index[2]=3; 56 classifier::KernelView(kernel,index); 73 classifier::KernelView kv(kernel,index); 74 if (kv.rows()!=index.size()){ 75 *error << "Error: KernelView(kernel, index)\n" << std::endl 76 << "Size of KernelView is " << kv.rows() << std::endl 77 << "expected " << index.size() << std::endl; 78 79 return false; 80 } 81 classifier::KernelView kv2(kernel); 82 if (kv2.rows()!=kernel.size()){ 83 *error << "Error: KernelView(kernel)\n" << std::endl 84 << "Size of KernelView is " << kv.rows() << std::endl 85 << "expected " << kernel.size() << std::endl; 86 87 return false; 88 } 57 89 return true; 58 90 } … … 85 117 is.close(); 86 118 classifier::KernelFunction* kf = new classifier::PolynomialKernelFunction(); 87 ok = (ok && test_MEV(data,kf,kernel_matlab,error_bound )88 & test_SEV(data,kf,kernel_matlab,error_bound ));119 ok = (ok && test_MEV(data,kf,kernel_matlab,error_bound, error) 120 & test_SEV(data,kf,kernel_matlab,error_bound, error)); 89 121 delete kf; 90 122 … … 93 125 is.close(); 94 126 kf = new classifier::PolynomialKernelFunction(2); 95 ok = (ok && test_MEV(data,kf,kernel_matlab2,error_bound )96 & test_SEV(data,kf,kernel_matlab2,error_bound ));127 ok = (ok && test_MEV(data,kf,kernel_matlab2,error_bound, error) 128 & test_SEV(data,kf,kernel_matlab2,error_bound, error)); 97 129 delete kf; 98 130 -
trunk/test/score_test.cc
r447 r463 7 7 #include <c++_tools/statistics/FoldChange.h> 8 8 #include <c++_tools/gslapi/vector.h> 9 #include <c++_tools/statistics/WilcoxonFoldChange.h> 9 10 10 11 #include <gsl/gsl_cdf.h> … … 108 109 statistics::Pearson pearson(true); 109 110 111 *error << "testing WilcoxonFoldChange" << std::endl; 112 statistics::WilcoxonFoldChange wfc(true); 113 110 114 111 115 if (ok) -
trunk/test/svm_test.cc
r453 r463 20 20 { 21 21 22 bool print = (argc>1 && argv[1]==std::string("-p")); 22 std::ostream* error; 23 if (argc>1 && argv[1]==std::string("-v")) 24 error = &std::cerr; 25 else { 26 error = new std::ofstream("/dev/null"); 27 if (argc>1) 28 std::cout << "svm_test -v : for printing extra information\n"; 29 } 30 *error << "testing svm" << std::endl; 23 31 bool ok = true; 24 32 … … 37 45 classifier::Kernel_MEV kernel2(data2,*kf2); 38 46 assert(kernel2.size()==3); 39 classifier::SVM classifier2(kernel2, target2); 47 assert(target2.size()==3); 48 classifier::KernelView kv2(kernel2); 49 *error << "testing with linear kernel" << std::endl; 50 assert(kv2.rows()==target2.size()); 51 classifier::SVM classifier2(kv2, target2); 52 *error << "training..."; 40 53 classifier2.train(); 54 *error << " done." << std::endl; 41 55 42 56 if (classifier2.alpha()*target2){ 43 std::cerr << "condition not fullfilled" << std::endl;57 *error << "condition not fullfilled" << std::endl; 44 58 return -1; 45 59 } 46 60 47 61 if (classifier2.alpha()(1)!=2 || classifier2.alpha()(2)!=2){ 48 std::cerr << "wrong alpha" << std::endl;49 std::cerr << "alpha: " << classifier2.alpha() << std::endl;50 std::cerr << "expected: 4 2 2" << std::endl;62 *error << "wrong alpha" << std::endl; 63 *error << "alpha: " << classifier2.alpha() << std::endl; 64 *error << "expected: 4 2 2" << std::endl; 51 65 52 66 return -1; … … 73 87 is.close(); 74 88 75 theplu::classifier::SVM classifier(kernel, target); 76 if (!classifier.train()){ 89 classifier::KernelView kv(kernel); 90 theplu::classifier::SVM svm(kv, target); 91 if (!svm.train()){ 77 92 ok=false; 78 if (print) 79 std::cerr << "Training failured" << std::endl; 93 *error << "Training failured" << std::endl; 80 94 } 81 95 82 theplu::gslapi::vector alpha = classifier.alpha();96 theplu::gslapi::vector alpha = svm.alpha(); 83 97 84 98 // Comparing alpha to alpha_matlab … … 86 100 diff_alpha-=alpha_matlab; 87 101 if (diff_alpha*diff_alpha> 1e-10 ){ 88 if (print) 89 std::cerr << "Difference to matlab alphas too large\n"; 102 *error << "Difference to matlab alphas too large\n"; 90 103 ok=false; 91 104 } 92 105 93 106 // Comparing output to target 94 theplu::gslapi::vector output( classifier.output());107 theplu::gslapi::vector output(svm.output()); 95 108 double slack = 0; 96 109 for (unsigned int i=0; i<target.size(); i++){ … … 101 114 double slack_bound=2e-7; 102 115 if (slack > slack_bound){ 103 if (print){ 104 std::cerr << "Slack too large. Is the bias correct?\n"; 105 std::cerr << "slack: " << slack << std::endl; 106 std::cerr << "expected less than " << slack_bound << std::endl; 107 } 116 *error << "Slack too large. Is the bias correct?\n"; 117 *error << "slack: " << slack << std::endl; 118 *error << "expected less than " << slack_bound << std::endl; 108 119 ok = false; 109 120 } … … 112 123 delete kf2; 113 124 125 if (error!=&std::cerr) 126 delete error; 127 114 128 if(ok) 115 129 return 0;
Note: See TracChangeset
for help on using the changeset viewer.