-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconstructKernel.m
98 lines (87 loc) · 2.4 KB
/
constructKernel.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
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
function K = constructKernel(fea_a,fea_b,options)
% function K = constructKernel(fea_a,fea_b,options)
% Usage:
% K = constructKernel(fea_a,[],options)
%
% K = constructKernel(fea_a,fea_b,options)
%
% fea_a, fea_b : Rows of vectors of data points.
%
% options : Struct value in Matlab. The fields in options that can
% be set:
% KernelType - Choices are:
% 'Gaussian' - e^{-(|x-y|^2)/2t^2}
% 'Polynomial' - (x'*y)^d
% 'PolyPlus' - (x'*y+1)^d
% 'Linear' - x'*y
%
% t - parameter for Gaussian
% d - parameter for Poly
%
% version 1.0 --Sep/2006
%
% Written by Deng Cai (dengcai2 AT cs.uiuc.edu)
%
if (~exist('options','var'))
options = [];
else
if ~isstruct(options)
error('parameter error!');
end
end
%=================================================
if ~isfield(options,'KernelType')
options.KernelType = 'Gaussian';
end
switch lower(options.KernelType)
case {lower('Gaussian')} % e^{-(|x-y|^2)/2t^2}
if ~isfield(options,'t')
options.t = 1;
end
case {lower('Polynomial')} % (x'*y)^d
if ~isfield(options,'d')
options.d = 2;
end
case {lower('PolyPlus')} % (x'*y+1)^d
if ~isfield(options,'d')
options.d = 2;
end
case {lower('Linear')} % x'*y
otherwise
error('KernelType does not exist!');
end
%=================================================
switch lower(options.KernelType)
case {lower('Gaussian')}
if isempty(fea_b)
D = EuDist2(fea_a,[],0);
else
D = EuDist2(fea_a,fea_b,0);
end
K = exp(-D/(2*options.t^2));
case {lower('Polynomial')}
if isempty(fea_b)
D = full(fea_a * fea_a');
else
D = full(fea_a * fea_b');
end
K = D.^options.d;
case {lower('PolyPlus')}
if isempty(fea_b)
D = full(fea_a * fea_a');
else
D = full(fea_a * fea_b');
end
K = (D+1).^options.d;
case {lower('Linear')}
if isempty(fea_b)
K = full(fea_a * fea_a');
else
K = full(fea_a * fea_b');
end
otherwise
error('KernelType does not exist!');
end
if isempty(fea_b)
K = max(K,K');
end