SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
StructuredOutputMachine.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  * Written (W) 2013 Thoralf Klein
9  * Written (W) 2012 Fernando José Iglesias García
10  * Copyright (C) 2012 Fernando José Iglesias García
11  */
12 
17 
18 using namespace shogun;
19 
21 : CMachine(), m_model(NULL), m_surrogate_loss(NULL)
22 {
23  register_parameters();
24 }
25 
27  CStructuredModel* model,
28  CStructuredLabels* labs)
29 : CMachine(), m_model(model), m_surrogate_loss(NULL)
30 {
31  SG_REF(m_model);
32  set_labels(labs);
33  register_parameters();
34 }
35 
37 {
41 }
42 
44 {
45  SG_REF(model);
47  m_model = model;
48 }
49 
51 {
52  SG_REF(m_model);
53  return m_model;
54 }
55 
56 void CStructuredOutputMachine::register_parameters()
57 {
58  SG_ADD((CSGObject**)&m_model, "m_model", "Structured model", MS_NOT_AVAILABLE);
59  SG_ADD((CSGObject**)&m_surrogate_loss, "m_surrogate_loss", "Surrogate loss", MS_NOT_AVAILABLE);
60  SG_ADD(&m_verbose, "verbose", "Verbosity flag", MS_NOT_AVAILABLE);
61  SG_ADD((CSGObject**)&m_helper, "helper", "Training helper", MS_NOT_AVAILABLE);
62 
63  m_verbose = false;
64  m_helper = NULL;
65 }
66 
68 {
70  REQUIRE(m_model != NULL, "please call set_model() before set_labels()\n");
72 }
73 
75 {
77 }
78 
80 {
81  return m_model->get_features();
82 }
83 
85 {
86  SG_REF(loss);
88  m_surrogate_loss = loss;
89 }
90 
92 {
94  return m_surrogate_loss;
95 }
96 
98 {
99  int32_t dim = m_model->get_dim();
100 
101  int32_t from=0, to=0;
102  CFeatures* features = get_features();
103  if (info)
104  {
105  from = info->m_from;
106  to = (info->m_N == 0) ? features->get_num_vectors() : from+info->m_N;
107  }
108  else
109  {
110  from = 0;
111  to = features->get_num_vectors();
112  }
113  SG_UNREF(features);
114 
115  float64_t R = 0.0;
116  for (int32_t i=0; i<dim; i++)
117  subgrad[i] = 0;
118 
119  for (int32_t i=from; i<to; i++)
120  {
121  CResultSet* result = m_model->argmax(SGVector<float64_t>(W,dim,false), i, true);
122  SGVector<float64_t> psi_pred = result->psi_pred;
123  SGVector<float64_t> psi_truth = result->psi_truth;
124  SGVector<float64_t>::vec1_plus_scalar_times_vec2(subgrad, 1.0, psi_pred.vector, dim);
125  SGVector<float64_t>::vec1_plus_scalar_times_vec2(subgrad, -1.0, psi_truth.vector, dim);
126  R += result->score;
127  SG_UNREF(result);
128  }
129 
130  return R;
131 }
132 
134 {
135  SG_ERROR("%s::risk_nslack_slack_rescale() has not been implemented!\n", get_name());
136  return 0.0;
137 }
138 
140 {
141  SG_ERROR("%s::risk_1slack_margin_rescale() has not been implemented!\n", get_name());
142  return 0.0;
143 }
144 
146 {
147  SG_ERROR("%s::risk_1slack_slack_rescale() has not been implemented!\n", get_name());
148  return 0.0;
149 }
150 
152 {
153  SG_ERROR("%s::risk_customized_formulation() has not been implemented!\n", get_name());
154  return 0.0;
155 }
156 
158  TMultipleCPinfo* info, EStructRiskType rtype)
159 {
160  float64_t ret = 0.0;
161  switch(rtype)
162  {
164  ret = risk_nslack_margin_rescale(subgrad, W, info);
165  break;
167  ret = risk_nslack_slack_rescale(subgrad, W, info);
168  break;
170  ret = risk_1slack_margin_rescale(subgrad, W, info);
171  break;
173  ret = risk_1slack_slack_rescale(subgrad, W, info);
174  break;
175  case CUSTOMIZED_RISK:
176  ret = risk_customized_formulation(subgrad, W, info);
177  break;
178  default:
179  SG_ERROR("%s::risk(): cannot recognize the risk type!\n", get_name());
180  ret = -1;
181  break;
182  }
183  return ret;
184 }
185 
187 {
188  if (m_helper == NULL)
189  {
190  SG_ERROR("%s::get_helper(): no helper has been created!"
191  "Please set verbose before training!\n", get_name());
192  }
193 
194  SG_REF(m_helper);
195  return m_helper;
196 }
197 
199 {
200  m_verbose = verbose;
201 }
202 
204 {
205  return m_verbose;
206 }
SGVector< float64_t > psi_truth
Base class of the labels used in Structured Output (SO) problems.
Class CLossFunction is the base class of all loss functions.
Definition: LossFunction.h:57
void set_labels(CStructuredLabels *labs)
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
virtual float64_t risk_customized_formulation(float64_t *subgrad, float64_t *W, TMultipleCPinfo *info=0)
virtual int32_t get_num_vectors() const =0
#define SG_ERROR(...)
Definition: SGIO.h:129
#define REQUIRE(x,...)
Definition: SGIO.h:206
virtual float64_t risk_1slack_slack_rescale(float64_t *subgrad, float64_t *W, TMultipleCPinfo *info=0)
virtual int32_t get_dim() const =0
CLossFunction * get_surrogate_loss() const
#define SG_REF(x)
Definition: SGObject.h:51
virtual const char * get_name() const
A generic learning machine interface.
Definition: Machine.h:143
void set_features(CFeatures *feats)
virtual float64_t risk_nslack_margin_rescale(float64_t *subgrad, float64_t *W, TMultipleCPinfo *info=0)
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:112
void set_model(CStructuredModel *model)
double float64_t
Definition: common.h:50
class CSOSVMHelper contains helper functions to compute primal objectives, dual objectives, average training losses, duality gaps etc. These values will be recorded to check convergence. This class is inspired by the matlab implementation of the block coordinate Frank-Wolfe SOSVM solver [1].
Definition: SOSVMHelper.h:31
virtual float64_t risk_nslack_slack_rescale(float64_t *subgrad, float64_t *W, TMultipleCPinfo *info=0)
virtual float64_t risk(float64_t *subgrad, float64_t *W, TMultipleCPinfo *info=0, EStructRiskType rtype=N_SLACK_MARGIN_RESCALING)
static void vec1_plus_scalar_times_vec2(T *vec1, const T scalar, const T *vec2, int32_t n)
x=x+alpha*y
Definition: SGVector.cpp:531
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
The class Features is the base class of all feature objects.
Definition: Features.h:68
SGVector< float64_t > psi_pred
CStructuredModel * get_model() const
static CStructuredLabels * to_structured(CLabels *base_labels)
#define SG_ADD(...)
Definition: SGObject.h:81
virtual void set_labels(CLabels *lab)
Definition: Machine.cpp:73
virtual float64_t risk_1slack_margin_rescale(float64_t *subgrad, float64_t *W, TMultipleCPinfo *info=0)
void set_surrogate_loss(CLossFunction *loss)

SHOGUN Machine Learning Toolbox - Documentation