SHOGUN  4.1.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
shogun_liblinear.h
浏览该文件的文档.
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 
43 namespace shogun
44 {
45 
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49 
51 struct liblinear_problem
52 {
54  int32_t l;
56  int32_t n;
58  float64_t* y;
60  CDotFeatures* x;
62  bool use_bias;
63 };
64 
66 struct liblinear_parameter
67 {
69  int32_t solver_type;
70 
71  /* these are for training only */
73  float64_t eps;
75  float64_t C;
77  int32_t nr_weight;
79  int32_t *weight_label;
81  float64_t* weight;
82 };
83 
85 struct liblinear_model
86 {
88  struct liblinear_parameter param;
90  int32_t nr_class;
92  int32_t nr_feature;
94  float64_t *w;
96  int32_t *label;
98  float64_t bias;
99 };
100 
101 void destroy_model(struct liblinear_model *model_);
102 void destroy_param(struct liblinear_parameter *param);
103 #ifdef __cplusplus
104 }
105 #endif
106 
108 class l2loss_svm_fun : public function
109 {
110 public:
117  l2loss_svm_fun(const liblinear_problem *prob, float64_t Cp, float64_t Cn);
118  ~l2loss_svm_fun();
119 
125  float64_t fun(float64_t *w);
126 
132  void grad(float64_t *w, float64_t *g);
133 
139  void Hv(float64_t *s, float64_t *Hs);
140 
145  int32_t get_nr_variable();
146 
147 private:
148  void Xv(float64_t *v, float64_t *Xv);
149  void subXv(float64_t *v, float64_t *Xv);
150  void subXTv(float64_t *v, float64_t *XTv);
151 
152  float64_t *C;
153  float64_t *z;
154  float64_t *D;
155  int32_t *I;
156  int32_t sizeI;
157  const liblinear_problem *prob;
158 };
159 
161 class l2r_lr_fun : public function
162 {
163 public:
170  l2r_lr_fun(const liblinear_problem *prob, float64_t* C);
171  ~l2r_lr_fun();
172 
178  float64_t fun(float64_t *w);
179 
185  void grad(float64_t *w, float64_t *g);
186 
192  void Hv(float64_t *s, float64_t *Hs);
193 
194  int32_t get_nr_variable();
195 
196 private:
197  void Xv(float64_t *v, float64_t *Xv);
198  void XTv(float64_t *v, float64_t *XTv);
199 
200  float64_t *C;
201  float64_t *z;
202  float64_t *D;
203  const liblinear_problem *m_prob;
204 };
205 
206 class l2r_l2_svc_fun : public function
207 {
208 public:
209  l2r_l2_svc_fun(const liblinear_problem *prob, float64_t* Cs);
210  ~l2r_l2_svc_fun();
211 
212  double fun(double *w);
213  void grad(double *w, double *g);
214  void Hv(double *s, double *Hs);
215 
216  int get_nr_variable();
217 
218 protected:
219  void Xv(double *v, double *Xv);
220  void subXv(double *v, double *Xv);
221  void subXTv(double *v, double *XTv);
222 
223  double *C;
224  double *z;
225  double *D;
226  int *I;
227  int sizeI;
228  const liblinear_problem *m_prob;
229 };
230 
231 class l2r_l2_svr_fun: public l2r_l2_svc_fun
232 {
233 public:
234  l2r_l2_svr_fun(const liblinear_problem *prob, double *Cs, double p);
235 
236  double fun(double *w);
237  void grad(double *w, double *g);
238 
239 private:
240  double m_p;
241 };
242 
243 
244 struct mcsvm_state
245 {
246  double* w;
247  double* B;
248  double* G;
249  double* alpha;
250  double* alpha_new;
251  int* index;
252  double* QD;
253  int* d_ind;
254  double* d_val;
255  int* alpha_index;
256  int* y_index;
257  int* active_size_i;
258  bool allocated,inited;
259 
260  mcsvm_state()
261  {
262  w = NULL;
263  B = NULL;
264  G = NULL;
265  alpha = NULL;
266  alpha_new = NULL;
267  index = NULL;
268  QD = NULL;
269  d_ind = NULL;
270  d_val = NULL;
271  alpha_index = NULL;
272  y_index = NULL;
273  active_size_i = NULL;
274  allocated = false;
275  inited = false;
276  }
277 
278  ~mcsvm_state()
279  {
280  SG_FREE(w);
281  SG_FREE(B);
282  SG_FREE(G);
283  SG_FREE(alpha);
284  SG_FREE(alpha_new);
285  SG_FREE(index);
286  SG_FREE(QD);
287  SG_FREE(d_ind);
288  SG_FREE(d_val);
289  SG_FREE(alpha_index);
290  SG_FREE(y_index);
291  SG_FREE(active_size_i);
292  }
293 };
294 
295 class Solver_MCSVM_CS
296 {
297  public:
298  Solver_MCSVM_CS(const liblinear_problem *prob, int nr_class, double *C,
299  double *w0, double eps, int max_iter,
300  double train_time, mcsvm_state* given_state);
301  ~Solver_MCSVM_CS();
302  void solve();
303  private:
304  void solve_sub_problem(double A_i, int yi, double C_yi, int active_i, double *alpha_new);
305  bool be_shrunk(int i, int m, int yi, double alpha_i, double minG);
306  double *C;
307  int w_size, l;
308  int nr_class;
309  int max_iter;
310  double eps;
311  double max_train_time;
312  double* w0;
313  const liblinear_problem *prob;
314  mcsvm_state* state;
315 };
316 
317 
318 }
319 #endif //_LIBLINEAR_H
320 
321 #endif // DOXYGEN_SHOULD_SKIP_THIS
double float64_t
Definition: common.h:50
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18

SHOGUN 机器学习工具包 - 项目文档