SHOGUN  6.1.3
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 
19 
20 using namespace shogun;
21 
23 : CMachine(), m_model(NULL), m_surrogate_loss(NULL)
24 {
25  register_parameters();
26 }
27 
29  CStructuredModel* model,
30  CStructuredLabels* labs)
31 : CMachine(), m_model(model), m_surrogate_loss(NULL)
32 {
33  SG_REF(m_model);
34  set_labels(labs);
35  register_parameters();
36 }
37 
39 {
43 }
44 
46 {
47  SG_REF(model);
49  m_model = model;
50 }
51 
53 {
54  SG_REF(m_model);
55  return m_model;
56 }
57 
58 void CStructuredOutputMachine::register_parameters()
59 {
60  SG_ADD((CSGObject**)&m_model, "m_model", "Structured model", MS_NOT_AVAILABLE);
61  SG_ADD((CSGObject**)&m_surrogate_loss, "m_surrogate_loss", "Surrogate loss", MS_NOT_AVAILABLE);
62  SG_ADD(&m_verbose, "verbose", "Verbosity flag", MS_NOT_AVAILABLE);
63  SG_ADD((CSGObject**)&m_helper, "helper", "Training helper", MS_NOT_AVAILABLE);
64 
65  m_verbose = false;
66  m_helper = NULL;
67 }
68 
70 {
72  REQUIRE(m_model != NULL, "please call set_model() before set_labels()\n");
74 }
75 
77 {
79 }
80 
82 {
83  return m_model->get_features();
84 }
85 
87 {
88  SG_REF(loss);
90  m_surrogate_loss = loss;
91 }
92 
94 {
96  return m_surrogate_loss;
97 }
98 
100 {
101  int32_t dim = m_model->get_dim();
102 
103  int32_t from=0, to=0;
104  CFeatures* features = get_features();
105  if (info)
106  {
107  from = info->m_from;
108  to = (info->m_N == 0) ? features->get_num_vectors() : from+info->m_N;
109  }
110  else
111  {
112  from = 0;
113  to = features->get_num_vectors();
114  }
115  SG_UNREF(features);
116 
117  float64_t R = 0.0;
118  linalg::zero(subgrad);
119 
120  for (int32_t i=from; i<to; i++)
121  {
122  CResultSet* result = m_model->argmax(SGVector<float64_t>(W.vector,dim,false), i, true);
123  SGVector<float64_t> psi_pred = result->psi_pred;
124  SGVector<float64_t> psi_truth = result->psi_truth;
126  SGVector<float64_t>::vec1_plus_scalar_times_vec2(subgrad.vector, -1.0, psi_truth.vector, dim);
127  R += result->score;
128  SG_UNREF(result);
129  }
130 
131  return R;
132 }
133 
135 {
136  SG_ERROR("%s::risk_nslack_slack_rescale() has not been implemented!\n", get_name());
137  return 0.0;
138 }
139 
141 {
142  SG_ERROR("%s::risk_1slack_margin_rescale() has not been implemented!\n", get_name());
143  return 0.0;
144 }
145 
147 {
148  SG_ERROR("%s::risk_1slack_slack_rescale() has not been implemented!\n", get_name());
149  return 0.0;
150 }
151 
153 {
154  SG_ERROR("%s::risk_customized_formulation() has not been implemented!\n", get_name());
155  return 0.0;
156 }
157 
159  TMultipleCPinfo* info, EStructRiskType rtype)
160 {
161  float64_t ret = 0.0;
162  switch(rtype)
163  {
165  ret = risk_nslack_margin_rescale(subgrad, W, info);
166  break;
168  ret = risk_nslack_slack_rescale(subgrad, W, info);
169  break;
171  ret = risk_1slack_margin_rescale(subgrad, W, info);
172  break;
174  ret = risk_1slack_slack_rescale(subgrad, W, info);
175  break;
176  case CUSTOMIZED_RISK:
177  ret = risk_customized_formulation(subgrad, W, info);
178  break;
179  default:
180  SG_ERROR("%s::risk(): cannot recognize the risk type!\n", get_name());
181  ret = -1;
182  break;
183  }
184  return ret;
185 }
186 
188 {
189  if (m_helper == NULL)
190  {
191  SG_ERROR("%s::get_helper(): no helper has been created!"
192  "Please set verbose before training!\n", get_name());
193  }
194 
195  SG_REF(m_helper);
196  return m_helper;
197 }
198 
200 {
201  m_verbose = verbose;
202 }
203 
205 {
206  return m_verbose;
207 }
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
virtual float64_t risk_customized_formulation(SGVector< float64_t > &subgrad, SGVector< float64_t > &W, TMultipleCPinfo *info=0)
void set_labels(CStructuredLabels *labs)
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
virtual int32_t get_num_vectors() const =0
virtual float64_t risk_1slack_margin_rescale(SGVector< float64_t > &subgrad, SGVector< float64_t > &W, TMultipleCPinfo *info=0)
#define SG_ERROR(...)
Definition: SGIO.h:128
#define REQUIRE(x,...)
Definition: SGIO.h:181
virtual int32_t get_dim() const =0
CLossFunction * get_surrogate_loss() const
#define SG_REF(x)
Definition: SGObject.h:52
virtual const char * get_name() const
A generic learning machine interface.
Definition: Machine.h:151
void set_features(CFeatures *feats)
virtual float64_t risk_nslack_margin_rescale(SGVector< float64_t > &subgrad, SGVector< float64_t > &W, TMultipleCPinfo *info=0)
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:124
void zero(Container< T > &a)
void set_model(CStructuredModel *model)
double float64_t
Definition: common.h:60
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(SGVector< float64_t > &subgrad, SGVector< float64_t > &W, TMultipleCPinfo *info=0, EStructRiskType rtype=N_SLACK_MARGIN_RESCALING)
Class CStructuredModel that represents the application specific model and contains most of the applic...
#define SG_UNREF(x)
Definition: SGObject.h:53
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:69
virtual float64_t risk_1slack_slack_rescale(SGVector< float64_t > &subgrad, SGVector< float64_t > &W, TMultipleCPinfo *info=0)
virtual float64_t risk_nslack_slack_rescale(SGVector< float64_t > &subgrad, SGVector< float64_t > &W, TMultipleCPinfo *info=0)
SGVector< float64_t > psi_pred
CStructuredModel * get_model() const
static CStructuredLabels * to_structured(CLabels *base_labels)
#define SG_ADD(...)
Definition: SGObject.h:93
virtual void set_labels(CLabels *lab)
Definition: Machine.cpp:72
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:586
void set_surrogate_loss(CLossFunction *loss)

SHOGUN Machine Learning Toolbox - Documentation