SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
shogun_liblinear.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007-2009 The LIBLINEAR Project.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither name of copyright holders nor the names of its contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
25  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 #ifndef DOXYGEN_SHOULD_SKIP_THIS
34 
35 #ifndef _LIBLINEAR_H
36 #define _LIBLINEAR_H
37 
38 #include <shogun/lib/config.h>
39 
40 #ifdef HAVE_LAPACK
43 #include <vector>
44 
45 namespace shogun
46 {
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
53 struct problem
54 {
56  int32_t l;
58  int32_t n;
60  float64_t* y;
62  CDotFeatures* x;
64  bool use_bias;
65 };
66 
68 struct parameter
69 {
71  int32_t solver_type;
72 
73  /* these are for training only */
75  float64_t eps;
77  float64_t C;
79  int32_t nr_weight;
81  int32_t *weight_label;
83  float64_t* weight;
84 };
85 
87 struct model
88 {
90  struct parameter param;
92  int32_t nr_class;
94  int32_t nr_feature;
96  float64_t *w;
98  int32_t *label;
100  float64_t bias;
101 };
102 
103 void destroy_model(struct model *model_);
104 void destroy_param(struct parameter *param);
105 #ifdef __cplusplus
106 }
107 #endif
108 
110 class l2loss_svm_fun : public function
111 {
112 public:
119  l2loss_svm_fun(const problem *prob, float64_t Cp, float64_t Cn);
120  ~l2loss_svm_fun();
121 
127  float64_t fun(float64_t *w);
128 
134  void grad(float64_t *w, float64_t *g);
135 
141  void Hv(float64_t *s, float64_t *Hs);
142 
147  int32_t get_nr_variable();
148 
149 private:
150  void Xv(float64_t *v, float64_t *Xv);
151  void subXv(float64_t *v, float64_t *Xv);
152  void subXTv(float64_t *v, float64_t *XTv);
153 
154  float64_t *C;
155  float64_t *z;
156  float64_t *D;
157  int32_t *I;
158  int32_t sizeI;
159  const problem *prob;
160 };
161 
163 class l2r_lr_fun : public function
164 {
165 public:
172  l2r_lr_fun(const problem *prob, float64_t* C);
173  ~l2r_lr_fun();
174 
180  float64_t fun(float64_t *w);
181 
187  void grad(float64_t *w, float64_t *g);
188 
194  void Hv(float64_t *s, float64_t *Hs);
195 
196  int32_t get_nr_variable();
197 
198 private:
199  void Xv(float64_t *v, float64_t *Xv);
200  void XTv(float64_t *v, float64_t *XTv);
201 
202  float64_t *C;
203  float64_t *z;
204  float64_t *D;
205  const problem *m_prob;
206 };
207 
208 class l2r_l2_svc_fun : public function
209 {
210 public:
211  l2r_l2_svc_fun(const problem *prob, float64_t* Cs);
212  ~l2r_l2_svc_fun();
213 
214  double fun(double *w);
215  void grad(double *w, double *g);
216  void Hv(double *s, double *Hs);
217 
218  int get_nr_variable();
219 
220 protected:
221  void Xv(double *v, double *Xv);
222  void subXv(double *v, double *Xv);
223  void subXTv(double *v, double *XTv);
224 
225  double *C;
226  double *z;
227  double *D;
228  int *I;
229  int sizeI;
230  const problem *m_prob;
231 };
232 
233 class l2r_l2_svr_fun: public l2r_l2_svc_fun
234 {
235 public:
236  l2r_l2_svr_fun(const problem *prob, double *Cs, double p);
237 
238  double fun(double *w);
239  void grad(double *w, double *g);
240 
241 private:
242  double m_p;
243 };
244 
245 
246 struct mcsvm_state
247 {
248  double* w;
249  double* B;
250  double* G;
251  double* alpha;
252  double* alpha_new;
253  int* index;
254  double* QD;
255  int* d_ind;
256  double* d_val;
257  int* alpha_index;
258  int* y_index;
259  int* active_size_i;
260  bool allocated,inited;
261 
262  mcsvm_state()
263  {
264  w = NULL;
265  B = NULL;
266  G = NULL;
267  alpha = NULL;
268  alpha_new = NULL;
269  index = NULL;
270  QD = NULL;
271  d_ind = NULL;
272  d_val = NULL;
273  alpha_index = NULL;
274  y_index = NULL;
275  active_size_i = NULL;
276  allocated = false;
277  inited = false;
278  }
279 
280  ~mcsvm_state()
281  {
282  SG_FREE(w);
283  SG_FREE(B);
284  SG_FREE(G);
285  SG_FREE(alpha);
286  SG_FREE(alpha_new);
287  SG_FREE(index);
288  SG_FREE(QD);
289  SG_FREE(d_ind);
290  SG_FREE(d_val);
291  SG_FREE(alpha_index);
292  SG_FREE(y_index);
293  SG_FREE(active_size_i);
294  }
295 };
296 
297 class Solver_MCSVM_CS
298 {
299  public:
300  Solver_MCSVM_CS(const problem *prob, int nr_class, double *C,
301  double *w0, double eps, int max_iter,
302  double train_time, mcsvm_state* given_state);
303  ~Solver_MCSVM_CS();
304  void solve();
305  private:
306  void solve_sub_problem(double A_i, int yi, double C_yi, int active_i, double *alpha_new);
307  bool be_shrunk(int i, int m, int yi, double alpha_i, double minG);
308  double *C;
309  int w_size, l;
310  int nr_class;
311  int max_iter;
312  double eps;
313  double max_train_time;
314  double* w0;
315  const problem *prob;
316  mcsvm_state* state;
317 };
318 
319 
320 }
321 #endif //HAVE_LAPACK
322 #endif //_LIBLINEAR_H
323 
324 #endif // DOXYGEN_SHOULD_SKIP_THIS

SHOGUN Machine Learning Toolbox - Documentation