File: Kernel.h

package info (click to toggle)
torch 2-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 5,488 kB
  • ctags: 3,217
  • sloc: cpp: 14,272; makefile: 201
file content (164 lines) | stat: -rw-r--r-- 3,995 bytes parent folder | download
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
// Copyright (C) 2002 Ronan Collobert (collober@iro.umontreal.ca)
//                
//
// This file is part of Torch. Release II.
// [The Ultimate Machine Learning Library]
//
// Torch is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// Torch is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Torch; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#ifndef KERNEL_INC
#define KERNEL_INC

#include "general.h"
#include "DataSet.h"
#include "EuclideanDataSet.h"

namespace Torch {

/** Kernel class.
    Note that all kernels are based on the #dotProduct#
    of their associated #DataSet#.

    @author Ronan Collobert (collober@iro.umontreal.ca)
*/
class Kernel : public Object
{
  public:
    DataSet *data;

    //-----

    ///
    Kernel(DataSet *data_);

    /** Kernel between the example #i# and #j# of the dataset.
        This function takes in account the #selected_examples#
        of the dataset.
    */
    virtual real eval(int i, int j) = 0;

    /** Kernel between the example #i# of the dataset and
        one example #y#. The structure of #y# \emph{must}
        be the same as those generated in #inputs# by
        the dataset #data#.
        Note that this function don't care about the #selected_examples#
        of the dataset: #i# is a \emph{real} index.
    */
    virtual real realEval(int i, List *y) = 0;

    /// Reset the kernel (Default, do nothing)
    virtual void reset();

    //-----

    virtual ~Kernel();
};

/** DotProduct

    @author Ronan Collobert (collober@iro.umontreal.ca)
 */
class DotKernel : public Kernel
{
  public:
    EuclideanDataSet *edata;

    ///
    DotKernel(EuclideanDataSet *edata_);

    virtual real eval(int i, int j);
    virtual real realEval(int i, List *y);
    virtual ~DotKernel();
};


/** Polynomial $k(x,y) = (s*x.y+r)^d$.
    
    Options:
    \begin{tabular}{lcll}
      "degree"  &  int  &   degree of the polynome & [2]\\
      "mul cst" &  real &   s                      & [1]\\
      "add cst" &  real &   r                      & [1]
    \end{tabular}

    @author Ronan Collobert (collober@iro.umontreal.ca)
*/
class PolynomialKernel : public Kernel
{
  public:
    EuclideanDataSet *edata;
    int d;
    real s, r;

    ///
    PolynomialKernel(EuclideanDataSet *edata_);
    virtual real eval(int i, int j);
    virtual real realEval(int i, List *y);
    virtual ~PolynomialKernel();
};

/** Gaussian $k(x,y) = exp(-g * ||x-y||^2)$
    
    Options:
    \begin{tabular}{lcll}
      "gamma"  &  real  &   g & [0.01]
    \end{tabular}

    @author Ronan Collobert (collober@iro.umontreal.ca)
*/
class GaussianKernel : public Kernel
{
  public:
    EuclideanDataSet *edata;
    real g;
    real *precalc;
    real *precalc_real;
    bool precalc_alloc;

    ///
    GaussianKernel(EuclideanDataSet *edata_);
    virtual void reset();
    virtual real eval(int i, int j);
    virtual real realEval(int i, List *y);
    virtual ~GaussianKernel();
};

/** Sigmoid $k(x,y) = tanh(s*x.y+r)$
    
    Options:
    \begin{tabular}{lcll}
      "mul cst" &  real &    s        &          [1]\\
      "add cst" &  real &    r        &          [1]
    \end{tabular}

    @author Ronan Collobert (collober@iro.umontreal.ca)
*/
class SigmoidKernel : public Kernel
{
  public:
    EuclideanDataSet *edata;
    real s, r;

    ///
    SigmoidKernel(EuclideanDataSet *edata_);
    virtual real eval(int i, int j);
    virtual real realEval(int i, List *y);
    virtual ~SigmoidKernel();
};


}

#endif