SHOGUN  v3.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MultitaskClusteredLogisticRegression.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  * Copyright (C) 2012 Sergey Lisitsyn
8  */
9 
13 #include <shogun/lib/SGVector.h>
14 
15 namespace shogun
16 {
17 
19  CMultitaskLogisticRegression(), m_rho1(0.0), m_rho2(0.0)
20 {
21 }
22 
24  float64_t rho1, float64_t rho2, CDotFeatures* train_features,
25  CBinaryLabels* train_labels, CTaskGroup* task_group, int32_t n_clusters) :
26  CMultitaskLogisticRegression(0.0,train_features,train_labels,(CTaskRelation*)task_group)
27 {
28  set_rho1(rho1);
29  set_rho2(rho2);
30  set_num_clusters(n_clusters);
31 }
32 
34 {
35  return m_rho1;
36 }
37 
39 {
40  return m_rho2;
41 }
42 
44 {
45  m_rho1 = rho1;
46 }
47 
49 {
50  m_rho2 = rho2;
51 }
52 
54 {
55  return m_num_clusters;
56 }
57 
59 {
60  m_num_clusters = num_clusters;
61 }
62 
64 {
65 }
66 
68 {
70  for (int32_t i=0; i<y.vlen; i++)
71  y[i] = ((CBinaryLabels*)m_labels)->get_label(i);
72 
73  malsar_options options = malsar_options::default_options();
74  options.termination = m_termination;
75  options.tolerance = m_tolerance;
76  options.max_iter = m_max_iter;
77  options.n_tasks = ((CTaskGroup*)m_task_relation)->get_num_tasks();
78  options.tasks_indices = tasks;
79  options.n_clusters = m_num_clusters;
80 
81 #ifdef HAVE_EIGEN3
82 #ifndef HAVE_CXX11
83  malsar_result_t model = malsar_clustered(
84  features, y.vector, m_rho1, m_rho2, options);
85 
86  m_tasks_w = model.w;
87  m_tasks_c = model.c;
88 #else
89  SG_WARNING("Clustered LR is unstable with C++11\n")
90  m_tasks_w = SGMatrix<float64_t>(((CDotFeatures*)features)->get_dim_feature_space(), options.n_tasks);
91  m_tasks_c = SGVector<float64_t>(options.n_tasks);
92 #endif
93 #else
94  SG_WARNING("Please install Eigen3 to use MultitaskClusteredLogisticRegression\n")
95  m_tasks_w = SGMatrix<float64_t>(((CDotFeatures*)features)->get_dim_feature_space(), options.n_tasks);
96  m_tasks_c = SGVector<float64_t>(options.n_tasks);
97 #endif
98  return true;
99 }
100 
102 {
103  if (data && (CDotFeatures*)data)
104  set_features((CDotFeatures*)data);
105 
109 
111  for (int32_t i=0; i<y.vlen; i++)
112  y[i] = ((CBinaryLabels*)m_labels)->get_label(i);
113 
114  malsar_options options = malsar_options::default_options();
115  options.termination = m_termination;
116  options.tolerance = m_tolerance;
117  options.max_iter = m_max_iter;
118  options.n_tasks = ((CTaskGroup*)m_task_relation)->get_num_tasks();
119  options.tasks_indices = ((CTaskGroup*)m_task_relation)->get_tasks_indices();
120  options.n_clusters = m_num_clusters;
121 
122 #ifdef HAVE_EIGEN3
123 #ifndef HAVE_CXX11
124  malsar_result_t model = malsar_clustered(
125  features, y.vector, m_rho1, m_rho2, options);
126 
127  m_tasks_w = model.w;
128  m_tasks_c = model.c;
129 #else
130  SG_WARNING("Clustered LR is unstable with C++11\n")
131  m_tasks_w = SGMatrix<float64_t>(((CDotFeatures*)features)->get_dim_feature_space(), options.n_tasks);
132  m_tasks_c = SGVector<float64_t>(options.n_tasks);
133 #endif
134 #else
135  SG_WARNING("Please install Eigen3 to use MultitaskClusteredLogisticRegression\n")
136  m_tasks_w = SGMatrix<float64_t>(((CDotFeatures*)features)->get_dim_feature_space(), options.n_tasks);
137  m_tasks_c = SGVector<float64_t>(options.n_tasks);
138 #endif
139 
140  SG_FREE(options.tasks_indices);
141 
142  return true;
143 }
144 
145 }

SHOGUN Machine Learning Toolbox - Documentation