SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 }

SHOGUN Machine Learning Toolbox - Documentation