SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SVMLin.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) 2006-2009 Soeren Sonnenburg
8  * Copyright (C) 2006-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
12 #include <shogun/labels/Labels.h>
14 #include <shogun/lib/external/ssl.h>
17 #include <shogun/labels/Labels.h>
19 
20 using namespace shogun;
21 
23 : CLinearMachine(), C1(1), C2(1), epsilon(1e-5), use_bias(true)
24 {
25 }
26 
28  float64_t C, CDotFeatures* traindat, CLabels* trainlab)
29 : CLinearMachine(), C1(C), C2(C), epsilon(1e-5), use_bias(true)
30 {
31  set_features(traindat);
32  set_labels(trainlab);
33 }
34 
35 
37 {
38 }
39 
41 {
43 
44  if (data)
45  {
46  if (!data->has_property(FP_DOT))
47  SG_ERROR("Specified features are not of type CDotFeatures\n")
48  set_features((CDotFeatures*) data);
49  }
50 
52 
53  SGVector<float64_t> train_labels=((CBinaryLabels*) m_labels)->get_labels();
54  int32_t num_feat=features->get_dim_feature_space();
55  int32_t num_vec=features->get_num_vectors();
56 
57  ASSERT(num_vec==train_labels.vlen)
58 
59  struct options Options;
60  struct data Data;
61  struct vector_double Weights;
62  struct vector_double Outputs;
63 
64  Data.l=num_vec;
65  Data.m=num_vec;
66  Data.u=0;
67  Data.n=num_feat+1;
68  Data.nz=num_feat+1;
69  Data.Y=train_labels.vector;
70  Data.features=features;
71  Data.C = SG_MALLOC(float64_t, Data.l);
72 
73  Options.algo = SVM;
74  Options.lambda=1/(2*get_C1());
75  Options.lambda_u=1/(2*get_C1());
76  Options.S=10000;
77  Options.R=0.5;
78  Options.epsilon = get_epsilon();
79  Options.cgitermax=10000;
80  Options.mfnitermax=50;
81  Options.Cp = get_C2()/get_C1();
82  Options.Cn = 1;
83 
84  if (use_bias)
85  Options.bias=1.0;
86  else
87  Options.bias=0.0;
88 
89  for (int32_t i=0;i<num_vec;i++)
90  {
91  if(train_labels.vector[i]>0)
92  Data.C[i]=Options.Cp;
93  else
94  Data.C[i]=Options.Cn;
95  }
96  ssl_train(&Data, &Options, &Weights, &Outputs);
97  ASSERT(Weights.vec && Weights.d==num_feat+1)
98 
99  float64_t sgn=train_labels.vector[0];
100  for (int32_t i=0; i<num_feat+1; i++)
101  Weights.vec[i]*=sgn;
102 
103  set_w(SGVector<float64_t>(Weights.vec, num_feat));
104  set_bias(Weights.vec[num_feat]);
105 
106  SG_FREE(Data.C);
107  SG_FREE(Outputs.vec);
108  return true;
109 }
virtual void set_w(const SGVector< float64_t > src_w)
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
bool use_bias
Definition: SVMLin.h:116
virtual int32_t get_num_vectors() const =0
CLabels * m_labels
Definition: Machine.h:361
#define SG_ERROR(...)
Definition: SGIO.h:129
float64_t get_C2()
Definition: SVMLin.h:67
Features that support dot products among other operations.
Definition: DotFeatures.h:44
virtual int32_t get_dim_feature_space() const =0
static const float64_t epsilon
Definition: libbmrm.cpp:25
index_t vlen
Definition: SGVector.h:494
float64_t get_epsilon()
Definition: SVMLin.h:91
#define ASSERT(x)
Definition: SGIO.h:201
virtual ~CSVMLin()
Definition: SVMLin.cpp:36
float64_t get_C1()
Definition: SVMLin.h:61
double float64_t
Definition: common.h:50
virtual void set_features(CDotFeatures *feat)
Class LinearMachine is a generic interface for all kinds of linear machines like classifiers.
Definition: LinearMachine.h:63
CDotFeatures * features
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
virtual bool train_machine(CFeatures *data=NULL)
Definition: SVMLin.cpp:40
The class Features is the base class of all feature objects.
Definition: Features.h:68
Binary Labels for binary classification.
Definition: BinaryLabels.h:37
virtual void set_bias(float64_t b)
bool has_property(EFeatureProperty p) const
Definition: Features.cpp:295
virtual void set_labels(CLabels *lab)
Definition: Machine.cpp:65

SHOGUN Machine Learning Toolbox - Documentation