SHOGUN  4.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SOSVMHelper.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) 2013 Shell Hu
8  * Copyright (C) 2013 Shell Hu
9  */
10 
12 #include <shogun/base/Parameter.h>
14  #include <shogun/mathematics/Math.h>
15 
16 using namespace shogun;
17 
19 {
20  init();
21 }
22 
24 {
25  m_bufsize = bufsize;
26  init();
27 }
28 
30 {
31 }
32 
33 void CSOSVMHelper::init()
34 {
35  SG_ADD(&m_primal, "primal", "History of primal values", MS_NOT_AVAILABLE);
36  SG_ADD(&m_dual, "dual", "History of dual values", MS_NOT_AVAILABLE);
37  SG_ADD(&m_duality_gap, "duality_gap", "History of duality gaps", MS_NOT_AVAILABLE);
38  SG_ADD(&m_eff_pass, "eff_pass", "Effective passes", MS_NOT_AVAILABLE);
39  SG_ADD(&m_train_error, "train_error", "History of training errors", MS_NOT_AVAILABLE);
40  SG_ADD(&m_tracker, "tracker", "Tracker of training progress", MS_NOT_AVAILABLE);
41  SG_ADD(&m_bufsize, "bufsize", "Buffer size", MS_NOT_AVAILABLE);
42 
43  m_tracker = 0;
44  m_bufsize = 1000;
45  m_primal = SGVector<float64_t>(m_bufsize);
46  m_dual = SGVector<float64_t>(m_bufsize);
47  m_duality_gap = SGVector<float64_t>(m_bufsize);
48  m_eff_pass = SGVector<float64_t>(m_bufsize);
49  m_train_error = SGVector<float64_t>(m_bufsize);
50  m_primal.zero();
51  m_dual.zero();
52  m_duality_gap.zero();
53  m_eff_pass.zero();
54  m_train_error.zero();
55 }
56 
58 {
59  float64_t hinge_losses = 0.0;
60  CStructuredLabels* labels = model->get_labels();
61  int32_t N = labels->get_num_labels();
62  SG_UNREF(labels);
63 
64  for (int32_t i = 0; i < N; i++)
65  {
66  // solve the loss-augmented inference for point i
67  CResultSet* result = model->argmax(w, i);
68 
69  // hinge loss for point i
70  float64_t hinge_loss_i = result->score;
71 
72  if (hinge_loss_i < 0)
73  hinge_loss_i = 0;
74 
75  hinge_losses += hinge_loss_i;
76 
77  SG_UNREF(result);
78  }
79 
80  return (lbda/2 * CMath::dot(w.vector, w.vector, w.vlen) + hinge_losses/N);
81 }
82 
84 {
85  return (-lbda/2 * CMath::dot(w.vector, w.vector, w.vlen) + aloss);
86 }
87 
89 {
90  float64_t loss = 0.0;
91  CStructuredLabels* labels = model->get_labels();
92  int32_t N = labels->get_num_labels();
93  SG_UNREF(labels);
94 
95  for (int32_t i = 0; i < N; i++)
96  {
97  // solve the standard inference for point i
98  CResultSet* result = model->argmax(w, i, is_ub);
99 
100  loss += result->delta;
101 
102  SG_UNREF(result);
103  }
104 
105  return loss / N;
106 }
107 
108 void CSOSVMHelper::add_debug_info(float64_t primal, float64_t eff_pass, float64_t train_error,
109  float64_t dual, float64_t dgap)
110 {
111  if (m_tracker >= m_bufsize)
112  {
113  SG_PRINT("%s::add_debug_information(): Buffer overflows! No more values will be recorded!\n",
114  get_name());
115 
116  return;
117  }
118 
119  m_primal[m_tracker] = primal;
120  m_eff_pass[m_tracker] = eff_pass;
121  m_train_error[m_tracker] = train_error;
122 
123  if (dgap >= 0)
124  {
125  m_dual[m_tracker] = dual;
126  m_duality_gap[m_tracker] = dgap;
127  }
128 
129  m_tracker++;
130 }
131 
133 {
134  return m_primal;
135 }
136 
138 {
139  return m_dual;
140 }
141 
143 {
144  return m_duality_gap;
145 }
146 
148 {
149  return m_eff_pass;
150 }
151 
153 {
154  return m_train_error;
155 }
156 
158 {
159  m_primal.resize_vector(m_tracker);
160  m_dual.resize_vector(m_tracker);
161  m_duality_gap.resize_vector(m_tracker);
162  m_eff_pass.resize_vector(m_tracker);
163  m_train_error.resize_vector(m_tracker);
164 }

SHOGUN Machine Learning Toolbox - Documentation