SHOGUN  4.1.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
SOSVMHelper.cpp
浏览该文件的文档.
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 }
Base class of the labels used in Structured Output (SO) problems.
virtual const char * get_name() const
Definition: SOSVMHelper.h:47
static float64_t primal_objective(SGVector< float64_t > w, CStructuredModel *model, float64_t lbda)
Definition: SOSVMHelper.cpp:57
CStructuredLabels * get_labels()
index_t vlen
Definition: SGVector.h:494
SGVector< float64_t > get_dual_values() const
#define SG_PRINT(...)
Definition: SGIO.h:137
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:112
double float64_t
Definition: common.h:50
static float64_t dual_objective(SGVector< float64_t > w, float64_t aloss, float64_t lbda)
Definition: SOSVMHelper.cpp:83
static float64_t dot(const bool *v1, const bool *v2, int32_t n)
Compute dot product between v1 and v2 (blas optimized)
Definition: Math.h:627
static float64_t average_loss(SGVector< float64_t > w, CStructuredModel *model, bool is_ub=false)
Definition: SOSVMHelper.cpp:88
Class CStructuredModel that represents the application specific model and contains most of the applic...
#define SG_UNREF(x)
Definition: SGObject.h:52
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
virtual CResultSet * argmax(SGVector< float64_t > w, int32_t feat_idx, bool const training=true)=0
virtual void add_debug_info(float64_t primal, float64_t eff_pass, float64_t train_error, float64_t dual=-1, float64_t dgap=-1)
virtual int32_t get_num_labels() const
SGVector< float64_t > get_duality_gaps() const
SGVector< float64_t > get_primal_values() const
void resize_vector(int32_t n)
Definition: SGVector.cpp:259
#define SG_ADD(...)
Definition: SGObject.h:81
SGVector< float64_t > get_train_errors() const
SGVector< float64_t > get_eff_passes() const

SHOGUN 机器学习工具包 - 项目文档