SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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
virtual void clear_normal()
Definition: Kernel.cpp:847
virtual bool init(CFeatures *lhs, CFeatures *rhs)
Definition: Kernel.cpp:98
#define DEF_PRECISION
Definition: SVMLight.h:38
#define SG_INFO(...)
Definition: SGIO.h:118
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:64
int32_t index_t
Definition: common.h:62
int32_t verbosity
Definition: SVMLight.h:657
bool use_kernel_cache
Definition: SVMLight.h:685
CLabels * m_labels
Definition: Machine.h:361
#define SG_ERROR(...)
Definition: SGIO.h:129
virtual bool delete_optimization()
Definition: Kernel.cpp:823
float64_t epsilon
Definition: SVM.h:251
Parallel * parallel
Definition: SGObject.h:499
virtual int32_t get_num_vec_lhs()
Definition: Kernel.h:516
bool has_property(EKernelProperty p)
Definition: Kernel.h:723
float64_t init_margin
Definition: SVMLight.h:660
void set_bias(float64_t bias)
float64_t C2
Definition: SVM.h:259
double float64_t
Definition: common.h:50
bool set_alpha(int32_t idx, float64_t val)
EOptimizationType get_optimization_type()
Definition: Kernel.h:741
int32_t precision_violations
Definition: SVMLight.h:664
ESolverType get_solver_type()
Definition: Machine.cpp:110
The Combined kernel is used to combine a number of kernels into a single CombinedKernel object by lin...
float64_t C1
Definition: SVM.h:257
bool set_support_vector(int32_t idx, int32_t val)
#define SG_UNREF(x)
Definition: SGObject.h:52
#define SG_DEBUG(...)
Definition: SGIO.h:107
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:556
class SVMlight
Definition: SVMLight.h:225
virtual int32_t get_num_subkernels()
Definition: Kernel.cpp:852
The Kernel base class.
Definition: Kernel.h:158
Binary Labels for binary classification.
Definition: BinaryLabels.h:37
int32_t get_cache_size()
Definition: Kernel.h:598
void set_kernel(CKernel *k)
virtual bool train_machine(CFeatures *data=NULL)
int32_t get_qpsize()
Definition: SVM.h:173
virtual bool has_features()
Definition: Kernel.h:534
void resize_kernel_cache(KERNELCACHE_IDX size, bool regression_hack=false)
Definition: Kernel.cpp:85
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