-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsvmkernel.m
More file actions
211 lines (177 loc) · 5.1 KB
/
Copy pathsvmkernel.m
File metadata and controls
211 lines (177 loc) · 5.1 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
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
function [K,option]=svmkernel(x,kernel,kerneloption,xsup,framematrix,vector,dual);
% Usage K=svkernel(x,kernel,kerneloption,xsup,frame,vector,dual);
%
% Returns the scalar product of the vectors x by using the
% mapping defined by the kernel function or x and xsup
% if the matrix xsup is defined
%
% Input
%
% x :input vectors
% kernel : kernel function
% Type Function Option
% Polynomial 'poly' Degree (<x,xsup>+1)^d
% Homogeneous polynomial 'polyhomog' Degree <x,xsup>^d
% Gaussian 'gaussian' Bandwidth
% Heavy Tailed RBF 'htrbf' [a,b] %see Chappelle 1999
% Mexican 1D Wavelet 'wavelet'
% Frame kernel 'frame' 'sin','numerical'...
%
% kerneloption : scalar or vector containing the option for the kernel
% 'gaussian' : scalar gamma is identical for all coordinates
% otherwise is a vector of length equal to the number of
% coordinate
%
%
% 'poly' : kerneloption is a scalar given the degree of the polynomial
% or is a vector which first element is the degree of the polynomial
% and other elements gives the bandwidth of each dimension.
% thus the vector is of size n+1 where n is the dimension of the problem.
%
%
% xsup : support vector
%
% ----- 1D Frame Kernel --------------------------
%
% framematrix frame elements for frame kernel
% vector sampling position of frame elements
% dual dual frame
% frame,vector and dual are respectively the matrices and the vector where the frame
% elements have been processed. these parameters are used only in case
%
%
% see also svmreg,svmclass,svmval, kernelwavelet,kernelframe
%
% O4/O6/2000 A. Rakotomamonjy
if nargin < 6
vector=[];
dual=[];
end;
if nargin <5
frame=[];
end;
if nargin<4
xsup=x;
end;
if nargin<3
kerneloption=1;
end;
if nargin<2
kernel='gaussian';
end;
if isempty(xsup)
xsup=x;
end;
[n1 n2]=size(x);
[n n3]=size(xsup);
ps = zeros(n1,n); % produit scalaire
switch lower(kernel)
case 'poly'
[nk,nk2]=size(kerneloption);
if nk>nk2
kerneloption=kerneloption';
nk2=nk;
end;
if nk2==1
degree=kerneloption;
var=ones(1,n2);
elseif nk2 ==2
degree=kerneloption(1);
var=ones(1,n2)*kerneloption(2);
elseif nk2== n2+1
degree=kerneloption(1);
var=kerneloption(2:n2+1);
elseif nk2 ==n2+2
degree=kerneloption(1);
var=kerneloption(2:n2+1);
end;
if nk2==1
aux=1;
else
aux=repmat(var,n,1);
end;
ps= x *(xsup.*aux.^2)';
if degree > 1
K =(ps+1).^degree;
else
K=ps;
end;
case 'polyhomog'
[nk,nk2]=size(kerneloption);
if nk>nk2
kerneloption=kerneloption';
nk2=nk;
end;
if nk2==1
degree=kerneloption;
var=ones(1,n2);
else
if nk2 ~=n2+1
degree=kerneloption(1);
var=ones(1,n2)*kerneloption(2);
else
degree=kerneloption(1);
var=kerneloption(2:nk2);
end;
end;
aux=repmat(var,n,1);
ps= x *(xsup.*aux.^2)';
K =(ps).^degree;
case 'gaussian'
[nk,nk2]=size(kerneloption);
if nk ~=nk2
if nk>nk2
kerneloption=kerneloption';
end;
else
kerneloption=ones(1,n2)*kerneloption;
end;
if length(kerneloption)~=n2 & length(kerneloption)~=n2+1
error('Number of kerneloption is not compatible with data...');
end;
metric = diag(1./kerneloption.^2);
ps = x*metric*xsup';
[nps,pps]=size(ps);
normx = sum(x.^2*metric,2);
normxsup = sum(xsup.^2*metric,2);
ps = -2*ps + repmat(normx,1,pps) + repmat(normxsup',nps,1) ;
K = exp(-ps/2);
case 'htrbf' % heavy tailed RBF %see Chappelle Paper%
b=kerneloption(2);
a=kerneloption(1);
for i=1:n
ps(:,i) = sum( abs((x.^a - ones(n1,1)*xsup(i,:).^a)).^b ,2);
end;
K = exp(-ps);
case 'gaussianslow' %
%b=kerneloption(2);
%a=kerneloption(1);
for i=1:n
ps(:,i) = sum( abs((x - ones(n1,1)*xsup(i,:))).^2 ,2)./kerneloption.^2/2;
end;
K = exp(-ps);
case 'multiquadric'
metric = diag(1./kerneloption);
ps = x*metric*xsup';
[nps,pps]=size(ps);
normx = sum(x.^2*metric,2);
normxsup = sum(xsup.^2*metric,2);
ps = -2*ps + repmat(normx,1,pps) + repmat(normxsup',nps,1) ;
K=sqrt(ps + 0.1);
case 'wavelet'
K=kernelwavelet(x,kerneloption,xsup);
case 'frame'
K=kernelframe(x,kerneloption,xsup,framematrix,vector,dual);
case 'wavelet2d'
K=wav2dkernelint(x,xsup,kerneloption);
case 'radialwavelet2d'
K=radialwavkernel(x,xsup);
case 'tensorwavkernel'
[K,option]=tensorwavkernel(x,xsup,kerneloption);
case 'numerical'
K=kerneloption.matrix;
case 'polymetric'
K=x*kerneloption.metric*xsup';
case 'jcb'
K=x*xsup';
end;