SHOGUN  v2.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  malsar_result_t model = malsar_clustered(
83  features, y.vector, m_rho1, m_rho2, options);
84 
85  m_tasks_w = model.w;
86  m_tasks_c = model.c;
87 #else
88  SG_WARNING("Please install Eigen3 to use MultitaskClusteredLogisticRegression\n");
89  m_tasks_w = SGMatrix<float64_t>(((CDotFeatures*)features)->get_dim_feature_space(), options.n_tasks);
90  m_tasks_c = SGVector<float64_t>(options.n_tasks);
91 #endif
92  return true;
93 }
94 
96 {
97  if (data && (CDotFeatures*)data)
98  set_features((CDotFeatures*)data);
99 
100  ASSERT(features);
101  ASSERT(m_labels);
103 
105  for (int32_t i=0; i<y.vlen; i++)
106  y[i] = ((CBinaryLabels*)m_labels)->get_label(i);
107 
108  malsar_options options = malsar_options::default_options();
109  options.termination = m_termination;
110  options.tolerance = m_tolerance;
111  options.max_iter = m_max_iter;
112  options.n_tasks = ((CTaskGroup*)m_task_relation)->get_num_tasks();
113  options.tasks_indices = ((CTaskGroup*)m_task_relation)->get_tasks_indices();
114  options.n_clusters = m_num_clusters;
115 
116 #ifdef HAVE_EIGEN3
117  malsar_result_t model = malsar_clustered(
118  features, y.vector, m_rho1, m_rho2, options);
119 
120  m_tasks_w = model.w;
121  m_tasks_c = model.c;
122 #else
123  SG_WARNING("Please install Eigen3 to use MultitaskClusteredLogisticRegression\n");
124  m_tasks_w = SGMatrix<float64_t>(((CDotFeatures*)features)->get_dim_feature_space(), options.n_tasks);
125  m_tasks_c = SGVector<float64_t>(options.n_tasks);
126 #endif
127 
128  for (int32_t i=0; i<options.n_tasks; i++)
129  options.tasks_indices[i].~SGVector<index_t>();
130  SG_FREE(options.tasks_indices);
131 
132  return true;
133 }
134 
135 }

SHOGUN Machine Learning Toolbox - Documentation