SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MulticlassLibLinear.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) 2012 Sergey Lisitsyn
8  * Copyright (C) 2012 Sergey Lisitsyn
9  */
10 
11 #include <shogun/lib/config.h>
15 #include <shogun/lib/v_array.h>
16 #include <shogun/lib/Signal.h>
18 
19 using namespace shogun;
20 
23 {
24  init_defaults();
25 }
26 
29 {
30  init_defaults();
31  set_C(C);
32 }
33 
34 void CMulticlassLibLinear::init_defaults()
35 {
36  set_C(1.0);
37  set_epsilon(1e-2);
38  set_max_iter(10000);
39  set_use_bias(false);
40  set_save_train_state(false);
41  m_train_state = NULL;
42 }
43 
44 void CMulticlassLibLinear::register_parameters()
45 {
46  SG_ADD(&m_C, "m_C", "regularization constant",MS_AVAILABLE);
47  SG_ADD(&m_epsilon, "m_epsilon", "tolerance epsilon",MS_NOT_AVAILABLE);
48  SG_ADD(&m_max_iter, "m_max_iter", "max number of iterations",MS_NOT_AVAILABLE);
49  SG_ADD(&m_use_bias, "m_use_bias", "indicates whether bias should be used",MS_NOT_AVAILABLE);
50  SG_ADD(&m_save_train_state, "m_save_train_state", "indicates whether bias should be used",MS_NOT_AVAILABLE);
51 }
52 
54 {
56 }
57 
59 {
60  if (!m_train_state)
61  SG_ERROR("Please enable save_train_state option and train machine.\n")
62 
64 
65  int32_t num_vectors = m_features->get_num_vectors();
66  int32_t num_classes = ((CMulticlassLabels*) m_labels)->get_num_classes();
67 
68  v_array<int32_t> nz_idxs;
69  nz_idxs.reserve(num_vectors);
70 
71  for (int32_t i=0; i<num_vectors; i++)
72  {
73  for (int32_t y=0; y<num_classes; y++)
74  {
75  if (CMath::abs(m_train_state->alpha[i*num_classes+y])>1e-6)
76  {
77  nz_idxs.push(i);
78  break;
79  }
80  }
81  }
82  int32_t num_nz = nz_idxs.index();
83  nz_idxs.reserve(num_nz);
84  return SGVector<int32_t>(nz_idxs.begin,num_nz);
85 }
86 
88 {
89  return SGMatrix<float64_t>();
90 }
91 
93 {
94  if (data)
95  set_features((CDotFeatures*)data);
96 
100 
101  int32_t num_vectors = m_features->get_num_vectors();
102  int32_t num_classes = ((CMulticlassLabels*) m_labels)->get_num_classes();
103  int32_t bias_n = m_use_bias ? 1 : 0;
104 
105  liblinear_problem mc_problem;
106  mc_problem.l = num_vectors;
107  mc_problem.n = m_features->get_dim_feature_space() + bias_n;
108  mc_problem.y = SG_MALLOC(float64_t, mc_problem.l);
109  for (int32_t i=0; i<num_vectors; i++)
110  mc_problem.y[i] = ((CMulticlassLabels*) m_labels)->get_int_label(i);
111 
112  mc_problem.x = m_features;
113  mc_problem.use_bias = m_use_bias;
114 
116 
117  if (!m_train_state)
118  m_train_state = new mcsvm_state();
119 
120  float64_t* C = SG_MALLOC(float64_t, num_vectors);
121  for (int32_t i=0; i<num_vectors; i++)
122  C[i] = m_C;
123 
125 
126  Solver_MCSVM_CS solver(&mc_problem,num_classes,C,w0.matrix,m_epsilon,
128  solver.solve();
129 
131  for (int32_t i=0; i<num_classes; i++)
132  {
133  CLinearMachine* machine = new CLinearMachine();
134  SGVector<float64_t> cw(mc_problem.n-bias_n);
135 
136  for (int32_t j=0; j<mc_problem.n-bias_n; j++)
137  cw[j] = m_train_state->w[j*num_classes+i];
138 
139  machine->set_w(cw);
140 
141  if (m_use_bias)
142  machine->set_bias(m_train_state->w[(mc_problem.n-bias_n)*num_classes+i]);
143 
144  m_machines->push_back(machine);
145  }
146 
147  if (!m_save_train_state)
149 
150  SG_FREE(C);
151  SG_FREE(mc_problem.y);
152 
153  return true;
154 }
void reserve(size_t length)
Definition: v_array.h:199
virtual ELabelType get_label_type() const =0
virtual void set_w(const SGVector< float64_t > src_w)
virtual SGMatrix< float64_t > obtain_regularizer_matrix() const
T * begin
Pointer to first element of the array.
Definition: v_array.h:157
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
multi-class labels 0,1,...
Definition: LabelTypes.h:20
void set_max_iter(int32_t max_iter)
Class v_array taken directly from JL's implementation.
virtual int32_t get_num_vectors() const =0
float64_t m_max_train_time
Definition: Machine.h:358
CLabels * m_labels
Definition: Machine.h:361
#define SG_ERROR(...)
Definition: SGIO.h:129
void set_save_train_state(bool save_train_state)
Features that support dot products among other operations.
Definition: DotFeatures.h:44
void push(const T &new_elem)
Definition: v_array.h:168
virtual int32_t get_dim_feature_space() const =0
Multiclass Labels for multi-class classification.
generic linear multiclass machine
#define ASSERT(x)
Definition: SGIO.h:201
CMulticlassStrategy * m_multiclass_strategy
virtual bool train_machine(CFeatures *data=NULL)
static void clear_cancel()
Definition: Signal.cpp:129
double float64_t
Definition: common.h:50
Class LinearMachine is a generic interface for all kinds of linear machines like classifiers.
Definition: LinearMachine.h:63
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
void set_epsilon(float64_t epsilon)
The class Features is the base class of all feature objects.
Definition: Features.h:68
virtual void set_bias(float64_t b)
multiclass one vs rest strategy used to train generic multiclass machines for K-class problems with b...
#define SG_ADD(...)
Definition: SGObject.h:84
void set_use_bias(bool use_bias)
SGVector< int32_t > get_support_vectors() const
static T abs(T a)
Definition: Math.h:179

SHOGUN Machine Learning Toolbox - Documentation