SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SVMLightOneClass.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 1999-2009 Soeren Sonnenburg
8  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
11 #include <shogun/lib/config.h>
12 
13 #ifdef USE_SVMLIGHT
14 
15 #include <shogun/io/SGIO.h>
17 #include <shogun/lib/Signal.h>
23 
24 #include <unistd.h>
25 
26 #ifdef USE_CPLEX
27 extern "C" {
28 #include <ilcplex/cplex.h>
29 }
30 #endif
31 
32 #include <shogun/base/Parallel.h>
33 
34 #ifdef HAVE_PTHREAD
35 #include <pthread.h>
36 #endif
37 
38 using namespace shogun;
39 
41 : CSVMLight()
42 {
43  set_C(C,C);
44  set_kernel(k);
45 }
46 
48 : CSVMLight()
49 {
50 }
51 
53 {
54  //certain setup params
55  mkl_converged=false;
56  verbosity=1 ;
57  init_margin=0.15;
58  init_iter=500;
61 
62  strcpy (learn_parm->predfile, "");
63  learn_parm->biased_hyperplane=0;
64  learn_parm->sharedslack=0;
65  learn_parm->remove_inconsistent=0;
66  learn_parm->skip_final_opt_check=0;
67  learn_parm->svm_maxqpsize=get_qpsize();
68  learn_parm->svm_newvarsinqp=learn_parm->svm_maxqpsize-1;
69  learn_parm->maxiter=100000;
70  learn_parm->svm_iter_to_shrink=100;
71  learn_parm->svm_c=C1;
72  learn_parm->transduction_posratio=0.33;
73  learn_parm->svm_costratio=C2/C1;
74  learn_parm->svm_costratio_unlab=1.0;
75  learn_parm->svm_unlabbound=1E-5;
76  learn_parm->epsilon_crit=epsilon; // GU: better decrease it ... ??
77  learn_parm->epsilon_a=1E-15;
78  learn_parm->compute_loo=0;
79  learn_parm->rho=1.0;
80  learn_parm->xa_depth=0;
81 
82  if (!kernel)
83  SG_ERROR("SVM_light can not proceed without kernel!\n")
84 
85  if (data)
86  kernel->init(data, data);
87 
88  if (!kernel->has_features())
89  SG_ERROR("SVM_light can not proceed without initialized kernel!\n")
90 
91  int32_t num_vec=kernel->get_num_vec_lhs();
92  SG_INFO("num_vec=%d\n", num_vec)
93 
95  m_labels=new CBinaryLabels(num_vec);
96  ((CBinaryLabels*) m_labels)->set_to_one();
97 
98  // in case of LINADD enabled kernels cleanup!
100  kernel->clear_normal() ;
101 
102  // output some info
103  SG_DEBUG("threads = %i\n", parallel->get_num_threads())
104  SG_DEBUG("qpsize = %i\n", learn_parm->svm_maxqpsize)
105  SG_DEBUG("epsilon = %1.1e\n", learn_parm->epsilon_crit)
106  SG_DEBUG("kernel->has_property(KP_LINADD) = %i\n", kernel->has_property(KP_LINADD))
107  SG_DEBUG("kernel->has_property(KP_KERNCOMBINATION) = %i\n", kernel->has_property(KP_KERNCOMBINATION))
108  SG_DEBUG("kernel->has_property(KP_BATCHEVALUATION) = %i\n", kernel->has_property(KP_BATCHEVALUATION))
109  SG_DEBUG("kernel->get_optimization_type() = %s\n", kernel->get_optimization_type()==FASTBUTMEMHUNGRY ? "FASTBUTMEMHUNGRY" : "SLOWBUTMEMEFFICIENT" )
110  SG_DEBUG("get_solver_type() = %i\n", get_solver_type())
111  SG_DEBUG("get_linadd_enabled() = %i\n", get_linadd_enabled())
112  SG_DEBUG("get_batch_computation_enabled() = %i\n", get_batch_computation_enabled())
113  SG_DEBUG("kernel->get_num_subkernels() = %i\n", kernel->get_num_subkernels())
114 
117 
118  SG_DEBUG("use_kernel_cache = %i\n", use_kernel_cache)
119 
121  {
122  for (index_t k_idx=0; k_idx<((CCombinedKernel*) kernel)->get_num_kernels(); k_idx++)
123  {
124  CKernel* kn = ((CCombinedKernel*) kernel)->get_kernel(k_idx);
125  // allocate kernel cache but clean up beforehand
127  SG_UNREF(kn);
128  }
129  }
130 
132 
133  // train the svm
134  svm_learn();
135 
136  // brain damaged svm light work around
137  create_new_model(model->sv_num-1);
138  set_bias(-model->b);
139  for (int32_t i=0; i<model->sv_num-1; i++)
140  {
141  set_alpha(i, model->alpha[i+1]);
142  set_support_vector(i, model->supvec[i+1]);
143  }
144 
145  // in case of LINADD enabled kernels cleanup!
147  {
148  kernel->clear_normal() ;
150  }
151 
152  if (use_kernel_cache)
154 
155  return true ;
156 }
157 #endif //USE_SVMLIGHT

SHOGUN Machine Learning Toolbox - Documentation