summaryrefslogtreecommitdiffstats
path: root/ml/dlib/dlib/svm/pegasos_abstract.h
blob: 008b1cb9453229977fc753c29d5d7eb241cd93f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
// Copyright (C) 2009  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.
#undef DLIB_PEGASoS_ABSTRACT_
#ifdef DLIB_PEGASoS_ABSTRACT_

#include <cmath>
#include "../algs.h"
#include "function_abstract.h"
#include "kernel_abstract.h"

namespace dlib
{

// ----------------------------------------------------------------------------------------

    template <
        typename kern_type
        >
    class svm_pegasos
    {
        /*!
            REQUIREMENTS ON kern_type
                is a kernel function object as defined in dlib/svm/kernel_abstract.h 

            WHAT THIS OBJECT REPRESENTS
                This object implements an online algorithm for training a support 
                vector machine for solving binary classification problems.  

                The implementation of the Pegasos algorithm used by this object is based
                on the following excellent paper:
                    Pegasos: Primal estimated sub-gradient solver for SVM (2007)
                    by Shai Shalev-Shwartz, Yoram Singer, Nathan Srebro 
                    In ICML 

                This SVM training algorithm has two interesting properties.  First, the 
                pegasos algorithm itself converges to the solution in an amount of time
                unrelated to the size of the training set (in addition to being quite fast
                to begin with).  This makes it an appropriate algorithm for learning from
                very large datasets.  Second, this object uses the dlib::kcentroid object 
                to maintain a sparse approximation of the learned decision function.  
                This means that the number of support vectors in the resulting decision 
                function is also unrelated to the size of the dataset (in normal SVM
                training algorithms, the number of support vectors grows approximately 
                linearly with the size of the training set).  
        !*/

    public:
        typedef kern_type kernel_type;
        typedef typename kernel_type::scalar_type scalar_type;
        typedef typename kernel_type::sample_type sample_type;
        typedef typename kernel_type::mem_manager_type mem_manager_type;
        typedef decision_function<kernel_type> trained_function_type;

        template <typename K_>
        struct rebind {
            typedef svm_pegasos<K_> other;
        };

        svm_pegasos (
        );
        /*!
            ensures
                - this object is properly initialized 
                - #get_lambda_class1() == 0.0001
                - #get_lambda_class2() == 0.0001
                - #get_tolerance() == 0.01
                - #get_train_count() == 0
                - #get_max_num_sv() == 40
        !*/

        svm_pegasos (
            const kernel_type& kernel_, 
            const scalar_type& lambda_,
            const scalar_type& tolerance_,
            unsigned long max_num_sv
        );
        /*!
            requires
                - lambda_ > 0
                - tolerance_ > 0
                - max_num_sv > 0
            ensures
                - this object is properly initialized 
                - #get_lambda_class1() == lambda_ 
                - #get_lambda_class2() == lambda_ 
                - #get_tolerance() == tolerance_
                - #get_kernel() == kernel_
                - #get_train_count() == 0
                - #get_max_num_sv() == max_num_sv
        !*/

        void clear (
        );
        /*!
            ensures
                - #get_train_count() == 0
                - clears out any memory of previous calls to train()
                - doesn't change any of the algorithm parameters.  I.e.
                    - #get_lambda_class1()  == get_lambda_class1()
                    - #get_lambda_class2()  == get_lambda_class2()
                    - #get_tolerance()      == get_tolerance()
                    - #get_kernel()         == get_kernel()
                    - #get_max_num_sv()     == get_max_num_sv()
        !*/

        const scalar_type get_lambda_class1 (
        ) const;
        /*!
            ensures
                - returns the SVM regularization term for the +1 class.  It is the 
                  parameter that determines the trade off between trying to fit the 
                  +1 training data exactly or allowing more errors but hopefully 
                  improving the generalization ability of the resulting classifier.  
                  Smaller values encourage exact fitting while larger values may 
                  encourage better generalization. It is also worth noting that the 
                  number of iterations it takes for this algorithm to converge is 
                  proportional to 1/lambda.  So smaller values of this term cause 
                  the running time of this algorithm to increase.  For more 
                  information you should consult the paper referenced above.
        !*/

        const scalar_type get_lambda_class2 (
        ) const;
        /*!
            ensures
                - returns the SVM regularization term for the -1 class.  It has
                  the same properties as the get_lambda_class1() parameter except that
                  it applies to the -1 class.
        !*/

        const scalar_type get_tolerance (
        ) const;
        /*!
            ensures
                - returns the tolerance used by the internal kcentroid object to 
                  represent the learned decision function.  Smaller values of this 
                  tolerance will result in a more accurate representation of the 
                  decision function but will use more support vectors (up to
                  a max of get_max_num_sv()).  
        !*/

        unsigned long get_max_num_sv (
        ) const;
        /*!
            ensures
                - returns the maximum number of support vectors this object is
                  allowed to use.
        !*/

        const kernel_type get_kernel (
        ) const;
        /*!
            ensures
                - returns the kernel used by this object
        !*/

        void set_kernel (
            kernel_type k
        );
        /*!
            ensures
                - #get_kernel() == k
                - #get_train_count() == 0
                  (i.e. clears any memory of previous training)
        !*/

        void set_tolerance (
            double tol
        );
        /*!
            requires
                - tol > 0
            ensures
                - #get_tolerance() == tol
                - #get_train_count() == 0
                  (i.e. clears any memory of previous training)
        !*/

        void set_max_num_sv (
            unsigned long max_num_sv
        );
        /*!
            requires
                - max_num_sv > 0
            ensures
                - #get_max_num_sv() == max_num_sv 
                - #get_train_count() == 0
                  (i.e. clears any memory of previous training)
        !*/

        void set_lambda (
            scalar_type lambda_
        );
        /*!
            requires
                - lambda_ > 0
            ensures
                - #get_lambda_class1() == lambda_
                - #get_lambda_class2() == lambda_
                - #get_train_count() == 0
                  (i.e. clears any memory of previous training)
        !*/

        void set_lambda_class1 (
            scalar_type lambda_
        );
        /*!
            requires
                - lambda_ > 0
            ensures
                - #get_lambda_class1() == lambda_ 
                  #get_train_count() == 0
                  (i.e. clears any memory of previous training)
        !*/

        void set_lambda_class2 (
            scalar_type lambda_
        );
        /*!
            requires
                - lambda_ > 0
            ensures
                - #get_lambda_class2() == lambda_ 
                  #get_train_count() == 0
                  (i.e. clears any memory of previous training)
        !*/

        unsigned long get_train_count (
        ) const;
        /*!
            ensures
                - returns how many times this->train() has been called
                  since this object was constructed or last cleared.  
        !*/

        scalar_type train (
            const sample_type& x,
            const scalar_type& y
        );
        /*!
            requires
                - y == 1 || y == -1
            ensures
                - trains this svm using the given sample x and label y
                - #get_train_count() == get_train_count() + 1
                - returns the current learning rate
                  (i.e. 1/(get_train_count()*min(get_lambda_class1(),get_lambda_class2())) )
        !*/

        scalar_type operator() (
            const sample_type& x
        ) const;
        /*!
            ensures
                - classifies the given x sample using the decision function
                  this object has learned so far.  
                - if (x is a sample predicted have +1 label) then
                    - returns a number >= 0 
                - else
                    - returns a number < 0
        !*/

        const decision_function<kernel_type> get_decision_function (
        ) const;
        /*!
            ensures
                - returns a decision function F that represents the function learned 
                  by this object so far.  I.e. it is the case that:
                    - for all x: F(x) == (*this)(x)
        !*/

        void swap (
            svm_pegasos& item
        );
        /*!
            ensures
                - swaps *this and item
        !*/

    }; 

// ----------------------------------------------------------------------------------------

    template <
        typename kern_type 
        >
    void swap(
        svm_pegasos<kern_type>& a, 
        svm_pegasos<kern_type>& b
    ) { a.swap(b); }
    /*!
        provides a global swap function
    !*/

    template <
        typename kern_type
        >
    void serialize (
        const svm_pegasos<kern_type>& item,
        std::ostream& out
    );
    /*!
        provides serialization support for svm_pegasos objects
    !*/

    template <
        typename kern_type 
        >
    void deserialize (
        svm_pegasos<kern_type>& item,
        std::istream& in 
    );
    /*!
        provides serialization support for svm_pegasos objects
    !*/

// ----------------------------------------------------------------------------------------

    template <
        typename T,
        typename U
        >
    void replicate_settings (
        const svm_pegasos<T>& source,
        svm_pegasos<U>& dest
    );
    /*!
        ensures
            - copies all the parameters from the source trainer to the dest trainer.
            - #dest.get_tolerance() == source.get_tolerance()
            - #dest.get_lambda_class1() == source.get_lambda_class1()
            - #dest.get_lambda_class2() == source.get_lambda_class2()
            - #dest.get_max_num_sv() == source.get_max_num_sv()
    !*/

// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------

    template <
        typename trainer_type
        >
    class batch_trainer 
    {
        /*!
            REQUIREMENTS ON trainer_type
                - trainer_type == some kind of online trainer object (e.g. svm_pegasos)
                  replicate_settings() must also be defined for the type.

            WHAT THIS OBJECT REPRESENTS
                This is a trainer object that is meant to wrap online trainer objects 
                that create decision_functions. It turns an online learning algorithm 
                such as svm_pegasos into a batch learning object.  This allows you to 
                use objects like svm_pegasos with functions (e.g. cross_validate_trainer) 
                that expect batch mode training objects.
        !*/

    public:
        typedef typename trainer_type::kernel_type kernel_type;
        typedef typename trainer_type::scalar_type scalar_type;
        typedef typename trainer_type::sample_type sample_type;
        typedef typename trainer_type::mem_manager_type mem_manager_type;
        typedef typename trainer_type::trained_function_type trained_function_type;


        batch_trainer (
        );
        /*!
            ensures
                - This object is in an uninitialized state.  You must
                  construct a real one with the other constructor and assign it
                  to this instance before you use this object.
        !*/

        batch_trainer (
            const trainer_type& online_trainer, 
            const scalar_type min_learning_rate_,
            bool verbose_,
            bool use_cache_,
            long cache_size_ = 100
        );
        /*!
            requires
                - min_learning_rate_ > 0
                - cache_size_ > 0
            ensures
                - returns a batch trainer object that uses the given online_trainer object
                  to train a decision function.
                - #get_min_learning_rate() == min_learning_rate_
                - if (verbose_ == true) then
                    - this object will output status messages to standard out while
                      training is under way.
                - if (use_cache_ == true) then
                    - this object will cache up to cache_size_ columns of the kernel 
                      matrix during the training process.
        !*/

        const scalar_type get_min_learning_rate (
        ) const;
        /*!
            ensures
                - returns the min learning rate that the online trainer must reach
                  before this object considers training to be complete.
        !*/

        template <
            typename in_sample_vector_type,
            typename in_scalar_vector_type
            >
        const decision_function<kernel_type> train (
            const in_sample_vector_type& x,
            const in_scalar_vector_type& y
        ) const;
        /*!
            ensures
                - trains and returns a decision_function using the trainer that was 
                  supplied to this object's constructor.
                - training continues until the online training object indicates that
                  its learning rate has dropped below get_min_learning_rate().
            throws
                - std::bad_alloc
                - any exceptions thrown by the trainer_type object
        !*/

    }; 

// ----------------------------------------------------------------------------------------

    template <
        typename trainer_type
        >
    const batch_trainer<trainer_type> batch (
        const trainer_type& trainer,
        const typename trainer_type::scalar_type min_learning_rate = 0.1
    ) { return batch_trainer<trainer_type>(trainer, min_learning_rate, false, false); }
    /*!
        requires
            - min_learning_rate > 0
            - trainer_type == some kind of online trainer object that creates decision_function
              objects (e.g. svm_pegasos).  replicate_settings() must also be defined for the type.
        ensures
            - returns a batch_trainer object that has been instantiated with the 
              given arguments.
    !*/

// ----------------------------------------------------------------------------------------

    template <
        typename trainer_type
        >
    const batch_trainer<trainer_type> verbose_batch (
        const trainer_type& trainer,
        const typename trainer_type::scalar_type min_learning_rate = 0.1
    ) { return batch_trainer<trainer_type>(trainer, min_learning_rate, true, false); }
    /*!
        requires
            - min_learning_rate > 0
            - trainer_type == some kind of online trainer object that creates decision_function
              objects (e.g. svm_pegasos).  replicate_settings() must also be defined for the type.
        ensures
            - returns a batch_trainer object that has been instantiated with the 
              given arguments (and is verbose).
    !*/

// ----------------------------------------------------------------------------------------

    template <
        typename trainer_type
        >
    const batch_trainer<trainer_type> batch_cached (
        const trainer_type& trainer,
        const typename trainer_type::scalar_type min_learning_rate = 0.1,
        long cache_size = 100
    ) { return batch_trainer<trainer_type>(trainer, min_learning_rate, false, true, cache_size); }
    /*!
        requires
            - min_learning_rate > 0
            - cache_size > 0
            - trainer_type == some kind of online trainer object that creates decision_function
              objects (e.g. svm_pegasos).  replicate_settings() must also be defined for the type.
        ensures
            - returns a batch_trainer object that has been instantiated with the 
              given arguments (uses a kernel cache).
    !*/

// ----------------------------------------------------------------------------------------

    template <
        typename trainer_type
        >
    const batch_trainer<trainer_type> verbose_batch_cached (
        const trainer_type& trainer,
        const typename trainer_type::scalar_type min_learning_rate = 0.1,
        long cache_size = 100
    ) { return batch_trainer<trainer_type>(trainer, min_learning_rate, true, true, cache_size); }
    /*!
        requires
            - min_learning_rate > 0
            - cache_size > 0
            - trainer_type == some kind of online trainer object that creates decision_function
              objects (e.g. svm_pegasos).  replicate_settings() must also be defined for the type.
        ensures
            - returns a batch_trainer object that has been instantiated with the 
              given arguments (is verbose and uses a kernel cache).
    !*/

// ----------------------------------------------------------------------------------------


}

#endif // DLIB_PEGASoS_ABSTRACT_