SHOGUN  v3.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PrimalMosekSOSVM.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  * Written (W) 2012 Fernando José Iglesias García
8  * Copyright (C) 2012 Fernando José Iglesias García
9  */
10 
11 #ifdef USE_MOSEK
12 
14 #include <shogun/lib/List.h>
17 #include <shogun/loss/HingeLoss.h>
18 
19 using namespace shogun;
20 
21 CPrimalMosekSOSVM::CPrimalMosekSOSVM()
23  po_value(0.0)
24 {
25  init();
26 }
27 
28 CPrimalMosekSOSVM::CPrimalMosekSOSVM(
29  CStructuredModel* model,
30  CStructuredLabels* labs)
31 : CLinearStructuredOutputMachine(model, labs),
32  po_value(0.0)
33 {
34  init();
35 }
36 
37 void CPrimalMosekSOSVM::init()
38 {
39  SG_ADD(&m_slacks, "slacks", "Slacks vector", MS_NOT_AVAILABLE);
40  //FIXME model selection available for SO machines
41  SG_ADD(&m_regularization, "regularization", "Regularization constant", MS_NOT_AVAILABLE);
42  SG_ADD(&m_epsilon, "epsilon", "Violation tolerance", MS_NOT_AVAILABLE);
43 
44  m_regularization = 1.0;
45  m_epsilon = 0.0;
46 }
47 
48 CPrimalMosekSOSVM::~CPrimalMosekSOSVM()
49 {
50 }
51 
52 bool CPrimalMosekSOSVM::train_machine(CFeatures* data)
53 {
54  SG_DEBUG("Entering CPrimalMosekSOSVM::train_machine.\n");
55  if (data)
56  set_features(data);
57 
58  CFeatures* model_features = get_features();
59  // Initialize the model for training
60  m_model->init_training();
61  // Check that the scenary is correct to start with training
62  m_model->check_training_setup();
63  SG_DEBUG("The training setup is correct.\n");
64 
65  // Dimensionality of the joint feature space
66  int32_t M = m_model->get_dim();
67  // Number of auxiliary variables in the optimization vector
68  int32_t num_aux = m_model->get_num_aux();
69  // Number of auxiliary constraints
70  int32_t num_aux_con = m_model->get_num_aux_con();
71  // Number of training examples
72  int32_t N = model_features->get_num_vectors();
73 
74  SG_DEBUG("M=%d, N =%d, num_aux=%d, num_aux_con=%d.\n", M, N, num_aux, num_aux_con);
75 
76  // Interface with MOSEK
77  CMosek* mosek = new CMosek(0, M+num_aux+N);
78  SG_REF(mosek);
79  if ( mosek->get_rescode() != MSK_RES_OK )
80  {
81  SG_PRINT("Mosek object could not be properly created..."
82  "aborting training of PrimalMosekSOSVM\n");
83 
84  return false;
85  }
86 
87  // Initialize the terms of the optimization problem
88  SGMatrix< float64_t > A, B, C;
89  SGVector< float64_t > a, b, lb, ub;
90  m_model->init_primal_opt(m_regularization, A, a, B, b, lb, ub, C);
91 
92  SG_DEBUG("Regularization used in PrimalMosekSOSVM equal to %.2f.\n", m_regularization);
93 
94  // Input terms of the problem that do not change between iterations
95  if ( mosek->init_sosvm(M, N, num_aux, num_aux_con, C, lb, ub, A, b) != MSK_RES_OK )
96  {
97  // MOSEK error took place
98  return false;
99  }
100 
101  // Initialize the weight vector
102  m_w = SGVector< float64_t >(M);
103  m_w.zero();
104 
105  m_slacks = SGVector< float64_t >(N);
106  m_slacks.zero();
107 
108  // Initialize the list of constraints
109  // Each element in results is a list of CResultSet with the constraints
110  // associated to each training example
111  CDynamicObjectArray* results = new CDynamicObjectArray(N);
112  SG_REF(results);
113  for ( int32_t i = 0 ; i < N ; ++i )
114  {
115  CList* list = new CList(true);
116  results->push_back(list);
117  }
118 
119  // Initialize variables used in the loop
120  int32_t num_con = num_aux_con; // number of constraints
121  int32_t old_num_con = num_con;
122  float64_t slack = 0.0;
123  float64_t max_slack = 0.0;
124  CResultSet* result = NULL;
125  CResultSet* cur_res = NULL;
126  CList* cur_list = NULL;
127  bool exception = false;
128  index_t iteration = 0;
129 
130  SGVector< float64_t > sol(M+num_aux+N);
131  sol.zero();
132 
133  SGVector< float64_t > aux(num_aux);
134 
135  do
136  {
137  SG_DEBUG("Iteration #%d: Cutting plane training with num_con=%d and old_num_con=%d.\n",
138  iteration, num_con, old_num_con);
139 
140  old_num_con = num_con;
141 
142  for ( int32_t i = 0 ; i < N ; ++i )
143  {
144  // Predict the result of the ith training example (loss-aug)
145  result = m_model->argmax(m_w, i);
146 
147  // Compute the loss associated with the prediction (surrogate loss, max(0, \tilde{H}))
148  slack = CHingeLoss().loss( compute_loss_arg(result) );
149  cur_list = (CList*) results->get_element(i);
150 
151  // Update the list of constraints
152  if ( cur_list->get_num_elements() > 0 )
153  {
154  // Find the maximum loss within the elements of
155  // the list of constraints
156  cur_res = (CResultSet*) cur_list->get_first_element();
157  max_slack = -CMath::INFTY;
158 
159  while ( cur_res != NULL )
160  {
161  max_slack = CMath::max(max_slack,
162  CHingeLoss().loss( compute_loss_arg(cur_res) ));
163 
164  SG_UNREF(cur_res);
165  cur_res = (CResultSet*) cur_list->get_next_element();
166  }
167 
168  if ( slack > max_slack + m_epsilon )
169  {
170  // The current training example is a
171  // violated constraint
172  if ( ! insert_result(cur_list, result) )
173  {
174  exception = true;
175  break;
176  }
177 
178  add_constraint(mosek, result, num_con, i);
179  ++num_con;
180  }
181  }
182  else
183  {
184  // First iteration of do ... while, add constraint
185  if ( ! insert_result(cur_list, result) )
186  {
187  exception = true;
188  break;
189  }
190 
191  add_constraint(mosek, result, num_con, i);
192  ++num_con;
193  }
194 
195  SG_UNREF(cur_list);
196  SG_UNREF(result);
197  }
198 
199  // Solve the QP
200  SG_DEBUG("Entering Mosek QP solver.\n");
201 
202  mosek->optimize(sol);
203  for ( int32_t i = 0 ; i < M+num_aux+N ; ++i )
204  {
205  if ( i < M )
206  m_w[i] = sol[i];
207  else if ( i < M+num_aux )
208  aux[i-M] = sol[i];
209  else
210  m_slacks[i-M-num_aux] = sol[i];
211  }
212 
213  SG_DEBUG("QP solved. The primal objective value is %.4f.\n", mosek->get_primal_objective_value());
214 
215  ++iteration;
216 
217  } while ( old_num_con != num_con && ! exception );
218 
219  po_value = mosek->get_primal_objective_value();
220 
221  // Free resources
222  SG_UNREF(results);
223  SG_UNREF(mosek);
224  SG_UNREF(model_features);
225  return true;
226 }
227 
228 float64_t CPrimalMosekSOSVM::compute_loss_arg(CResultSet* result) const
229 {
230  // Dimensionality of the joint feature space
231  int32_t M = m_w.vlen;
232 
233  return SGVector< float64_t >::dot(m_w.vector, result->psi_pred.vector, M) +
234  result->delta -
235  SGVector< float64_t >::dot(m_w.vector, result->psi_truth.vector, M);
236 }
237 
238 bool CPrimalMosekSOSVM::insert_result(CList* result_list, CResultSet* result) const
239 {
240  bool succeed = result_list->insert_element(result);
241 
242  if ( ! succeed )
243  {
244  SG_PRINT("ResultSet could not be inserted in the list..."
245  "aborting training of PrimalMosekSOSVM\n");
246  }
247 
248  return succeed;
249 }
250 
251 bool CPrimalMosekSOSVM::add_constraint(
252  CMosek* mosek,
253  CResultSet* result,
254  index_t con_idx,
255  index_t train_idx) const
256 {
257  int32_t M = m_model->get_dim();
258  SGVector< float64_t > dPsi(M);
259 
260  for ( int i = 0 ; i < M ; ++i )
261  dPsi[i] = result->psi_pred[i] - result->psi_truth[i]; // -dPsi(y)
262 
263  return ( mosek->add_constraint_sosvm(dPsi, con_idx, train_idx,
264  m_model->get_num_aux(), -result->delta) == MSK_RES_OK );
265 }
266 
267 
268 float64_t CPrimalMosekSOSVM::compute_primal_objective() const
269 {
270  return po_value;
271 }
272 
273 EMachineType CPrimalMosekSOSVM::get_classifier_type()
274 {
275  return CT_PRIMALMOSEKSOSVM;
276 }
277 
278 void CPrimalMosekSOSVM::set_regularization(float64_t C)
279 {
280  m_regularization = C;
281 }
282 
284 {
285  m_epsilon = epsilon;
286 }
287 
288 #endif /* USE_MOSEK */

SHOGUN Machine Learning Toolbox - Documentation