SHOGUN  3.2.1
 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/config.h>
16 
17 #include <shogun/lib/common.h>
18 #include <shogun/machine/Machine.h>
21 #include <shogun/labels/Labels.h>
22 
23 namespace shogun
24 {
25 template <class ST> class CStringFeatures;
26 
28 class CWDSVMOcas : public CMachine
29 {
30  public:
33 
35  CWDSVMOcas();
36 
41  CWDSVMOcas(E_SVM_TYPE type);
42 
51  CWDSVMOcas(
52  float64_t C, int32_t d, int32_t from_d,
53  CStringFeatures<uint8_t>* traindat, CLabels* trainlab);
54  virtual ~CWDSVMOcas();
55 
61 
68  inline void set_C(float64_t c_neg, float64_t c_pos) { C1=c_neg; C2=c_pos; }
69 
74  inline float64_t get_C1() { return C1; }
75 
80  inline float64_t get_C2() { return C2; }
81 
86  inline void set_epsilon(float64_t eps) { epsilon=eps; }
87 
92  inline float64_t get_epsilon() { return epsilon; }
93 
99  {
100  SG_REF(feat);
102  features=feat;
103  }
104 
110  {
111  SG_REF(features);
112  return features;
113  }
114 
119  inline void set_bias_enabled(bool enable_bias) { use_bias=enable_bias; }
120 
125  inline bool get_bias_enabled() { return use_bias; }
126 
131  inline void set_bufsize(int32_t sz) { bufsize=sz; }
132 
137  inline int32_t get_bufsize() { return bufsize; }
138 
144  inline void set_degree(int32_t d, int32_t from_d)
145  {
146  degree=d;
147  from_degree=from_d;
148  }
149 
154  inline int32_t get_degree() { return degree; }
155 
162  virtual CBinaryLabels* apply_binary(CFeatures* data=NULL);
163 
170  virtual CRegressionLabels* apply_regression(CFeatures* data=NULL);
171 
177  virtual float64_t apply_one(int32_t num)
178  {
180  if (!wd_weights)
181  set_wd_weights();
182 
183  int32_t len=0;
184  float64_t sum=0;
185  bool free_vec;
186  uint8_t* vec=features->get_feature_vector(num, len, free_vec);
187  //SG_INFO("len %d, string_length %d\n", len, string_length)
188  ASSERT(len==string_length)
189 
190  for (int32_t j=0; j<string_length; j++)
191  {
192  int32_t offs=w_dim_single_char*j;
193  int32_t val=0;
194  for (int32_t k=0; (j+k<string_length) && (k<degree); k++)
195  {
196  val=val*alphabet_size + vec[j+k];
197  sum+=wd_weights[k] * w[offs+val];
198  offs+=w_offsets[k];
199  }
200  }
201  features->free_feature_vector(vec, num, free_vec);
202  return sum/normalization_const;
203  }
204 
207  {
210  for (int32_t i=0; i<degree; i++)
212 
214  SG_DEBUG("normalization_const:%f\n", normalization_const)
215  }
216 
222 
223 
224  protected:
225 
231 
236  int32_t set_wd_weights();
237 
246  static void compute_W(
247  float64_t *sq_norm_W, float64_t *dp_WoldW, float64_t *alpha,
248  uint32_t nSel, void* ptr );
249 
256  static float64_t update_W(float64_t t, void* ptr );
257 
263  static void* add_new_cut_helper(void* ptr);
264 
273  static int add_new_cut(
274  float64_t *new_col_H, uint32_t *new_cut, uint32_t cut_length,
275  uint32_t nSel, void* ptr );
276 
282  static void* compute_output_helper(void* ptr);
283 
289  static int compute_output( float64_t *output, void* ptr );
290 
297  static int sort( float64_t* vals, float64_t* data, uint32_t size);
298 
300  static inline void print(ocas_return_value_T value)
301  {
302  return;
303  }
304 
305 
307  virtual const char* get_name() const { return "WDSVMOcas"; }
308 
309  protected:
318  virtual bool train_machine(CFeatures* data=NULL);
319 
320  protected:
324  bool use_bias;
326  int32_t bufsize;
334  E_SVM_TYPE method;
335 
337  int32_t degree;
339  int32_t from_degree;
343  int32_t num_vec;
345  int32_t string_length;
347  int32_t alphabet_size;
348 
351 
357  int32_t* w_offsets;
359  int32_t w_dim;
368 
373 };
374 }
375 #endif

SHOGUN Machine Learning Toolbox - Documentation