SVMLightOneClass.cpp

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 1999-2009 Soeren Sonnenburg
00008  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #include <shogun/lib/config.h>
00012 
00013 #ifdef USE_SVMLIGHT
00014 
00015 #include <shogun/io/SGIO.h>
00016 #include <shogun/mathematics/lapack.h>
00017 #include <shogun/lib/Signal.h>
00018 #include <shogun/mathematics/Math.h>
00019 #include <shogun/classifier/svm/SVMLightOneClass.h>
00020 #include <shogun/machine/KernelMachine.h>
00021 #include <shogun/kernel/CombinedKernel.h>
00022 
00023 #include <unistd.h>
00024 
00025 #ifdef USE_CPLEX
00026 extern "C" {
00027 #include <ilcplex/cplex.h>
00028 }
00029 #endif
00030 
00031 #include <shogun/base/Parallel.h>
00032 
00033 #ifdef HAVE_PTHREAD
00034 #include <pthread.h>
00035 #endif
00036 
00037 using namespace shogun;
00038 
00039 CSVMLightOneClass::CSVMLightOneClass(float64_t C, CKernel* k)
00040 : CSVMLight()
00041 {
00042     set_C(C,C);
00043     set_kernel(k);
00044 }
00045 
00046 CSVMLightOneClass::CSVMLightOneClass()
00047 : CSVMLight()
00048 {
00049 }
00050 
00051 bool CSVMLightOneClass::train_machine(CFeatures* data)
00052 {
00053     //certain setup params
00054     mkl_converged=false;
00055     verbosity=1 ;
00056     init_margin=0.15;
00057     init_iter=500;
00058     precision_violations=0;
00059     opt_precision=DEF_PRECISION;
00060 
00061     strcpy (learn_parm->predfile, "");
00062     learn_parm->biased_hyperplane=0;
00063     learn_parm->sharedslack=0;
00064     learn_parm->remove_inconsistent=0;
00065     learn_parm->skip_final_opt_check=0;
00066     learn_parm->svm_maxqpsize=get_qpsize();
00067     learn_parm->svm_newvarsinqp=learn_parm->svm_maxqpsize-1;
00068     learn_parm->maxiter=100000;
00069     learn_parm->svm_iter_to_shrink=100;
00070     learn_parm->svm_c=C1;
00071     learn_parm->transduction_posratio=0.33;
00072     learn_parm->svm_costratio=C2/C1;
00073     learn_parm->svm_costratio_unlab=1.0;
00074     learn_parm->svm_unlabbound=1E-5;
00075     learn_parm->epsilon_crit=epsilon; // GU: better decrease it ... ??
00076     learn_parm->epsilon_a=1E-15;
00077     learn_parm->compute_loo=0;
00078     learn_parm->rho=1.0;
00079     learn_parm->xa_depth=0;
00080 
00081     if (!kernel)
00082         SG_ERROR( "SVM_light can not proceed without kernel!\n");
00083 
00084     if (data)
00085         kernel->init(data, data);
00086 
00087     if (!kernel->has_features())
00088         SG_ERROR( "SVM_light can not proceed without initialized kernel!\n");
00089 
00090     int32_t num_vec=kernel->get_num_vec_lhs();
00091     SG_INFO("num_vec=%d\n", num_vec);
00092 
00093     SG_UNREF(labels);
00094     labels=new CLabels(num_vec);
00095     labels->set_to_one();
00096 
00097     // in case of LINADD enabled kernels cleanup!
00098     if (kernel->has_property(KP_LINADD) && get_linadd_enabled())
00099         kernel->clear_normal() ;
00100 
00101     // output some info
00102     SG_DEBUG( "threads = %i\n", parallel->get_num_threads()) ;
00103     SG_DEBUG( "qpsize = %i\n", learn_parm->svm_maxqpsize) ;
00104     SG_DEBUG( "epsilon = %1.1e\n", learn_parm->epsilon_crit) ;
00105     SG_DEBUG( "kernel->has_property(KP_LINADD) = %i\n", kernel->has_property(KP_LINADD)) ;
00106     SG_DEBUG( "kernel->has_property(KP_KERNCOMBINATION) = %i\n", kernel->has_property(KP_KERNCOMBINATION)) ;
00107     SG_DEBUG( "kernel->has_property(KP_BATCHEVALUATION) = %i\n", kernel->has_property(KP_BATCHEVALUATION)) ;
00108     SG_DEBUG( "kernel->get_optimization_type() = %s\n", kernel->get_optimization_type()==FASTBUTMEMHUNGRY ? "FASTBUTMEMHUNGRY" : "SLOWBUTMEMEFFICIENT" ) ;
00109     SG_DEBUG( "get_solver_type() = %i\n", get_solver_type());
00110     SG_DEBUG( "get_linadd_enabled() = %i\n", get_linadd_enabled()) ;
00111     SG_DEBUG( "get_batch_computation_enabled() = %i\n", get_batch_computation_enabled()) ;
00112     SG_DEBUG( "kernel->get_num_subkernels() = %i\n", kernel->get_num_subkernels()) ;
00113 
00114     use_kernel_cache = !((kernel->get_kernel_type() == K_CUSTOM) ||
00115                          (get_linadd_enabled() && kernel->has_property(KP_LINADD)));
00116 
00117     SG_DEBUG( "use_kernel_cache = %i\n", use_kernel_cache) ;
00118 
00119     if (kernel->get_kernel_type() == K_COMBINED)
00120     {
00121         CKernel* kn = ((CCombinedKernel*)kernel)->get_first_kernel();
00122 
00123         while (kn)
00124         {
00125             // allocate kernel cache but clean up beforehand
00126             kn->resize_kernel_cache(kn->get_cache_size());
00127             SG_UNREF(kn);
00128             kn = ((CCombinedKernel*) kernel)->get_next_kernel();
00129         }
00130     }
00131 
00132     kernel->resize_kernel_cache(kernel->get_cache_size());
00133 
00134     // train the svm
00135     svm_learn();
00136 
00137     // brain damaged svm light work around
00138     create_new_model(model->sv_num-1);
00139     set_bias(-model->b);
00140     for (int32_t i=0; i<model->sv_num-1; i++)
00141     {
00142         set_alpha(i, model->alpha[i+1]);
00143         set_support_vector(i, model->supvec[i+1]);
00144     }
00145 
00146     // in case of LINADD enabled kernels cleanup!
00147     if (kernel->has_property(KP_LINADD) && get_linadd_enabled())
00148     {
00149         kernel->clear_normal() ;
00150         kernel->delete_optimization() ;
00151     }
00152 
00153     if (use_kernel_cache)
00154         kernel->kernel_cache_cleanup();
00155 
00156     return true ;
00157 }
00158 #endif //USE_SVMLIGHT
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation