Changeset 73


Ignore:
Timestamp:
May 3, 2004, 12:13:24 PM (19 years ago)
Author:
Peter
Message:

changes to match svm class

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/test_svm.cc

    r57 r73  
    66#include "vector.h"
    77#include "SVM.h"
     8#include "Kernel.h"
     9#include "PolynomialKernelFunction.h"
    810
    911// Standard includes
     
    1820
    1921
    20   cout << "reading data/nm_kernel.txt" << endl;   
    21   //ifstream is("k.txt");
    22   ifstream is("data/nm_kernel.txt");
    23   theplu::gslapi::matrix kernel(is);
    24   is.close();
     22  cout << "reading data/nm_kernel.txt" << endl;
     23  ifstream is("data/nm_kernel.txt");
     24  theplu::gslapi::matrix kernel(is);
     25  is.close();
    2526
    26   theplu::gslapi::matrix k(kernel);
     27  theplu::gslapi::matrix k(kernel);
    2728 
    28   cout << "reading data/nm_target_bin.txt" << endl;   
    29   is.open("data/nm_target_bin.txt");
     29  cout << "reading data/nm_target_bin.txt" << endl;
     30  is.open("data/nm_target_bin.txt");
     31  theplu::gslapi::vector target(is);
     32  is.close();
     33
     34  cout << "reading data/nm_alpha_linear_matlab.txt" << endl;
     35  is.open("data/nm_alpha_linear_matlab.txt");
     36  theplu::gslapi::vector alpha_matlab(is);
     37  is.close();
     38
     39
     40  cout << "create svm" << endl;
     41  theplu::cpptools::SVM svm(kernel, target);
     42  cout << "train" << endl;
     43  svm.train();
     44  cout << "get alpha" << endl;
     45  theplu::gslapi::vector alpha = svm.get_alpha();
     46     
     47  // Comparing alpha to alpha_matlab
     48  theplu::gslapi::vector diff_alpha = alpha - alpha_matlab;
     49  cout << diff_alpha*diff_alpha << endl;
    3050 
    31   theplu::gslapi::vector target(is);
    32   is.close();
     51  // Comparing output to target
     52  theplu::gslapi::vector output = svm.get_output();
     53  double slack = 0;
     54  for (unsigned int i=0; i<target.size(); i++){
     55    if (output[i]*target[i] < 1){
     56      slack += 1 - output[i]*target[i];
     57    }
     58  }
     59  cout << "slack:" << slack << endl;
    3360
    34   cout << "data/nm_alpha_linear_matlab.txt" << endl;   
    35   is.open("data/nm_alpha_linear_matlab.txt");
    36   theplu::gslapi::vector alpha_matlab(is);
    37   is.close();
    38 
    39   //training the SVM
    40   cout << "Training the SVM" << endl;   
    41   theplu::cpptools::SVM svm(kernel, target);
    42   cout << "Training the SVM" << endl;   
    43   svm.train();
    44  
    45   //getting alpha
    46   cout << "getting alpha" << endl;   
    47   theplu::gslapi::vector alpha;
    48   alpha =  svm.get_alpha();
    49      
    50   // Comparing alpha to alpha_matlab
    51   theplu::gslapi::vector diff_alpha = alpha - alpha_matlab;
    52   cout << "Checking difference" << endl;   
    53   cout << diff_alpha*diff_alpha << endl;
    54  
    55   // Comparing output to target
    56   theplu::gslapi::vector output = svm.get_output();
    57   double slack = 0;
    58   for (unsigned int i=0; i<target.size(); i++){
    59    if (output[i]*target[i] < 1){
    60     slack += 1 - output[i]*target[i];
    61    }
    62   }
    63   cout << "slack:" << slack << endl;
    64 
    65   //training the SVM
    66   cout << "Training the SVM" << endl;   
    67   svm.train(1);
    68  
    69  
    7061  return 0;
    7162}
Note: See TracChangeset for help on using the changeset viewer.