SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MultitaskL12LogisticRegression.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  init();
22 }
23 
25  float64_t rho1, float64_t rho2, CDotFeatures* train_features,
26  CBinaryLabels* train_labels, CTaskGroup* task_group) :
27  CMultitaskLogisticRegression(0.0,train_features,train_labels,(CTaskRelation*)task_group)
28 {
29  set_rho1(rho1);
30  set_rho2(rho2);
31  init();
32 }
33 
34 void CMultitaskL12LogisticRegression::init()
35 {
36  SG_ADD(&m_rho1,"rho1","rho L1/L2 regularization parameter",MS_AVAILABLE);
37  SG_ADD(&m_rho2,"rho2","rho L2 regularization parameter",MS_AVAILABLE);
38 }
39 
41 {
42  m_rho1 = rho1;
43 }
44 
46 {
47  m_rho2 = rho2;
48 }
49 
51 {
52  return m_rho1;
53 }
54 
56 {
57  return m_rho2;
58 }
59 
61 {
62 }
63 
65 {
67  for (int32_t i=0; i<y.vlen; i++)
68  y[i] = ((CBinaryLabels*)m_labels)->get_label(i);
69 
70  malsar_options options = malsar_options::default_options();
71  options.termination = m_termination;
72  options.tolerance = m_tolerance;
73  options.max_iter = m_max_iter;
74  options.n_tasks = ((CTaskGroup*)m_task_relation)->get_num_tasks();
75  options.tasks_indices = tasks;
76 #ifdef HAVE_EIGEN3
77  malsar_result_t model = malsar_joint_feature_learning(
78  features, y.vector, m_rho1, m_rho2, options);
79 
80  m_tasks_w = model.w;
81  m_tasks_c = model.c;
82 #else
83  SG_WARNING("Please install Eigen3 to use MultitaskL12LogisticRegression\n");
84  m_tasks_w = SGMatrix<float64_t>(((CDotFeatures*)features)->get_dim_feature_space(), options.n_tasks);
85  m_tasks_c = SGVector<float64_t>(options.n_tasks);
86 #endif
87 
88  return true;
89 }
90 
92 {
93  if (data && (CDotFeatures*)data)
94  set_features((CDotFeatures*)data);
95 
99 
101  for (int32_t i=0; i<y.vlen; i++)
102  y[i] = ((CBinaryLabels*)m_labels)->get_label(i);
103 
104  malsar_options options = malsar_options::default_options();
105  options.termination = m_termination;
106  options.tolerance = m_tolerance;
107  options.max_iter = m_max_iter;
108  options.n_tasks = ((CTaskGroup*)m_task_relation)->get_num_tasks();
109  options.tasks_indices = ((CTaskGroup*)m_task_relation)->get_tasks_indices();
110 
111 #ifdef HAVE_EIGEN3
112  malsar_result_t model = malsar_joint_feature_learning(
113  features, y.vector, m_rho1, m_rho2, options);
114 
115  m_tasks_w = model.w;
116  m_tasks_c = model.c;
117 #else
118  SG_WARNING("Please install Eigen3 to use MultitaskL12LogisticRegression\n");
119  m_tasks_w = SGMatrix<float64_t>(((CDotFeatures*)features)->get_dim_feature_space(), options.n_tasks);
120  m_tasks_c = SGVector<float64_t>(options.n_tasks);
121 #endif
122 
123  for (int32_t i=0; i<options.n_tasks; i++)
124  options.tasks_indices[i].~SGVector<index_t>();
125  SG_FREE(options.tasks_indices);
126 
127  return true;
128 }
129 
130 }

SHOGUN Machine Learning Toolbox - Documentation