SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 */

SHOGUN Machine Learning Toolbox - Documentation