SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SVM_linear.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  int32_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 Cp, float64_t Cn);
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 *prob;
206 };
207 
208 class l2r_l2_svc_fun : public function
209 {
210 public:
211  l2r_l2_svc_fun(const problem *prob, double Cp, double Cn);
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 private:
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 *prob;
231 };
232 
233 class Solver_MCSVM_CS
234 {
235  public:
236  Solver_MCSVM_CS(const problem *prob, int nr_class, double *C, double eps=0.1, int max_iter=100000);
237  ~Solver_MCSVM_CS();
238  void Solve(double *w);
239  private:
240  void solve_sub_problem(double A_i, int yi, double C_yi, int active_i, double *alpha_new);
241  bool be_shrunk(int i, int m, int yi, double alpha_i, double minG);
242  double *B, *C, *G;
243  int w_size, l;
244  int nr_class;
245  int max_iter;
246  double eps;
247  const problem *prob;
248 };
249 
250 
251 }
252 #endif //HAVE_LAPACK
253 #endif //_LIBLINEAR_H
254 
255 #endif // DOXYGEN_SHOULD_SKIP_THIS

SHOGUN Machine Learning Toolbox - Documentation