SHOGUN  6.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
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 #ifndef _WIN32
25 #include <unistd.h>
26 #endif
27 
28 #ifdef USE_CPLEX
29 extern "C" {
30 #include <ilcplex/cplex.h>
31 }
32 #endif
33 
34 #include <shogun/base/Parallel.h>
35 
36 using namespace shogun;
37 
39 : CSVMLight()
40 {
41  set_C(C,C);
42  set_kernel(k);
43 }
44 
46 : CSVMLight()
47 {
48 }
49 
51 {
52  //certain setup params
53  mkl_converged=false;
54  verbosity=1 ;
55  init_margin=0.15;
56  init_iter=500;
59 
60  strcpy (learn_parm->predfile, "");
61  learn_parm->biased_hyperplane=0;
62  learn_parm->sharedslack=0;
63  learn_parm->remove_inconsistent=0;
64  learn_parm->skip_final_opt_check=0;
65  learn_parm->svm_maxqpsize=get_qpsize();
66  learn_parm->svm_newvarsinqp=learn_parm->svm_maxqpsize-1;
67  learn_parm->maxiter=100000;
68  learn_parm->svm_iter_to_shrink=100;
69  learn_parm->svm_c=C1;
70  learn_parm->transduction_posratio=0.33;
71  learn_parm->svm_costratio=C2/C1;
72  learn_parm->svm_costratio_unlab=1.0;
73  learn_parm->svm_unlabbound=1E-5;
74  learn_parm->epsilon_crit=epsilon; // GU: better decrease it ... ??
75  learn_parm->epsilon_a=1E-15;
76  learn_parm->compute_loo=0;
77  learn_parm->rho=1.0;
78  learn_parm->xa_depth=0;
79 
80  if (!kernel)
81  SG_ERROR("SVM_light can not proceed without kernel!\n")
82 
83  if (data)
84  kernel->init(data, data);
85 
86  if (!kernel->has_features())
87  SG_ERROR("SVM_light can not proceed without initialized kernel!\n")
88 
89  int32_t num_vec=kernel->get_num_vec_lhs();
90  SG_INFO("num_vec=%d\n", num_vec)
91 
93  m_labels=new CBinaryLabels(num_vec);
94  ((CBinaryLabels*) m_labels)->set_to_one();
95 
96  // in case of LINADD enabled kernels cleanup!
98  kernel->clear_normal() ;
99 
100  // output some info
101  SG_DEBUG("threads = %i\n", parallel->get_num_threads())
102  SG_DEBUG("qpsize = %i\n", learn_parm->svm_maxqpsize)
103  SG_DEBUG("epsilon = %1.1e\n", learn_parm->epsilon_crit)
104  SG_DEBUG("kernel->has_property(KP_LINADD) = %i\n", kernel->has_property(KP_LINADD))
105  SG_DEBUG("kernel->has_property(KP_KERNCOMBINATION) = %i\n", kernel->has_property(KP_KERNCOMBINATION))
106  SG_DEBUG("kernel->has_property(KP_BATCHEVALUATION) = %i\n", kernel->has_property(KP_BATCHEVALUATION))
107  SG_DEBUG("kernel->get_optimization_type() = %s\n", kernel->get_optimization_type()==FASTBUTMEMHUNGRY ? "FASTBUTMEMHUNGRY" : "SLOWBUTMEMEFFICIENT" )
108  SG_DEBUG("get_solver_type() = %i\n", get_solver_type())
109  SG_DEBUG("get_linadd_enabled() = %i\n", get_linadd_enabled())
110  SG_DEBUG("get_batch_computation_enabled() = %i\n", get_batch_computation_enabled())
111  SG_DEBUG("kernel->get_num_subkernels() = %i\n", kernel->get_num_subkernels())
112 
115 
116  SG_DEBUG("use_kernel_cache = %i\n", use_kernel_cache)
117 
119  {
120  for (index_t k_idx=0; k_idx<((CCombinedKernel*) kernel)->get_num_kernels(); k_idx++)
121  {
122  CKernel* kn = ((CCombinedKernel*) kernel)->get_kernel(k_idx);
123  // allocate kernel cache but clean up beforehand
125  SG_UNREF(kn);
126  }
127  }
128 
130 
131  // train the svm
132  svm_learn();
133 
134  // brain damaged svm light work around
135  create_new_model(model->sv_num-1);
136  set_bias(-model->b);
137  for (int32_t i=0; i<model->sv_num-1; i++)
138  {
139  set_alpha(i, model->alpha[i+1]);
140  set_support_vector(i, model->supvec[i+1]);
141  }
142 
143  // in case of LINADD enabled kernels cleanup!
145  {
146  kernel->clear_normal() ;
148  }
149 
150  if (use_kernel_cache)
152 
153  return true ;
154 }
155 #endif //USE_SVMLIGHT
virtual void clear_normal()
Definition: Kernel.cpp:834
virtual bool init(CFeatures *lhs, CFeatures *rhs)
Definition: Kernel.cpp:96
#define DEF_PRECISION
Definition: SVMLight.h:38
#define SG_INFO(...)
Definition: SGIO.h:117
float64_t opt_precision
Definition: SVMLight.h:668
int32_t init_iter
Definition: SVMLight.h:662
int32_t get_num_threads() const
Definition: Parallel.cpp:97
int32_t index_t
Definition: common.h:72
int32_t verbosity
Definition: SVMLight.h:657
bool use_kernel_cache
Definition: SVMLight.h:685
CLabels * m_labels
Definition: Machine.h:365
#define SG_ERROR(...)
Definition: SGIO.h:128
virtual bool delete_optimization()
Definition: Kernel.cpp:810
float64_t epsilon
Definition: SVM.h:266
Parallel * parallel
Definition: SGObject.h:561
virtual int32_t get_num_vec_lhs()
bool has_property(EKernelProperty p)
float64_t init_margin
Definition: SVMLight.h:660
void set_bias(float64_t bias)
float64_t C2
Definition: SVM.h:274
double float64_t
Definition: common.h:60
bool set_alpha(int32_t idx, float64_t val)
EOptimizationType get_optimization_type()
int32_t precision_violations
Definition: SVMLight.h:664
ESolverType get_solver_type()
Definition: Machine.cpp:102
The Combined kernel is used to combine a number of kernels into a single CombinedKernel object by lin...
float64_t C1
Definition: SVM.h:272
bool set_support_vector(int32_t idx, int32_t val)
#define SG_UNREF(x)
Definition: SGObject.h:53
#define SG_DEBUG(...)
Definition: SGIO.h:106
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
virtual EKernelType get_kernel_type()=0
The class Features is the base class of all feature objects.
Definition: Features.h:68
LEARN_PARM * learn_parm
Definition: SVMLight.h:655
void kernel_cache_cleanup()
Definition: Kernel.cpp:542
class SVMlight
Definition: SVMLight.h:225
virtual int32_t get_num_subkernels()
Definition: Kernel.cpp:839
The Kernel base class.
Binary Labels for binary classification.
Definition: BinaryLabels.h:37
int32_t get_cache_size()
void set_kernel(CKernel *k)
virtual bool train_machine(CFeatures *data=NULL)
int32_t get_qpsize()
Definition: SVM.h:173
virtual bool has_features()
void resize_kernel_cache(KERNELCACHE_IDX size, bool regression_hack=false)
Definition: Kernel.cpp:83
void set_C(float64_t c_neg, float64_t c_pos)
Definition: SVM.h:118
bool create_new_model(int32_t num)

SHOGUN Machine Learning Toolbox - Documentation