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  ASSERT(hinge_loss_i >= 0);
71 
72  hinge_losses += hinge_loss_i;
73 
74  SG_UNREF(result);
75  }
76 
77  return (lbda/2*SGVector<float64_t>::dot(w.vector, w.vector, w.vlen) + hinge_losses/N);
78 }
79 
81 {
82  return (lbda/2*SGVector<float64_t>::dot(w.vector, w.vector, w.vlen) - b_alpha);
83 }
84 
86 {
87  float64_t loss = 0.0;
88  CStructuredLabels* labels = model->get_labels();
89  int32_t N = labels->get_num_labels();
90  SG_UNREF(labels);
91 
92  for (int32_t i = 0; i < N; i++)
93  {
94  // solve the standard inference for point i
95  CResultSet* result = model->argmax(w, i, false);
96 
97  loss += result->delta;
98 
99  SG_UNREF(result);
100  }
101 
102  return loss / N;
103 }
104 
105 void CSOSVMHelper::add_debug_info(float64_t primal, float64_t eff_pass, float64_t train_error,
106  float64_t dual, float64_t dgap)
107 {
108  if (m_tracker >= m_bufsize)
109  {
110  SG_PRINT("%s::add_debug_information(): Buffer overflows! No more values will be recorded!\n",
111  get_name());
112 
113  return;
114  }
115 
116  m_primal[m_tracker] = primal;
117  m_eff_pass[m_tracker] = eff_pass;
118  m_train_error[m_tracker] = train_error;
119 
120  if (dgap >= 0)
121  {
122  m_dual[m_tracker] = dual;
123  m_duality_gap[m_tracker] = dgap;
124  }
125 
126  m_tracker++;
127 }
128 
130 {
131  return m_primal;
132 }
133 
135 {
136  return m_dual;
137 }
138 
140 {
141  return m_duality_gap;
142 }
143 
145 {
146  return m_eff_pass;
147 }
148 
150 {
151  return m_train_error;
152 }
153 
155 {
156  m_primal.resize_vector(m_tracker);
157  m_dual.resize_vector(m_tracker);
158  m_duality_gap.resize_vector(m_tracker);
159  m_eff_pass.resize_vector(m_tracker);
160  m_train_error.resize_vector(m_tracker);
161 }

SHOGUN Machine Learning Toolbox - Documentation