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

SHOGUN Machine Learning Toolbox - Documentation