SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 
10 
12 #ifdef USE_GPL_SHOGUN
15 #include <shogun/lib/SGVector.h>
17 
18 namespace shogun
19 {
20 
21 class CMultitaskL12LogisticRegression::Self
22 {
23 public:
24 
26  float64_t m_rho1;
27 
29  float64_t m_rho2;
30 };
31 
32 CMultitaskL12LogisticRegression::CMultitaskL12LogisticRegression() :
33  CMultitaskLogisticRegression(), self()
34 {
35  set_rho1(0.0);
36  set_rho2(0.0);
37  init();
38 }
39 
40 CMultitaskL12LogisticRegression::CMultitaskL12LogisticRegression(
41  float64_t rho1, float64_t rho2, CDotFeatures* train_features,
42  CBinaryLabels* train_labels, CTaskGroup* task_group) :
43  CMultitaskLogisticRegression(0.0,train_features,train_labels,(CTaskRelation*)task_group)
44 {
45  set_rho1(rho1);
46  set_rho2(rho2);
47  init();
48 }
49 
50 void CMultitaskL12LogisticRegression::init()
51 {
52  SG_ADD(&self->m_rho1,"rho1","rho L1/L2 regularization parameter",MS_AVAILABLE);
53  SG_ADD(&self->m_rho2,"rho2","rho L2 regularization parameter",MS_AVAILABLE);
54 }
55 
56 void CMultitaskL12LogisticRegression::set_rho1(float64_t rho1)
57 {
58  self->m_rho1 = rho1;
59 }
60 
61 void CMultitaskL12LogisticRegression::set_rho2(float64_t rho2)
62 {
63  self->m_rho2 = rho2;
64 }
65 
66 float64_t CMultitaskL12LogisticRegression::get_rho1() const
67 {
68  return self->m_rho1;
69 }
70 
71 float64_t CMultitaskL12LogisticRegression::get_rho2() const
72 {
73  return self->m_rho2;
74 }
75 
76 CMultitaskL12LogisticRegression::~CMultitaskL12LogisticRegression()
77 {
78 }
79 
80 bool CMultitaskL12LogisticRegression::train_locked_implementation(SGVector<index_t>* tasks)
81 {
82  SGVector<float64_t> y(m_labels->get_num_labels());
83  for (int32_t i=0; i<y.vlen; i++)
84  y[i] = ((CBinaryLabels*)m_labels)->get_label(i);
85 
86  malsar_options options = malsar_options::default_options();
87  options.termination = m_termination;
88  options.tolerance = m_tolerance;
89  options.max_iter = m_max_iter;
90  options.n_tasks = ((CTaskGroup*)m_task_relation)->get_num_tasks();
91  options.tasks_indices = tasks;
92  malsar_result_t model = malsar_joint_feature_learning(
93  features, y.vector, self->m_rho1, self->m_rho2, options);
94 
95  m_tasks_w = model.w;
96  m_tasks_c = model.c;
97 
98  return true;
99 }
100 
101 bool CMultitaskL12LogisticRegression::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 
121  malsar_result_t model = malsar_joint_feature_learning(
122  features, y.vector, self->m_rho1, self->m_rho2, options);
123 
124  m_tasks_w = model.w;
125  m_tasks_c = model.c;
126 
127  SG_FREE(options.tasks_indices);
128 
129  return true;
130 }
131 
132 }
133 
134 #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_ADD(...)
Definition: SGObject.h:84

SHOGUN Machine Learning Toolbox - Documentation