SHOGUN  3.2.1
 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>
15 
16 namespace shogun
17 {
18 
20  CMultitaskLogisticRegression(), m_rho1(0.0), m_rho2(0.0)
21 {
22  init();
23 }
24 
26  float64_t rho1, float64_t rho2, CDotFeatures* train_features,
27  CBinaryLabels* train_labels, CTaskGroup* task_group) :
28  CMultitaskLogisticRegression(0.0,train_features,train_labels,(CTaskRelation*)task_group)
29 {
30  set_rho1(rho1);
31  set_rho2(rho2);
32  init();
33 }
34 
35 void CMultitaskL12LogisticRegression::init()
36 {
37  SG_ADD(&m_rho1,"rho1","rho L1/L2 regularization parameter",MS_AVAILABLE);
38  SG_ADD(&m_rho2,"rho2","rho L2 regularization parameter",MS_AVAILABLE);
39 }
40 
42 {
43  m_rho1 = rho1;
44 }
45 
47 {
48  m_rho2 = rho2;
49 }
50 
52 {
53  return m_rho1;
54 }
55 
57 {
58  return m_rho2;
59 }
60 
62 {
63 }
64 
66 {
68  for (int32_t i=0; i<y.vlen; i++)
69  y[i] = ((CBinaryLabels*)m_labels)->get_label(i);
70 
71  malsar_options options = malsar_options::default_options();
72  options.termination = m_termination;
73  options.tolerance = m_tolerance;
74  options.max_iter = m_max_iter;
75  options.n_tasks = ((CTaskGroup*)m_task_relation)->get_num_tasks();
76  options.tasks_indices = tasks;
77 #ifdef HAVE_EIGEN3
78  malsar_result_t model = malsar_joint_feature_learning(
79  features, y.vector, m_rho1, m_rho2, options);
80 
81  m_tasks_w = model.w;
82  m_tasks_c = model.c;
83 #else
84  SG_WARNING("Please install Eigen3 to use MultitaskL12LogisticRegression\n")
85  m_tasks_w = SGMatrix<float64_t>(((CDotFeatures*)features)->get_dim_feature_space(), options.n_tasks);
86  m_tasks_c = SGVector<float64_t>(options.n_tasks);
87 #endif
88 
89  return true;
90 }
91 
93 {
94  if (data && (CDotFeatures*)data)
95  set_features((CDotFeatures*)data);
96 
100 
102  for (int32_t i=0; i<y.vlen; i++)
103  y[i] = ((CBinaryLabels*)m_labels)->get_label(i);
104 
105  malsar_options options = malsar_options::default_options();
106  options.termination = m_termination;
107  options.tolerance = m_tolerance;
108  options.max_iter = m_max_iter;
109  options.n_tasks = ((CTaskGroup*)m_task_relation)->get_num_tasks();
110  options.tasks_indices = ((CTaskGroup*)m_task_relation)->get_tasks_indices();
111 
112 #ifdef HAVE_EIGEN3
113  malsar_result_t model = malsar_joint_feature_learning(
114  features, y.vector, m_rho1, m_rho2, options);
115 
116  m_tasks_w = model.w;
117  m_tasks_c = model.c;
118 #else
119  SG_WARNING("Please install Eigen3 to use MultitaskL12LogisticRegression\n")
120  m_tasks_w = SGMatrix<float64_t>(((CDotFeatures*)features)->get_dim_feature_space(), options.n_tasks);
121  m_tasks_c = SGVector<float64_t>(options.n_tasks);
122 #endif
123 
124  SG_FREE(options.tasks_indices);
125 
126  return true;
127 }
128 
129 }

SHOGUN Machine Learning Toolbox - Documentation