-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathkernel.h
More file actions
108 lines (92 loc) · 2.7 KB
/
Copy pathkernel.h
File metadata and controls
108 lines (92 loc) · 2.7 KB
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
#ifndef LIBVM_KERNEL_H_
#define LIBVM_KERNEL_H_
#include "utilities.h"
typedef double Qfloat;
enum { LINEAR, POLY, RBF, SIGMOID, PRECOMPUTED }; // kernel_type
struct KernelParameter {
int kernel_type;
int degree; // for poly
double gamma; // for poly/rbf/sigmoid
double coef0; // for poly/sigmoid
};
//
// Kernel Cache
//
// l is the number of total data items
// size is the cache size limit in bytes
//
class Cache {
public:
Cache(int l, long int size);
~Cache();
// request data [0,len)
// return some position p where [p,len) need to be filled
// (p >= len if nothing needs to be filled)
int get_data(const int index, Qfloat **data, int len);
void SwapIndex(int i, int j);
private:
int l_;
long int size_;
struct Head {
Head *prev, *next; // a circular list
Qfloat *data;
int len; // data[0,len) is cached in this entry
};
Head *head_;
Head lru_head_;
void DeleteLRU(Head *h);
void InsertLRU(Head *h);
};
//
// Kernel evaluation
//
// the static method KernelFunction is for doing single kernel evaluation
// the constructor of Kernel prepares to calculate the l*l kernel matrix
// the member function get_Q is for getting one column from the Q Matrix
//
class QMatrix {
public:
virtual Qfloat *get_Q(int column, int len) const = 0;
virtual double *get_QD() const = 0;
virtual void SwapIndex(int i, int j) const = 0;
virtual ~QMatrix() {}
};
class Kernel : public QMatrix {
public:
Kernel(int l, Node *const *x, const KernelParameter *param);
virtual ~Kernel();
static double KernelFunction(const Node *x, const Node *y, const KernelParameter *param);
virtual Qfloat *get_Q(int column, int len) const = 0;
virtual double *get_QD() const = 0;
virtual void SwapIndex(int i, int j) const;
protected:
double (Kernel::*kernel_function)(int i, int j) const;
private:
const Node **x_;
double *x_square_;
// KernelParameter
const int kernel_type_;
const int degree_;
const double gamma_;
const double coef0_;
static double Dot(const Node *px, const Node *py);
double KernelLinear(int i, int j) const {
return Dot(x_[i], x_[j]);
}
double KernelPoly(int i, int j) const {
return std::pow(gamma_*Dot(x_[i], x_[j])+coef0_, degree_);
}
double KernelRBF(int i, int j) const {
return exp(-gamma_*(x_square_[i]+x_square_[j]-2*Dot(x_[i], x_[j])));
}
double KernelSigmoid(int i, int j) const {
return tanh(gamma_*Dot(x_[i], x_[j])+coef0_);
}
double KernelPrecomputed(int i, int j) const {
return x_[i][static_cast<int>(x_[j][0].value)].value;
}
void KernelText();
};
void InitKernelParam(struct KernelParameter *param);
const char *CheckKernelParameter(const struct KernelParameter *param);
#endif // LIBVM_KERNEL_H_