SHOGUN  3.2.1
 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 
42 #include <vector>
43 
44 namespace shogun
45 {
46 
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 
52 struct liblinear_problem
53 {
55  int32_t l;
57  int32_t n;
59  float64_t* y;
61  CDotFeatures* x;
63  bool use_bias;
64 };
65 
67 struct liblinear_parameter
68 {
70  int32_t solver_type;
71 
72  /* these are for training only */
74  float64_t eps;
76  float64_t C;
78  int32_t nr_weight;
80  int32_t *weight_label;
82  float64_t* weight;
83 };
84 
86 struct liblinear_model
87 {
89  struct liblinear_parameter param;
91  int32_t nr_class;
93  int32_t nr_feature;
95  float64_t *w;
97  int32_t *label;
99  float64_t bias;
100 };
101 
102 void destroy_model(struct liblinear_model *model_);
103 void destroy_param(struct liblinear_parameter *param);
104 #ifdef __cplusplus
105 }
106 #endif
107 
109 class l2loss_svm_fun : public function
110 {
111 public:
118  l2loss_svm_fun(const liblinear_problem *prob, float64_t Cp, float64_t Cn);
119  ~l2loss_svm_fun();
120 
126  float64_t fun(float64_t *w);
127 
133  void grad(float64_t *w, float64_t *g);
134 
140  void Hv(float64_t *s, float64_t *Hs);
141 
146  int32_t get_nr_variable();
147 
148 private:
149  void Xv(float64_t *v, float64_t *Xv);
150  void subXv(float64_t *v, float64_t *Xv);
151  void subXTv(float64_t *v, float64_t *XTv);
152 
153  float64_t *C;
154  float64_t *z;
155  float64_t *D;
156  int32_t *I;
157  int32_t sizeI;
158  const liblinear_problem *prob;
159 };
160 
162 class l2r_lr_fun : public function
163 {
164 public:
171  l2r_lr_fun(const liblinear_problem *prob, float64_t* C);
172  ~l2r_lr_fun();
173 
179  float64_t fun(float64_t *w);
180 
186  void grad(float64_t *w, float64_t *g);
187 
193  void Hv(float64_t *s, float64_t *Hs);
194 
195  int32_t get_nr_variable();
196 
197 private:
198  void Xv(float64_t *v, float64_t *Xv);
199  void XTv(float64_t *v, float64_t *XTv);
200 
201  float64_t *C;
202  float64_t *z;
203  float64_t *D;
204  const liblinear_problem *m_prob;
205 };
206 
207 class l2r_l2_svc_fun : public function
208 {
209 public:
210  l2r_l2_svc_fun(const liblinear_problem *prob, float64_t* Cs);
211  ~l2r_l2_svc_fun();
212 
213  double fun(double *w);
214  void grad(double *w, double *g);
215  void Hv(double *s, double *Hs);
216 
217  int get_nr_variable();
218 
219 protected:
220  void Xv(double *v, double *Xv);
221  void subXv(double *v, double *Xv);
222  void subXTv(double *v, double *XTv);
223 
224  double *C;
225  double *z;
226  double *D;
227  int *I;
228  int sizeI;
229  const liblinear_problem *m_prob;
230 };
231 
232 class l2r_l2_svr_fun: public l2r_l2_svc_fun
233 {
234 public:
235  l2r_l2_svr_fun(const liblinear_problem *prob, double *Cs, double p);
236 
237  double fun(double *w);
238  void grad(double *w, double *g);
239 
240 private:
241  double m_p;
242 };
243 
244 
245 struct mcsvm_state
246 {
247  double* w;
248  double* B;
249  double* G;
250  double* alpha;
251  double* alpha_new;
252  int* index;
253  double* QD;
254  int* d_ind;
255  double* d_val;
256  int* alpha_index;
257  int* y_index;
258  int* active_size_i;
259  bool allocated,inited;
260 
261  mcsvm_state()
262  {
263  w = NULL;
264  B = NULL;
265  G = NULL;
266  alpha = NULL;
267  alpha_new = NULL;
268  index = NULL;
269  QD = NULL;
270  d_ind = NULL;
271  d_val = NULL;
272  alpha_index = NULL;
273  y_index = NULL;
274  active_size_i = NULL;
275  allocated = false;
276  inited = false;
277  }
278 
279  ~mcsvm_state()
280  {
281  SG_FREE(w);
282  SG_FREE(B);
283  SG_FREE(G);
284  SG_FREE(alpha);
285  SG_FREE(alpha_new);
286  SG_FREE(index);
287  SG_FREE(QD);
288  SG_FREE(d_ind);
289  SG_FREE(d_val);
290  SG_FREE(alpha_index);
291  SG_FREE(y_index);
292  SG_FREE(active_size_i);
293  }
294 };
295 
296 class Solver_MCSVM_CS
297 {
298  public:
299  Solver_MCSVM_CS(const liblinear_problem *prob, int nr_class, double *C,
300  double *w0, double eps, int max_iter,
301  double train_time, mcsvm_state* given_state);
302  ~Solver_MCSVM_CS();
303  void solve();
304  private:
305  void solve_sub_problem(double A_i, int yi, double C_yi, int active_i, double *alpha_new);
306  bool be_shrunk(int i, int m, int yi, double alpha_i, double minG);
307  double *C;
308  int w_size, l;
309  int nr_class;
310  int max_iter;
311  double eps;
312  double max_train_time;
313  double* w0;
314  const liblinear_problem *prob;
315  mcsvm_state* state;
316 };
317 
318 
319 }
320 #endif //_LIBLINEAR_H
321 
322 #endif // DOXYGEN_SHOULD_SKIP_THIS

SHOGUN Machine Learning Toolbox - Documentation