SHOGUN  5.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
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 
11 #ifdef USE_GPL_SHOGUN
12 
15 #include <shogun/lib/SGVector.h>
17 #include <shogun/lib/SGMatrix.h>
18 
19 namespace shogun
20 {
21 
22 CMultitaskClusteredLogisticRegression::CMultitaskClusteredLogisticRegression() :
23  CMultitaskLogisticRegression(), m_rho1(0.0), m_rho2(0.0)
24 {
25 }
26 
27 CMultitaskClusteredLogisticRegression::CMultitaskClusteredLogisticRegression(
28  float64_t rho1, float64_t rho2, CDotFeatures* train_features,
29  CBinaryLabels* train_labels, CTaskGroup* task_group, int32_t n_clusters) :
30  CMultitaskLogisticRegression(0.0,train_features,train_labels,(CTaskRelation*)task_group)
31 {
32  set_rho1(rho1);
33  set_rho2(rho2);
34  set_num_clusters(n_clusters);
35 }
36 
37 int32_t CMultitaskClusteredLogisticRegression::get_rho1() const
38 {
39  return m_rho1;
40 }
41 
42 int32_t CMultitaskClusteredLogisticRegression::get_rho2() const
43 {
44  return m_rho2;
45 }
46 
47 void CMultitaskClusteredLogisticRegression::set_rho1(float64_t rho1)
48 {
49  m_rho1 = rho1;
50 }
51 
52 void CMultitaskClusteredLogisticRegression::set_rho2(float64_t rho2)
53 {
54  m_rho2 = rho2;
55 }
56 
57 int32_t CMultitaskClusteredLogisticRegression::get_num_clusters() const
58 {
59  return m_num_clusters;
60 }
61 
62 void CMultitaskClusteredLogisticRegression::set_num_clusters(int32_t num_clusters)
63 {
64  m_num_clusters = num_clusters;
65 }
66 
67 CMultitaskClusteredLogisticRegression::~CMultitaskClusteredLogisticRegression()
68 {
69 }
70 
71 bool CMultitaskClusteredLogisticRegression::train_locked_implementation(SGVector<index_t>* tasks)
72 {
73  SGVector<float64_t> y(m_labels->get_num_labels());
74  for (int32_t i=0; i<y.vlen; i++)
75  y[i] = ((CBinaryLabels*)m_labels)->get_label(i);
76 
77  malsar_options options = malsar_options::default_options();
78  options.termination = m_termination;
79  options.tolerance = m_tolerance;
80  options.max_iter = m_max_iter;
81  options.n_tasks = ((CTaskGroup*)m_task_relation)->get_num_tasks();
82  options.tasks_indices = tasks;
83  options.n_clusters = m_num_clusters;
84 
85 #ifndef HAVE_CXX11
86  malsar_result_t model = malsar_clustered(
87  features, y.vector, m_rho1, m_rho2, options);
88 
89  m_tasks_w = model.w;
90  m_tasks_c = model.c;
91 #else
92  SG_WARNING("Clustered LR is unstable with C++11\n")
93  m_tasks_w = SGMatrix<float64_t>(((CDotFeatures*)features)->get_dim_feature_space(), options.n_tasks);
94  m_tasks_w.set_const(0);
95  m_tasks_c = SGVector<float64_t>(options.n_tasks);
96  m_tasks_c.set_const(0);
97 #endif
98  return true;
99 }
100 
101 bool CMultitaskClusteredLogisticRegression::train_machine(CFeatures* data)
102 {
103  if (data && (CDotFeatures*)data)
104  set_features((CDotFeatures*)data);
105 
106  ASSERT(features)
107  ASSERT(m_labels)
108  ASSERT(m_task_relation)
109 
110  SGVector<float64_t> y(m_labels->get_num_labels());
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 #ifndef HAVE_CXX11
123  malsar_result_t model = malsar_clustered(
124  features, y.vector, m_rho1, m_rho2, options);
125 
126  m_tasks_w = model.w;
127  m_tasks_c = model.c;
128 #else
129  SG_WARNING("Clustered LR is unstable with C++11\n")
130  m_tasks_w = SGMatrix<float64_t>(((CDotFeatures*)features)->get_dim_feature_space(), options.n_tasks);
131  m_tasks_w.set_const(0);
132  m_tasks_c = SGVector<float64_t>(options.n_tasks);
133  m_tasks_c.set_const(0);
134 #endif
135 
136  SG_FREE(options.tasks_indices);
137 
138  return true;
139 }
140 
141 }
142 
143 #endif //USE_GPL_SHOGUN
#define ASSERT(x)
Definition: SGIO.h:201
double float64_t
Definition: common.h:50
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
#define SG_WARNING(...)
Definition: SGIO.h:128

SHOGUN Machine Learning Toolbox - Documentation