-
Notifications
You must be signed in to change notification settings - Fork 29
/
construct_kernel.m
68 lines (60 loc) · 1.68 KB
/
construct_kernel.m
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
% Kernel construction.
%
% -------------------------------------------------------
% Input:
% X1: nxm data matrix (rows are samples)
% X2: nxm data matrix (rows are samples)
% opts: Structure value with the following fields:
% opts.KernelType: kernel type, choices are:
% 'Linear': (x'*y),'Gaussian': (e^{-(|x-y|^2)/2sigma^2}),
% 'Polynomial': ((x'*y)^d)
% opts.sigma: variance for Gaussian kernel
% opts.d: degree for polynomial kernel
%
% Output:
% K: nxn kernel matrix
%
function K = construct_kernel(X1,X2,options)
if (~exist('options','var'))
options = [];
else
if ~isstruct(options)
error('parameter error!');
end
end
if ~isfield(options,'KernelType')
options.KernelType = 'linear';
end
switch lower(options.KernelType)
case 'gaussian' % e^{-(|x-y|^2)/2t^2}
if ~isfield(options,'sigma')
options.sigma = 1;
end
if isempty(X2)
D = eucl_dist2(X1,[],0);
else
D = eucl_dist2(X1,X2,0);
end
K = exp(-D/(2*options.sigma^2));
case 'polynomial' % (x'*y)^d
if ~isfield(options,'d')
options.d = 2;
end
if isempty(X2)
D = full(X1 * X1');
else
D = full(X1 * X2');
end
K = D.^options.d;
case 'linear' % x'*y
if isempty(X2)
K = full(X1 * X1');
else
K = full(X1 * X2');
end
otherwise
error('Parameter error: opts is not a structure.');
end
if isempty(X2)
K = max(K,K');
end