SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
KernelMachine.h
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  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
11 #ifndef _KERNEL_MACHINE_H__
12 #define _KERNEL_MACHINE_H__
13 
14 #include <shogun/lib/common.h>
15 #include <shogun/io/SGIO.h>
16 #include <shogun/kernel/Kernel.h>
17 #include <shogun/features/Labels.h>
18 #include <shogun/machine/Machine.h>
19 
20 #include <stdio.h>
21 
22 namespace shogun
23 {
24 class CMachine;
25 class CLabels;
26 class CKernel;
27 
43 class CKernelMachine : public CMachine
44 {
45  public:
48 
50  virtual ~CKernelMachine();
51 
57  virtual const char* get_name() const {
58  return "KernelMachine"; }
59 
64  inline void set_kernel(CKernel* k)
65  {
67  SG_REF(k);
68  kernel=k;
69  }
70 
75  inline CKernel* get_kernel()
76  {
77  SG_REF(kernel);
78  return kernel;
79  }
80 
85  inline void set_batch_computation_enabled(bool enable)
86  {
87  use_batch_computation=enable;
88  }
89 
95  {
96  return use_batch_computation;
97  }
98 
103  inline void set_linadd_enabled(bool enable)
104  {
105  use_linadd=enable;
106  }
107 
112  inline bool get_linadd_enabled()
113  {
114  return use_linadd ;
115  }
116 
121  inline void set_bias_enabled(bool enable_bias) { use_bias=enable_bias; }
122 
127  inline bool get_bias_enabled() { return use_bias; }
128 
134  {
135  return m_bias;
136  }
137 
142  inline void set_bias(float64_t bias)
143  {
144  m_bias=bias;
145  }
146 
152  inline int32_t get_support_vector(int32_t idx)
153  {
154  ASSERT(m_svs.vector && idx<m_svs.vlen);
155  return m_svs.vector[idx];
156  }
157 
163  inline float64_t get_alpha(int32_t idx)
164  {
165  if (!m_alpha.vector)
166  SG_ERROR("No alphas set\n");
167  if (idx>=m_alpha.vlen)
168  SG_ERROR("Alphas index (%d) out of range (%d)\n", idx, m_svs.vlen);
169  return m_alpha.vector[idx];
170  }
171 
178  inline bool set_support_vector(int32_t idx, int32_t val)
179  {
180  if (m_svs.vector && idx<m_svs.vlen)
181  m_svs.vector[idx]=val;
182  else
183  return false;
184 
185  return true;
186  }
187 
194  inline bool set_alpha(int32_t idx, float64_t val)
195  {
196  if (m_alpha.vector && idx<m_alpha.vlen)
197  m_alpha.vector[idx]=val;
198  else
199  return false;
200 
201  return true;
202  }
203 
208  inline int32_t get_num_support_vectors()
209  {
210  return m_svs.vlen;
211  }
212 
218  {
219  m_alpha = alphas;
220  }
221 
227  {
228  m_svs = svs;
229  }
230 
235  {
236  int32_t nsv = get_num_support_vectors();
237  int32_t* svs = NULL;
238 
239  if (nsv>0)
240  {
241  svs = SG_MALLOC(int32_t, nsv);
242  for(int32_t i=0; i<nsv; i++)
243  svs[i] = get_support_vector(i);
244  }
245 
246  return SGVector<int32_t>(svs,nsv);
247  }
248 
253  {
254  int32_t nsv = get_num_support_vectors();
255  float64_t* alphas = NULL;
256 
257  if (nsv>0)
258  {
259  alphas = SG_MALLOC(float64_t, nsv);
260  for(int32_t i=0; i<nsv; i++)
261  alphas[i] = get_alpha(i);
262  }
263 
264  return SGVector<float64_t>(alphas,nsv);
265  }
266 
271  inline bool create_new_model(int32_t num)
272  {
275 
276  m_bias=0;
277 
278  if (num>0)
279  {
281  m_svs= SGVector<int32_t>(num);
282  return (m_alpha.vector!=NULL && m_svs.vector!=NULL);
283  }
284  else
285  return true;
286  }
287 
293 
298  virtual CLabels* apply();
299 
305  virtual CLabels* apply(CFeatures* data);
306 
312  virtual float64_t apply(int32_t num);
313 
319  static void* apply_helper(void* p);
320 
321  protected:
325  virtual void store_model_features();
326 
327  protected:
335  bool use_bias;
338 
341 
344 };
345 }
346 #endif /* _KERNEL_MACHINE_H__ */

SHOGUN Machine Learning Toolbox - Documentation