SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MulticlassTreeGuidedLogisticRegression.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
17 
18 using namespace shogun;
19 
22 {
23  init_defaults();
24 }
25 
28 {
29  init_defaults();
30  set_z(z);
31  set_index_tree(tree);
32 }
33 
34 void CMulticlassTreeGuidedLogisticRegression::init_defaults()
35 {
36  m_index_tree = NULL;
37  set_z(0.1);
38  set_epsilon(1e-2);
39  set_max_iter(10000);
40 }
41 
42 void CMulticlassTreeGuidedLogisticRegression::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 
50 {
52 }
53 
55 {
56  if (data)
57  set_features((CDotFeatures*)data);
58 
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  if (m_index_tree->is_general())
85  {
87  options.G = G.vector;
88  }
90  options.ind_t = ind_t.vector;
91  options.n_nodes = ind_t.size()/3;
92  options.tolerance = m_epsilon;
93  options.max_iter = m_max_iter;
94  slep_result_t result = slep_mc_tree_lr(m_features,(CMulticlassLabels*)m_labels,m_z,options);
95 
96  SGMatrix<float64_t> all_w = result.w;
97  SGVector<float64_t> all_c = result.c;
98  for (int32_t i=0; i<n_classes; i++)
99  {
100  SGVector<float64_t> w(n_feats);
101  for (int32_t j=0; j<n_feats; j++)
102  w[j] = all_w(j,i);
103  float64_t c = all_c[i];
104  CLinearMachine* machine = new CLinearMachine();
105  machine->set_w(w);
106  machine->set_bias(c);
107  m_machines->push_back(machine);
108  }
109  return true;
110 }
111 #endif /* HAVE_EIGEN3 */

SHOGUN Machine Learning Toolbox - Documentation