SHOGUN  4.1.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 
12 #ifdef HAVE_EIGEN3
14 #include <shogun/io/SGIO.h>
18 
19 using namespace shogun;
20 
23 {
24  init_defaults();
25 }
26 
29 {
30  init_defaults();
31  set_z(z);
32 }
33 
34 void CMulticlassLogisticRegression::init_defaults()
35 {
36  set_z(0.1);
37  set_epsilon(1e-2);
38  set_max_iter(10000);
39 }
40 
41 void CMulticlassLogisticRegression::register_parameters()
42 {
43  SG_ADD(&m_z, "m_z", "regularization constant",MS_AVAILABLE);
44  SG_ADD(&m_epsilon, "m_epsilon", "tolerance epsilon",MS_NOT_AVAILABLE);
45  SG_ADD(&m_max_iter, "m_max_iter", "max number of iterations",MS_NOT_AVAILABLE);
46 }
47 
49 {
50 }
51 
53 {
54  if (data)
55  set_features((CDotFeatures*)data);
56 
57  REQUIRE(m_features, "%s::train_machine(): No features attached!\n");
58  REQUIRE(m_labels, "%s::train_machine(): No labels attached!\n");
59  REQUIRE(m_labels->get_label_type()==LT_MULTICLASS, "%s::train_machine(): "
60  "Attached labels are no multiclass labels\n");
61  REQUIRE(m_multiclass_strategy, "%s::train_machine(): No multiclass strategy"
62  " attached!\n");
63 
64  int32_t n_classes = ((CMulticlassLabels*)m_labels)->get_num_classes();
65  int32_t n_feats = m_features->get_dim_feature_space();
66 
67  slep_options options = slep_options::default_options();
68  if (m_machines->get_num_elements()!=0)
69  {
70  SGMatrix<float64_t> all_w_old(n_feats, n_classes);
71  SGVector<float64_t> all_c_old(n_classes);
72  for (int32_t i=0; i<n_classes; i++)
73  {
75  SGVector<float64_t> w = machine->get_w();
76  for (int32_t j=0; j<n_feats; j++)
77  all_w_old(j,i) = w[j];
78  all_c_old[i] = machine->get_bias();
79  SG_UNREF(machine);
80  }
81  options.last_result = new slep_result_t(all_w_old,all_c_old);
83  }
84  options.tolerance = m_epsilon;
85  options.max_iter = m_max_iter;
86  slep_result_t result = slep_mc_plain_lr(m_features,(CMulticlassLabels*)m_labels,m_z,options);
87 
88  SGMatrix<float64_t> all_w = result.w;
89  SGVector<float64_t> all_c = result.c;
90  for (int32_t i=0; i<n_classes; i++)
91  {
92  SGVector<float64_t> w(n_feats);
93  for (int32_t j=0; j<n_feats; j++)
94  w[j] = all_w(j,i);
95  float64_t c = all_c[i];
96  CLinearMachine* machine = new CLinearMachine();
97  machine->set_w(w);
98  machine->set_bias(c);
99  m_machines->push_back(machine);
100  }
101  return true;
102 }
103 #endif /* HAVE_EIGEN3 */
virtual ELabelType get_label_type() const =0
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
CLabels * m_labels
Definition: Machine.h:361
#define REQUIRE(x,...)
Definition: SGIO.h:206
slep_result_t slep_mc_plain_lr(CDotFeatures *features, CMulticlassLabels *labels, float64_t z, const slep_options &options)
Features that support dot products among other operations.
Definition: DotFeatures.h:44
virtual bool train_machine(CFeatures *data=NULL)
virtual int32_t get_dim_feature_space() const =0
Multiclass Labels for multi-class classification.
generic linear multiclass machine
CMulticlassStrategy * m_multiclass_strategy
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()
CSGObject * get_element(int32_t index) const
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:81

SHOGUN Machine Learning Toolbox - Documentation