SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ssl.h
Go to the documentation of this file.
1 /* Copyright 2006 Vikas Sindhwani (vikass@cs.uchicago.edu)
2  SVM-lin: Fast SVM Solvers for Supervised and Semi-supervised Learning
3 
4  This file is part of SVM-lin.
5 
6  SVM-lin is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  SVM-lin is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with SVM-lin (see gpl.txt); if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 #ifndef DOXYGEN_SHOULD_SKIP_THIS
21 
22 #ifndef _SSL_H
23 #define _SSL_H
24 
25 /* OPTIMIZATION CONSTANTS */
26 #define CGITERMAX 10000 /* maximum number of CGLS iterations */
27 #define SMALL_CGITERMAX 10 /* for heuristic 1 in reference [2] */
28 #define EPSILON 1e-6 /* most tolerances are set to this value */
29 #define BIG_EPSILON 0.01 /* for heuristic 2 in reference [2] */
30 #define RELATIVE_STOP_EPS 1e-9 /* for L2-SVM-MFN relative stopping criterion */
31 #define MFNITERMAX 50 /* maximum number of MFN iterations */
32 #define TSVM_ANNEALING_RATE 1.5 /* rate at which lambda_u is increased in TSVM */
33 #define TSVM_LAMBDA_SMALL 1e-5 /* lambda_u starts from this value */
34 #define DA_ANNEALING_RATE 1.5 /* annealing rate for DA */
35 #define DA_INIT_TEMP 10 /* initial temperature relative to lambda_u */
36 #define DA_INNER_ITERMAX 100 /* maximum fixed temperature iterations for DA */
37 #define DA_OUTER_ITERMAX 30 /* maximum number of outer loops for DA */
38 
39 #include <shogun/lib/common.h>
41 
42 namespace shogun
43 {
45 struct data
46 {
48  int32_t m;
50  int32_t l;
52  int32_t u;
54  int32_t n;
56  int32_t nz;
57 
59  shogun::CDotFeatures* features;
61  float64_t *Y;
63  float64_t *C;
64 };
65 
67 struct vector_double
68 {
70  int32_t d;
72  float64_t *vec;
73 };
74 
76 struct vector_int
77 {
79  int32_t d;
81  int32_t *vec;
82 };
83 
84 enum { RLS, SVM, TSVM, DA_SVM }; /* currently implemented algorithms */
85 
87 struct options
88 {
89  /* user options */
91  int32_t algo;
93  float64_t lambda;
95  float64_t lambda_u;
97  int32_t S;
99  float64_t R;
101  float64_t Cp;
103  float64_t Cn;
104 
105  /* internal optimization options */
109  int32_t cgitermax;
111  int32_t mfnitermax;
112 
114  float64_t bias;
115 };
116 
118 class Delta {
119  public:
121  Delta() { delta=0.0; index=0;s=0; }
122 
126  int32_t index;
128  int32_t s;
129 };
130 
131 inline bool operator<(const Delta& a , const Delta& b)
132 {
133  return (a.delta < b.delta);
134 }
135 
136 void initialize(struct vector_double *A, int32_t k, float64_t a);
137 /* initializes a vector_double to be of length k, all elements set to a */
138 void initialize(struct vector_int *A, int32_t k);
139 /* initializes a vector_int to be of length k, elements set to 1,2..k. */
140 void GetLabeledData(struct data *Data_Labeled, const struct data *Data);
141 /* extracts labeled data from Data and copies it into Data_Labeled */
142 float64_t norm_square(const vector_double *A); /* returns squared length of A */
143 
144 /* ssl_train: takes data, options, uninitialized weight and output
145  vector_doubles, routes it to the algorithm */
146 /* the learnt weight vector and the outputs it gives on the data matrix are saved */
147 void ssl_train(
148  struct data *Data,
149  struct options *Options,
150  struct vector_double *W, /* weight vector */
151  struct vector_double *O); /* output vector */
152 
153 /* svmlin algorithms and their subroutines */
154 
155 /* Conjugate Gradient for Sparse Linear Least Squares Problems */
156 /* Solves: min_w 0.5*Options->lamda*w'*w + 0.5*sum_{i in Subset} Data->C[i] (Y[i]- w' x_i)^2 */
157 /* over a subset of examples x_i specified by vector_int Subset */
158 int32_t CGLS(
159  const struct data *Data,
160  const struct options *Options,
161  const struct vector_int *Subset,
162  struct vector_double *Weights,
163  struct vector_double *Outputs);
164 
165 /* Linear Modified Finite Newton L2-SVM*/
166 /* Solves: min_w 0.5*Options->lamda*w'*w + 0.5*sum_i Data->C[i] max(0,1 - Y[i] w' x_i)^2 */
167 int32_t L2_SVM_MFN(
168  const struct data *Data,
169  struct options *Options,
170  struct vector_double *Weights,
171  struct vector_double *Outputs,
172  int32_t ini); /* use ini=0 if no good starting guess for Weights, else 1 */
173 
175  float64_t *w,
176  float64_t *w_bar,
177  float64_t lambda,
178  float64_t *o,
179  float64_t *o_bar,
180  float64_t *Y,
181  float64_t *C,
182  int32_t d,
183  int32_t l);
184 
185 /* Transductive L2-SVM */
186 /* Solves : min_(w, Y[i],i in UNlabeled) 0.5*Options->lamda*w'*w + 0.5*(1/Data->l)*sum_{i in labeled} max(0,1 - Y[i] w' x_i)^2 + 0.5*(Options->lambda_u/Data->u)*sum_{i in UNlabeled} max(0,1 - Y[i] w' x_i)^2
187  subject to: (1/Data->u)*sum_{i in UNlabeled} max(0,Y[i]) = Options->R */
188 int32_t TSVM_MFN(
189  const struct data *Data,
190  struct options *Options,
191  struct vector_double *Weights,
192  struct vector_double *Outputs);
193 
194 int32_t switch_labels(
195  float64_t* Y,
196  float64_t* o,
197  int32_t* JU,
198  int32_t u,
199  int32_t S);
200 
201 /* Deterministic Annealing*/
202 int32_t DA_S3VM(
203  struct data *Data,
204  struct options *Options,
205  struct vector_double *Weights,
206  struct vector_double *Outputs);
207 
208 void optimize_p(
209  const float64_t* g, int32_t u, float64_t T, float64_t r, float64_t*p);
210 
211 int32_t optimize_w(
212  const struct data *Data,
213  const float64_t *p,
214  struct options *Options,
215  struct vector_double *Weights,
216  struct vector_double *Outputs,
217  int32_t ini);
218 
220  float64_t normWeights,
221  float64_t *Y,
222  float64_t *Outputs,
223  int32_t m,
224  float64_t lambda,
225  float64_t lambda_u);
226 
227 float64_t entropy(const float64_t *p, int32_t u);
228 
229 /* KL-divergence */
230 float64_t KL(const float64_t *p, const float64_t *q, int32_t u);
231 }
232 #endif // _SSL_H
233 
234 #endif // DOXYGEN_SHOULD_SKIP_THIS

SHOGUN Machine Learning Toolbox - Documentation