SHOGUN  3.2.1
 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);
92  m_tasks_c = SGVector<float64_t>(options.n_tasks);
94 #endif
95 #else
96  SG_WARNING("Please install Eigen3 to use MultitaskClusteredLogisticRegression\n")
97  m_tasks_w = SGMatrix<float64_t>(((CDotFeatures*)features)->get_dim_feature_space(), options.n_tasks);
98  m_tasks_c = SGVector<float64_t>(options.n_tasks);
99 #endif
100  return true;
101 }
102 
104 {
105  if (data && (CDotFeatures*)data)
106  set_features((CDotFeatures*)data);
107 
111 
113  for (int32_t i=0; i<y.vlen; i++)
114  y[i] = ((CBinaryLabels*)m_labels)->get_label(i);
115 
116  malsar_options options = malsar_options::default_options();
117  options.termination = m_termination;
118  options.tolerance = m_tolerance;
119  options.max_iter = m_max_iter;
120  options.n_tasks = ((CTaskGroup*)m_task_relation)->get_num_tasks();
121  options.tasks_indices = ((CTaskGroup*)m_task_relation)->get_tasks_indices();
122  options.n_clusters = m_num_clusters;
123 
124 #ifdef HAVE_EIGEN3
125 #ifndef HAVE_CXX11
126  malsar_result_t model = malsar_clustered(
127  features, y.vector, m_rho1, m_rho2, options);
128 
129  m_tasks_w = model.w;
130  m_tasks_c = model.c;
131 #else
132  SG_WARNING("Clustered LR is unstable with C++11\n")
133  m_tasks_w = SGMatrix<float64_t>(((CDotFeatures*)features)->get_dim_feature_space(), options.n_tasks);
134  m_tasks_w.set_const(0);
135  m_tasks_c = SGVector<float64_t>(options.n_tasks);
136  m_tasks_c.set_const(0);
137 #endif
138 #else
139  SG_WARNING("Please install Eigen3 to use MultitaskClusteredLogisticRegression\n")
140  m_tasks_w = SGMatrix<float64_t>(((CDotFeatures*)features)->get_dim_feature_space(), options.n_tasks);
141  m_tasks_c = SGVector<float64_t>(options.n_tasks);
142 #endif
143 
144  SG_FREE(options.tasks_indices);
145 
146  return true;
147 }
148 
149 }
virtual int32_t get_num_labels() const =0
CLabels * m_labels
Definition: Machine.h:356
class TaskGroup used to represent a group of tasks. Tasks in group do not overlap.
Definition: TaskGroup.h:26
Features that support dot products among other operations.
Definition: DotFeatures.h:41
class Multitask Logistic Regression used to solve classification problems with a few tasks related vi...
#define ASSERT(x)
Definition: SGIO.h:203
double float64_t
Definition: common.h:48
virtual void set_features(CDotFeatures *feat)
void set_const(T const_elem)
Definition: SGVector.cpp:124
CDotFeatures * features
virtual bool train_locked_implementation(SGVector< index_t > *tasks)
malsar_result_t malsar_clustered(CDotFeatures *features, double *y, double rho1, double rho2, const malsar_options &options)
The class Features is the base class of all feature objects.
Definition: Features.h:62
Binary Labels for binary classification.
Definition: BinaryLabels.h:36
void set_const(T const_elem)
Definition: SGMatrix.cpp:89
used to represent tasks in multitask learning
Definition: TaskRelation.h:28
#define SG_WARNING(...)
Definition: SGIO.h:130

SHOGUN Machine Learning Toolbox - Documentation