SHOGUN  v3.2.0
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  SG_FREE(options.tasks_indices);
124 
125  return true;
126 }
127 
128 }
virtual int32_t get_num_labels() const =0
CLabels * m_labels
Definition: Machine.h:356
virtual bool train_locked_implementation(SGVector< index_t > *tasks)
class TaskGroup used to represent a group of tasks. Tasks in group do not overlap.
Definition: TaskGroup.h:26
Features that support dot products among other operations.
Definition: DotFeatures.h:41
class Multitask Logistic Regression used to solve classification problems with a few tasks related vi...
#define ASSERT(x)
Definition: SGIO.h:203
double float64_t
Definition: common.h:48
virtual void set_features(CDotFeatures *feat)
CDotFeatures * features
malsar_result_t malsar_joint_feature_learning(CDotFeatures *features, double *y, double rho1, double rho2, const malsar_options &options)
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:16
The class Features is the base class of all feature objects.
Definition: Features.h:62
Binary Labels for binary classification.
Definition: BinaryLabels.h:36
used to represent tasks in multitask learning
Definition: TaskRelation.h:28
#define SG_WARNING(...)
Definition: SGIO.h:130
#define SG_ADD(...)
Definition: SGObject.h:71

SHOGUN Machine Learning Toolbox - Documentation