SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MulticlassLogisticRegression.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 
13 #ifdef USE_GPL_SHOGUN
15 #include <shogun/io/SGIO.h>
19 
20 using namespace shogun;
21 
22 CMulticlassLogisticRegression::CMulticlassLogisticRegression() :
24 {
25  init_defaults();
26 }
27 
28 CMulticlassLogisticRegression::CMulticlassLogisticRegression(float64_t z, CDotFeatures* feats, CLabels* labs) :
30 {
31  init_defaults();
32  set_z(z);
33 }
34 
35 void CMulticlassLogisticRegression::init_defaults()
36 {
37  set_z(0.1);
38  set_epsilon(1e-2);
39  set_max_iter(10000);
40 }
41 
42 void CMulticlassLogisticRegression::register_parameters()
43 {
44  SG_ADD(&m_z, "m_z", "regularization constant",MS_AVAILABLE);
45  SG_ADD(&m_epsilon, "m_epsilon", "tolerance epsilon",MS_NOT_AVAILABLE);
46  SG_ADD(&m_max_iter, "m_max_iter", "max number of iterations",MS_NOT_AVAILABLE);
47 }
48 
49 CMulticlassLogisticRegression::~CMulticlassLogisticRegression()
50 {
51 }
52 
53 bool CMulticlassLogisticRegression::train_machine(CFeatures* data)
54 {
55  if (data)
56  set_features((CDotFeatures*)data);
57 
58  REQUIRE(m_features, "%s::train_machine(): No features attached!\n");
59  REQUIRE(m_labels, "%s::train_machine(): No labels attached!\n");
60  REQUIRE(m_labels->get_label_type()==LT_MULTICLASS, "%s::train_machine(): "
61  "Attached labels are no multiclass labels\n");
62  REQUIRE(m_multiclass_strategy, "%s::train_machine(): No multiclass strategy"
63  " attached!\n");
64 
65  int32_t n_classes = ((CMulticlassLabels*)m_labels)->get_num_classes();
66  int32_t n_feats = m_features->get_dim_feature_space();
67 
68  slep_options options = slep_options::default_options();
69  if (m_machines->get_num_elements()!=0)
70  {
71  SGMatrix<float64_t> all_w_old(n_feats, n_classes);
72  SGVector<float64_t> all_c_old(n_classes);
73  for (int32_t i=0; i<n_classes; i++)
74  {
75  CLinearMachine* machine = (CLinearMachine*)m_machines->get_element(i);
76  SGVector<float64_t> w = machine->get_w();
77  for (int32_t j=0; j<n_feats; j++)
78  all_w_old(j,i) = w[j];
79  all_c_old[i] = machine->get_bias();
80  SG_UNREF(machine);
81  }
82  options.last_result = new slep_result_t(all_w_old,all_c_old);
83  m_machines->reset_array();
84  }
85  options.tolerance = m_epsilon;
86  options.max_iter = m_max_iter;
87  slep_result_t result = slep_mc_plain_lr(m_features,(CMulticlassLabels*)m_labels,m_z,options);
88 
89  SGMatrix<float64_t> all_w = result.w;
90  SGVector<float64_t> all_c = result.c;
91  for (int32_t i=0; i<n_classes; i++)
92  {
93  SGVector<float64_t> w(n_feats);
94  for (int32_t j=0; j<n_feats; j++)
95  w[j] = all_w(j,i);
96  float64_t c = all_c[i];
97  CLinearMachine* machine = new CLinearMachine();
98  machine->set_w(w);
99  machine->set_bias(c);
100  m_machines->push_back(machine);
101  }
102  return true;
103 }
104 #endif //USE_GPL_SHOGUN
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
multi-class labels 0,1,...
Definition: LabelTypes.h:20
#define REQUIRE(x,...)
Definition: SGIO.h:206
Features that support dot products among other operations.
Definition: DotFeatures.h:44
Multiclass Labels for multi-class classification.
generic linear multiclass machine
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
virtual SGVector< float64_t > get_w() const
#define SG_UNREF(x)
Definition: SGObject.h:52
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
The class Features is the base class of all feature objects.
Definition: Features.h:68
virtual float64_t get_bias()
virtual void set_bias(float64_t b)
void set_epsilon(float *begin, float max)
Definition: JLCoverTree.h:513
multiclass one vs rest strategy used to train generic multiclass machines for K-class problems with b...
#define SG_ADD(...)
Definition: SGObject.h:81

SHOGUN Machine Learning Toolbox - Documentation