SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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>
14 
15 using namespace shogun;
16 
17 CMachine::CMachine() : CSGObject(), m_max_train_time(0), m_labels(NULL),
18  m_solver_type(ST_AUTO)
19 {
20  m_data_locked=false;
22 
23  SG_ADD(&m_max_train_time, "max_train_time",
24  "Maximum training time.", MS_NOT_AVAILABLE);
25  SG_ADD((machine_int_t*) &m_solver_type, "solver_type",
26  "Type of solver.", MS_NOT_AVAILABLE);
27  SG_ADD((CSGObject**) &m_labels, "labels",
28  "Labels to be used.", MS_NOT_AVAILABLE);
29  SG_ADD(&m_store_model_features, "store_model_features",
30  "Should feature data of model be stored after training?", MS_NOT_AVAILABLE);
31  SG_ADD(&m_data_locked, "data_locked",
32  "Indicates whether data is locked", MS_NOT_AVAILABLE);
33 
35  new SGParamInfo("data_locked", CT_SCALAR, ST_NONE, PT_BOOL, 1),
36  new SGParamInfo()
37  );
38 
40 }
41 
43 {
45 }
46 
48 {
49  /* not allowed to train on locked data */
50  if (m_data_locked)
51  {
52  SG_ERROR("%s::train data_lock() was called, only train_locked() is"
53  " possible. Call data_unlock if you want to call train()\n",
54  get_name());
55  }
56 
58  {
59  if (m_labels == NULL)
60  SG_ERROR("%s@%p: No labels given", get_name(), this)
61 
63  }
64 
65  bool result = train_machine(data);
66 
69 
70  return result;
71 }
72 
74 {
75  if (lab != NULL)
76  if (!is_label_valid(lab))
77  SG_ERROR("Invalid label for %s", get_name())
78 
79  SG_REF(lab);
81  m_labels = lab;
82 }
83 
85 {
87  return m_labels;
88 }
89 
91 {
92  m_max_train_time = t;
93 }
94 
96 {
97  return m_max_train_time;
98 }
99 
101 {
102  return CT_NONE;
103 }
104 
106 {
107  m_solver_type = st;
108 }
109 
111 {
112  return m_solver_type;
113 }
114 
116 {
117  m_store_model_features = store_model;
118 }
119 
120 void CMachine::data_lock(CLabels* labs, CFeatures* features)
121 {
122  SG_DEBUG("entering %s::data_lock\n", get_name())
123  if (!supports_locking())
124  {
125  {
126  SG_ERROR("%s::data_lock(): Machine does not support data locking!\n",
127  get_name());
128  }
129  }
130 
131  if (!labs)
132  {
133  SG_ERROR("%s::data_lock() is not possible will NULL labels!\n",
134  get_name());
135  }
136 
137  /* first set labels */
138  set_labels(labs);
139 
140  if (m_data_locked)
141  {
142  SG_ERROR("%s::data_lock() was already called. Dont lock twice!",
143  get_name());
144  }
145 
146  m_data_locked=true;
147  post_lock(labs,features);
148  SG_DEBUG("leaving %s::data_lock\n", get_name())
149 }
150 
152 {
153  SG_DEBUG("entering %s::data_lock\n", get_name())
154  if (m_data_locked)
155  m_data_locked=false;
156 
157  SG_DEBUG("leaving %s::data_lock\n", get_name())
158 }
159 
161 {
162  SG_DEBUG("entering %s::apply(%s at %p)\n",
163  get_name(), data ? data->get_name() : "NULL", data);
164 
165  CLabels* result=NULL;
166 
167  switch (get_machine_problem_type())
168  {
169  case PT_BINARY:
170  result=apply_binary(data);
171  break;
172  case PT_REGRESSION:
173  result=apply_regression(data);
174  break;
175  case PT_MULTICLASS:
176  result=apply_multiclass(data);
177  break;
178  case PT_STRUCTURED:
179  result=apply_structured(data);
180  break;
181  case PT_LATENT:
182  result=apply_latent(data);
183  break;
184  default:
185  SG_ERROR("Unknown problem type")
186  break;
187  }
188 
189  SG_DEBUG("leaving %s::apply(%s at %p)\n",
190  get_name(), data ? data->get_name() : "NULL", data);
191 
192  return result;
193 }
194 
196 {
197  switch (get_machine_problem_type())
198  {
199  case PT_BINARY:
200  return apply_locked_binary(indices);
201  case PT_REGRESSION:
202  return apply_locked_regression(indices);
203  case PT_MULTICLASS:
204  return apply_locked_multiclass(indices);
205  case PT_STRUCTURED:
206  return apply_locked_structured(indices);
207  case PT_LATENT:
208  return apply_locked_latent(indices);
209  default:
210  SG_ERROR("Unknown problem type")
211  break;
212  }
213  return NULL;
214 }
215 
217 {
218  SG_ERROR("This machine does not support apply_binary()\n")
219  return NULL;
220 }
221 
223 {
224  SG_ERROR("This machine does not support apply_regression()\n")
225  return NULL;
226 }
227 
229 {
230  SG_ERROR("This machine does not support apply_multiclass()\n")
231  return NULL;
232 }
233 
235 {
236  SG_ERROR("This machine does not support apply_structured()\n")
237  return NULL;
238 }
239 
241 {
242  SG_ERROR("This machine does not support apply_latent()\n")
243  return NULL;
244 }
245 
247 {
248  SG_ERROR("apply_locked_binary(SGVector<index_t>) is not yet implemented "
249  "for %s\n", get_name());
250  return NULL;
251 }
252 
254 {
255  SG_ERROR("apply_locked_regression(SGVector<index_t>) is not yet implemented "
256  "for %s\n", get_name());
257  return NULL;
258 }
259 
261 {
262  SG_ERROR("apply_locked_multiclass(SGVector<index_t>) is not yet implemented "
263  "for %s\n", get_name());
264  return NULL;
265 }
266 
268 {
269  SG_ERROR("apply_locked_structured(SGVector<index_t>) is not yet implemented "
270  "for %s\n", get_name());
271  return NULL;
272 }
273 
275 {
276  SG_ERROR("apply_locked_latent(SGVector<index_t>) is not yet implemented "
277  "for %s\n", get_name());
278  return NULL;
279 }
virtual const char * get_name() const =0
EMachineType
Definition: Machine.h:33
void set_max_train_time(float64_t t)
Definition: Machine.cpp:90
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:32
Real Labels are real-valued labels.
ParameterMap * m_parameter_map
Definition: SGObject.h:514
virtual CLabels * apply_locked(SGVector< index_t > indices)
Definition: Machine.cpp:195
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
ESolverType
Definition: Machine.h:98
float64_t m_max_train_time
Definition: Machine.h:358
CLabels * m_labels
Definition: Machine.h:361
#define SG_ERROR(...)
Definition: SGIO.h:129
void put(const SGParamInfo *key, const SGParamInfo *value)
ESolverType m_solver_type
Definition: Machine.h:364
bool m_data_locked
Definition: Machine.h:370
virtual CStructuredLabels * apply_locked_structured(SGVector< index_t > indices)
Definition: Machine.cpp:267
virtual bool train_machine(CFeatures *data=NULL)
Definition: Machine.h:318
bool m_store_model_features
Definition: Machine.h:367
virtual const char * get_name() const
Definition: Machine.h:305
#define SG_REF(x)
Definition: SGObject.h:51
Multiclass Labels for multi-class classification.
virtual CBinaryLabels * apply_binary(CFeatures *data=NULL)
Definition: Machine.cpp:216
virtual void set_store_model_features(bool store_model)
Definition: Machine.cpp:115
virtual ~CMachine()
Definition: Machine.cpp:42
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:112
double float64_t
Definition: common.h:50
virtual CRegressionLabels * apply_regression(CFeatures *data=NULL)
Definition: Machine.cpp:222
virtual void data_unlock()
Definition: Machine.cpp:151
virtual void data_lock(CLabels *labs, CFeatures *features)
Definition: Machine.cpp:120
virtual CLabels * get_labels()
Definition: Machine.cpp:84
float64_t get_max_train_time()
Definition: Machine.cpp:95
ESolverType get_solver_type()
Definition: Machine.cpp:110
virtual CLatentLabels * apply_latent(CFeatures *data=NULL)
Definition: Machine.cpp:240
virtual EMachineType get_classifier_type()
Definition: Machine.cpp:100
virtual EProblemType get_machine_problem_type() const
Definition: Machine.h:299
virtual CRegressionLabels * apply_locked_regression(SGVector< index_t > indices)
Definition: Machine.cpp:253
virtual void store_model_features()
Definition: Machine.h:335
virtual bool supports_locking() const
Definition: Machine.h:293
virtual CMulticlassLabels * apply_locked_multiclass(SGVector< index_t > indices)
Definition: Machine.cpp:260
#define SG_UNREF(x)
Definition: SGObject.h:52
virtual CStructuredLabels * apply_structured(CFeatures *data=NULL)
Definition: Machine.cpp:234
#define SG_DEBUG(...)
Definition: SGIO.h:107
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
virtual void post_lock(CLabels *labs, CFeatures *features)
Definition: Machine.h:287
int machine_int_t
Definition: common.h:59
virtual bool is_label_valid(CLabels *lab) const
Definition: Machine.h:348
The class Features is the base class of all feature objects.
Definition: Features.h:68
virtual CBinaryLabels * apply_locked_binary(SGVector< index_t > indices)
Definition: Machine.cpp:246
virtual bool train(CFeatures *data=NULL)
Definition: Machine.cpp:47
Binary Labels for binary classification.
Definition: BinaryLabels.h:37
virtual CMulticlassLabels * apply_multiclass(CFeatures *data=NULL)
Definition: Machine.cpp:228
virtual bool train_require_labels() const
Definition: Machine.h:354
#define SG_ADD(...)
Definition: SGObject.h:81
virtual CLatentLabels * apply_locked_latent(SGVector< index_t > indices)
Definition: Machine.cpp:274
virtual void set_labels(CLabels *lab)
Definition: Machine.cpp:73
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:26
virtual void ensure_valid(const char *context=NULL)=0
void set_solver_type(ESolverType st)
Definition: Machine.cpp:105
virtual CLabels * apply(CFeatures *data=NULL)
Definition: Machine.cpp:160

SHOGUN Machine Learning Toolbox - Documentation