SHOGUN  v3.2.0
Machine.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) 1999-2009 Soeren Sonnenburg
8  * Written (W) 2011-2012 Heiko Strathmann
9  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
10  */
11 
12 #include <shogun/machine/Machine.h>
13 #include <shogun/base/Parameter.h>
16 
17 using namespace shogun;
18 
19 CMachine::CMachine() : CSGObject(), m_max_train_time(0), m_labels(NULL),
20  m_solver_type(ST_AUTO)
21 {
22  m_data_locked=false;
24 
25  SG_ADD(&m_max_train_time, "max_train_time",
26  "Maximum training time.", MS_NOT_AVAILABLE);
27  SG_ADD((machine_int_t*) &m_solver_type, "solver_type",
28  "Type of solver.", MS_NOT_AVAILABLE);
29  SG_ADD((CSGObject**) &m_labels, "labels",
30  "Labels to be used.", MS_NOT_AVAILABLE);
31  SG_ADD(&m_store_model_features, "store_model_features",
32  "Should feature data of model be stored after training?", MS_NOT_AVAILABLE);
33  SG_ADD(&m_data_locked, "data_locked",
34  "Indicates whether data is locked", MS_NOT_AVAILABLE);
35 
37  new SGParamInfo("data_locked", CT_SCALAR, ST_NONE, PT_BOOL, 1),
38  new SGParamInfo()
39  );
40 
42 }
43 
45 {
47 }
48 
50 {
51  /* not allowed to train on locked data */
52  if (m_data_locked)
53  {
54  SG_ERROR("%s::train data_lock() was called, only train_locked() is"
55  " possible. Call data_unlock if you want to call train()\n",
56  get_name());
57  }
58 
60  {
61  if (m_labels == NULL)
62  SG_ERROR("%s@%p: No labels given", get_name(), this)
63 
65  }
66 
67  bool result = train_machine(data);
68 
71 
72  return result;
73 }
74 
76 {
77  if (lab != NULL)
78  if (!is_label_valid(lab))
79  SG_ERROR("Invalid label for %s", get_name())
80 
81  SG_REF(lab);
83  m_labels = lab;
84 }
85 
87 {
89  return m_labels;
90 }
91 
93 {
94  m_max_train_time = t;
95 }
96 
98 {
99  return m_max_train_time;
100 }
101 
103 {
104  return CT_NONE;
105 }
106 
108 {
109  m_solver_type = st;
110 }
111 
113 {
114  return m_solver_type;
115 }
116 
118 {
119  m_store_model_features = store_model;
120 }
121 
122 void CMachine::data_lock(CLabels* labs, CFeatures* features)
123 {
124  SG_DEBUG("entering %s::data_lock\n", get_name())
125  if (!supports_locking())
126  {
127  {
128  SG_ERROR("%s::data_lock(): Machine does not support data locking!\n",
129  get_name());
130  }
131  }
132 
133  if (!labs)
134  {
135  SG_ERROR("%s::data_lock() is not possible will NULL labels!\n",
136  get_name());
137  }
138 
139  /* first set labels */
140  set_labels(labs);
141 
142  if (m_data_locked)
143  {
144  SG_ERROR("%s::data_lock() was already called. Dont lock twice!",
145  get_name());
146  }
147 
148  m_data_locked=true;
149  post_lock(labs,features);
150  SG_DEBUG("leaving %s::data_lock\n", get_name())
151 }
152 
154 {
155  SG_DEBUG("entering %s::data_lock\n", get_name())
156  if (m_data_locked)
157  m_data_locked=false;
158 
159  SG_DEBUG("leaving %s::data_lock\n", get_name())
160 }
161 
163 {
164  SG_DEBUG("entering %s::apply(%s at %p)\n",
165  get_name(), data ? data->get_name() : "NULL", data);
166 
167  CLabels* result=NULL;
168 
169  switch (get_machine_problem_type())
170  {
171  case PT_BINARY:
172  result=apply_binary(data);
173  break;
174  case PT_REGRESSION:
175  result=apply_regression(data);
176  break;
177  case PT_MULTICLASS:
178  result=apply_multiclass(data);
179  break;
180  case PT_STRUCTURED:
181  result=apply_structured(data);
182  break;
183  case PT_LATENT:
184  result=apply_latent(data);
185  break;
186  default:
187  SG_ERROR("Unknown problem type")
188  break;
189  }
190 
191  SG_DEBUG("leaving %s::apply(%s at %p)\n",
192  get_name(), data ? data->get_name() : "NULL", data);
193 
194  return result;
195 }
196 
198 {
199  switch (get_machine_problem_type())
200  {
201  case PT_BINARY:
202  return apply_locked_binary(indices);
203  case PT_REGRESSION:
204  return apply_locked_regression(indices);
205  case PT_MULTICLASS:
206  return apply_locked_multiclass(indices);
207  case PT_STRUCTURED:
208  return apply_locked_structured(indices);
209  case PT_LATENT:
210  return apply_locked_latent(indices);
211  default:
212  SG_ERROR("Unknown problem type")
213  break;
214  }
215  return NULL;
216 }
217 
219 {
220  SG_ERROR("This machine does not support apply_binary()\n")
221  return NULL;
222 }
223 
225 {
226  SG_ERROR("This machine does not support apply_regression()\n")
227  return NULL;
228 }
229 
231 {
232  SG_ERROR("This machine does not support apply_multiclass()\n")
233  return NULL;
234 }
235 
237 {
238  SG_ERROR("This machine does not support apply_structured()\n")
239  return NULL;
240 }
241 
243 {
244  SG_ERROR("This machine does not support apply_latent()\n")
245  return NULL;
246 }
247 
249 {
250  SG_ERROR("apply_locked_binary(SGVector<index_t>) is not yet implemented "
251  "for %s\n", get_name());
252  return NULL;
253 }
254 
256 {
257  SG_ERROR("apply_locked_regression(SGVector<index_t>) is not yet implemented "
258  "for %s\n", get_name());
259  return NULL;
260 }
261 
263 {
264  SG_ERROR("apply_locked_multiclass(SGVector<index_t>) is not yet implemented "
265  "for %s\n", get_name());
266  return NULL;
267 }
268 
270 {
271  SG_ERROR("apply_locked_structured(SGVector<index_t>) is not yet implemented "
272  "for %s\n", get_name());
273  return NULL;
274 }
275 
277 {
278  SG_ERROR("apply_locked_latent(SGVector<index_t>) is not yet implemented "
279  "for %s\n", get_name());
280  return NULL;
281 }
282 
283 
virtual const char * get_name() const =0
EMachineType
Definition: Machine.h:33
void set_max_train_time(float64_t t)
Definition: Machine.cpp:92
Base class of the labels used in Structured Output (SO) problems.
Class that holds informations about a certain parameter of an CSGObject. Contains name...
Definition: ParameterMap.h:28
Real Labels are real-valued labels.
ParameterMap * m_parameter_map
Definition: SGObject.h:491
virtual CLabels * apply_locked(SGVector< index_t > indices)
Definition: Machine.cpp:197
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:35
#define SG_UNREF(x)
Definition: SGRefObject.h:35
ESolverType
Definition: Machine.h:94
float64_t m_max_train_time
Definition: Machine.h:353
CLabels * m_labels
Definition: Machine.h:356
#define SG_ERROR(...)
Definition: SGIO.h:131
void put(const SGParamInfo *key, const SGParamInfo *value)
ESolverType m_solver_type
Definition: Machine.h:359
bool m_data_locked
Definition: Machine.h:365
virtual CStructuredLabels * apply_locked_structured(SGVector< index_t > indices)
Definition: Machine.cpp:269
virtual bool train_machine(CFeatures *data=NULL)
Definition: Machine.h:313
bool m_store_model_features
Definition: Machine.h:362
virtual const char * get_name() const
Definition: Machine.h:300
Multiclass Labels for multi-class classification.
virtual CBinaryLabels * apply_binary(CFeatures *data=NULL)
Definition: Machine.cpp:218
virtual void set_store_model_features(bool store_model)
Definition: Machine.cpp:117
virtual ~CMachine()
Definition: Machine.cpp:44
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:102
virtual bool supports_locking() const
Definition: Machine.h:288
double float64_t
Definition: common.h:48
virtual CRegressionLabels * apply_regression(CFeatures *data=NULL)
Definition: Machine.cpp:224
virtual void data_unlock()
Definition: Machine.cpp:153
#define SG_REF(x)
Definition: SGRefObject.h:34
virtual void data_lock(CLabels *labs, CFeatures *features)
Definition: Machine.cpp:122
virtual CLabels * get_labels()
Definition: Machine.cpp:86
float64_t get_max_train_time()
Definition: Machine.cpp:97
ESolverType get_solver_type()
Definition: Machine.cpp:112
virtual CLatentLabels * apply_latent(CFeatures *data=NULL)
Definition: Machine.cpp:242
virtual EMachineType get_classifier_type()
Definition: Machine.cpp:102
virtual CRegressionLabels * apply_locked_regression(SGVector< index_t > indices)
Definition: Machine.cpp:255
virtual void store_model_features()
Definition: Machine.h:330
virtual CMulticlassLabels * apply_locked_multiclass(SGVector< index_t > indices)
Definition: Machine.cpp:262
virtual bool train_require_labels() const
Definition: Machine.h:349
virtual CStructuredLabels * apply_structured(CFeatures *data=NULL)
Definition: Machine.cpp:236
#define SG_DEBUG(...)
Definition: SGIO.h:109
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:16
virtual void post_lock(CLabels *labs, CFeatures *features)
Definition: Machine.h:282
int machine_int_t
Definition: common.h:57
The class Features is the base class of all feature objects.
Definition: Features.h:62
virtual CBinaryLabels * apply_locked_binary(SGVector< index_t > indices)
Definition: Machine.cpp:248
virtual bool train(CFeatures *data=NULL)
Definition: Machine.cpp:49
Binary Labels for binary classification.
Definition: BinaryLabels.h:36
virtual CMulticlassLabels * apply_multiclass(CFeatures *data=NULL)
Definition: Machine.cpp:230
#define SG_ADD(...)
Definition: SGObject.h:71
virtual EProblemType get_machine_problem_type() const
Definition: Machine.h:294
virtual CLatentLabels * apply_locked_latent(SGVector< index_t > indices)
Definition: Machine.cpp:276
virtual void set_labels(CLabels *lab)
Definition: Machine.cpp:75
abstract class for latent labels As latent labels always depends on the given application, this class only defines the API that the user has to implement for latent labels.
Definition: LatentLabels.h:24
virtual void ensure_valid(const char *context=NULL)=0
void set_solver_type(ESolverType st)
Definition: Machine.cpp:107
virtual bool is_label_valid(CLabels *lab) const
Definition: Machine.h:343
virtual CLabels * apply(CFeatures *data=NULL)
Definition: Machine.cpp:162

SHOGUN Machine Learning Toolbox - Documentation