SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
WDSVMOcas.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) 2007-2008 Vojtech Franc
8  * Written (W) 2007-2009 Soeren Sonnenburg
9  * Copyright (C) 2007-2009 Fraunhofer Institute FIRST and Max-Planck-Society
10  */
11 
12 #ifndef _WDSVMOCAS_H___
13 #define _WDSVMOCAS_H___
14 
15 #include <shogun/lib/common.h>
16 #include <shogun/machine/Machine.h>
19 #include <shogun/labels/Labels.h>
20 
21 namespace shogun
22 {
23 template <class ST> class CStringFeatures;
24 
26 class CWDSVMOcas : public CMachine
27 {
28  public:
31 
33  CWDSVMOcas();
34 
39  CWDSVMOcas(E_SVM_TYPE type);
40 
49  CWDSVMOcas(
50  float64_t C, int32_t d, int32_t from_d,
51  CStringFeatures<uint8_t>* traindat, CLabels* trainlab);
52  virtual ~CWDSVMOcas();
53 
58  virtual inline EMachineType get_classifier_type() { return CT_WDSVMOCAS; }
59 
66  inline void set_C(float64_t c_neg, float64_t c_pos) { C1=c_neg; C2=c_pos; }
67 
72  inline float64_t get_C1() { return C1; }
73 
78  inline float64_t get_C2() { return C2; }
79 
84  inline void set_epsilon(float64_t eps) { epsilon=eps; }
85 
90  inline float64_t get_epsilon() { return epsilon; }
91 
97  {
99  SG_REF(feat);
100  features=feat;
101  }
102 
108  {
109  SG_REF(features);
110  return features;
111  }
112 
117  inline void set_bias_enabled(bool enable_bias) { use_bias=enable_bias; }
118 
123  inline bool get_bias_enabled() { return use_bias; }
124 
129  inline void set_bufsize(int32_t sz) { bufsize=sz; }
130 
135  inline int32_t get_bufsize() { return bufsize; }
136 
142  inline void set_degree(int32_t d, int32_t from_d)
143  {
144  degree=d;
145  from_degree=from_d;
146  }
147 
152  inline int32_t get_degree() { return degree; }
153 
160  virtual CBinaryLabels* apply_binary(CFeatures* data=NULL);
161 
168  virtual CRegressionLabels* apply_regression(CFeatures* data=NULL);
169 
175  inline virtual float64_t apply_one(int32_t num)
176  {
177  ASSERT(features);
178  if (!wd_weights)
179  set_wd_weights();
180 
181  int32_t len=0;
182  float64_t sum=0;
183  bool free_vec;
184  uint8_t* vec=features->get_feature_vector(num, len, free_vec);
185  //SG_INFO("len %d, string_length %d\n", len, string_length);
186  ASSERT(len==string_length);
187 
188  for (int32_t j=0; j<string_length; j++)
189  {
190  int32_t offs=w_dim_single_char*j;
191  int32_t val=0;
192  for (int32_t k=0; (j+k<string_length) && (k<degree); k++)
193  {
194  val=val*alphabet_size + vec[j+k];
195  sum+=wd_weights[k] * w[offs+val];
196  offs+=w_offsets[k];
197  }
198  }
199  features->free_feature_vector(vec, num, free_vec);
200  return sum/normalization_const;
201  }
202 
205  {
206  ASSERT(features);
208  for (int32_t i=0; i<degree; i++)
210 
212  SG_DEBUG("normalization_const:%f\n", normalization_const);
213  }
214 
220 
221 
222  protected:
223 
229 
234  int32_t set_wd_weights();
235 
244  static void compute_W(
245  float64_t *sq_norm_W, float64_t *dp_WoldW, float64_t *alpha,
246  uint32_t nSel, void* ptr );
247 
254  static float64_t update_W(float64_t t, void* ptr );
255 
261  static void* add_new_cut_helper(void* ptr);
262 
271  static int add_new_cut(
272  float64_t *new_col_H, uint32_t *new_cut, uint32_t cut_length,
273  uint32_t nSel, void* ptr );
274 
280  static void* compute_output_helper(void* ptr);
281 
287  static int compute_output( float64_t *output, void* ptr );
288 
295  static int sort( float64_t* vals, float64_t* data, uint32_t size);
296 
298  static inline void print(ocas_return_value_T value)
299  {
300  return;
301  }
302 
303 
305  inline virtual const char* get_name() const { return "WDSVMOcas"; }
306 
307  protected:
316  virtual bool train_machine(CFeatures* data=NULL);
317 
318  protected:
322  bool use_bias;
324  int32_t bufsize;
332  E_SVM_TYPE method;
333 
335  int32_t degree;
337  int32_t from_degree;
341  int32_t num_vec;
343  int32_t string_length;
345  int32_t alphabet_size;
346 
349 
355  int32_t* w_offsets;
357  int32_t w_dim;
366 
371 };
372 }
373 #endif

SHOGUN Machine Learning Toolbox - Documentation