SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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>
15 #include <shogun/lib/SGMatrix.h>
16 
17 namespace shogun
18 {
19 
21  CMultitaskLogisticRegression(), m_rho1(0.0), m_rho2(0.0)
22 {
23 }
24 
26  float64_t rho1, float64_t rho2, CDotFeatures* train_features,
27  CBinaryLabels* train_labels, CTaskGroup* task_group, int32_t n_clusters) :
28  CMultitaskLogisticRegression(0.0,train_features,train_labels,(CTaskRelation*)task_group)
29 {
30  set_rho1(rho1);
31  set_rho2(rho2);
32  set_num_clusters(n_clusters);
33 }
34 
36 {
37  return m_rho1;
38 }
39 
41 {
42  return m_rho2;
43 }
44 
46 {
47  m_rho1 = rho1;
48 }
49 
51 {
52  m_rho2 = rho2;
53 }
54 
56 {
57  return m_num_clusters;
58 }
59 
61 {
62  m_num_clusters = num_clusters;
63 }
64 
66 {
67 }
68 
70 {
72  for (int32_t i=0; i<y.vlen; i++)
73  y[i] = ((CBinaryLabels*)m_labels)->get_label(i);
74 
75  malsar_options options = malsar_options::default_options();
76  options.termination = m_termination;
77  options.tolerance = m_tolerance;
78  options.max_iter = m_max_iter;
79  options.n_tasks = ((CTaskGroup*)m_task_relation)->get_num_tasks();
80  options.tasks_indices = tasks;
81  options.n_clusters = m_num_clusters;
82 
83 #ifdef HAVE_EIGEN3
84 #ifndef HAVE_CXX11
85  malsar_result_t model = malsar_clustered(
86  features, y.vector, m_rho1, m_rho2, options);
87 
88  m_tasks_w = model.w;
89  m_tasks_c = model.c;
90 #else
91  SG_WARNING("Clustered LR is unstable with C++11\n")
92  m_tasks_w = SGMatrix<float64_t>(((CDotFeatures*)features)->get_dim_feature_space(), options.n_tasks);
94  m_tasks_c = SGVector<float64_t>(options.n_tasks);
96 #endif
97 #else
98  SG_WARNING("Please install Eigen3 to use MultitaskClusteredLogisticRegression\n")
99  m_tasks_w = SGMatrix<float64_t>(((CDotFeatures*)features)->get_dim_feature_space(), options.n_tasks);
100  m_tasks_c = SGVector<float64_t>(options.n_tasks);
101 #endif
102  return true;
103 }
104 
106 {
107  if (data && (CDotFeatures*)data)
108  set_features((CDotFeatures*)data);
109 
113 
115  for (int32_t i=0; i<y.vlen; i++)
116  y[i] = ((CBinaryLabels*)m_labels)->get_label(i);
117 
118  malsar_options options = malsar_options::default_options();
119  options.termination = m_termination;
120  options.tolerance = m_tolerance;
121  options.max_iter = m_max_iter;
122  options.n_tasks = ((CTaskGroup*)m_task_relation)->get_num_tasks();
123  options.tasks_indices = ((CTaskGroup*)m_task_relation)->get_tasks_indices();
124  options.n_clusters = m_num_clusters;
125 
126 #ifdef HAVE_EIGEN3
127 #ifndef HAVE_CXX11
128  malsar_result_t model = malsar_clustered(
129  features, y.vector, m_rho1, m_rho2, options);
130 
131  m_tasks_w = model.w;
132  m_tasks_c = model.c;
133 #else
134  SG_WARNING("Clustered LR is unstable with C++11\n")
135  m_tasks_w = SGMatrix<float64_t>(((CDotFeatures*)features)->get_dim_feature_space(), options.n_tasks);
136  m_tasks_w.set_const(0);
137  m_tasks_c = SGVector<float64_t>(options.n_tasks);
138  m_tasks_c.set_const(0);
139 #endif
140 #else
141  SG_WARNING("Please install Eigen3 to use MultitaskClusteredLogisticRegression\n")
142  m_tasks_w = SGMatrix<float64_t>(((CDotFeatures*)features)->get_dim_feature_space(), options.n_tasks);
143  m_tasks_c = SGVector<float64_t>(options.n_tasks);
144 #endif
145 
146  SG_FREE(options.tasks_indices);
147 
148  return true;
149 }
150 
151 }
virtual int32_t get_num_labels() const =0
CLabels * m_labels
Definition: Machine.h:361
class TaskGroup used to represent a group of tasks. Tasks in group do not overlap.
Definition: TaskGroup.h:28
Features that support dot products among other operations.
Definition: DotFeatures.h:44
class Multitask Logistic Regression used to solve classification problems with a few tasks related vi...
#define ASSERT(x)
Definition: SGIO.h:201
double float64_t
Definition: common.h:50
virtual void set_features(CDotFeatures *feat)
CDotFeatures * features
virtual bool train_locked_implementation(SGVector< index_t > *tasks)
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
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:68
Binary Labels for binary classification.
Definition: BinaryLabels.h:37
used to represent tasks in multitask learning
Definition: TaskRelation.h:31
#define SG_WARNING(...)
Definition: SGIO.h:128
void set_const(T const_elem)
Definition: SGMatrix.cpp:133
void set_const(T const_elem)
Definition: SGVector.cpp:152

SHOGUN Machine Learning Toolbox - Documentation