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

SHOGUN Machine Learning Toolbox - Documentation