-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathkernel.m
More file actions
135 lines (125 loc) · 5.9 KB
/
Copy pathkernel.m
File metadata and controls
135 lines (125 loc) · 5.9 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
function k = kernel(u,v,options)
% KERNEL determines kernel function for kernel-based machine learning
% methods including Support Vector Machines.
%
%
%
% Inputs:
% u: First input vector (p dimensional column vector)
% v: Second input vector (p dimensional column vector)
% options: Struct value in Matlab
% The fields in options that can be set:
%
% options.KernelType: choices are:
%
% 'linear' without kernel function (no parameter)(default value)
% 'poly' simple polynomial kernel function (with 1 parameter which is degree of polynomial)
% 'polyplus' polynomial kernel function (with 1 parameter which is degree of polynomial)
% 'sphnorm_poly' spherically normalized polynomial kernel function (with 2 parameters:
% the first one is degree of polynomial & the second one is spherical normalization parameter)
% 'rbf' Gaussian (RBF) kernel function (with 1 parameyer which is width of RBF (sigma)
% 'wave' wavelet kernel function (with 1 parameter which is dilation factor.
%
% options.KernelPars: a row vector. Minimum size of it is 0 (when no parameters is needed for kernel function)
% and maximum size of it is 2 (when kernel function has 2 parameters)
%
%
% Output:
% k: kernel function value (a real scalar)
%
%
% Sample use:
% k = kernel(u,v,options); % computes dot product in feature space
%
%
%
% Bugs: (1) Before using this function, you should determine suitable values for options.KernelType and options.KernelPars
% variables otherwise the default values will be used by function.
% (2) If you use spherically normalized kernels, you'd better already normalize the training data to zero mean and unit variance then set the spherical
% normalization parameter to 1.
%
%
%
% More details can be found in:
%
% S. Meshgini, A. Aghagolzadeh, H. Seyedarabi, "Face recognition using
% Gabor-based direct linear discriminant analysis and support vector machine,"
% Computers & Electrical Engineering, vol. 39, no. 3, pp. 727-745, 2013.
%
%
%
% (C) Saeed Meshgini, Ph.D.
% University of Tabriz
% checking the correct use of input arguments:
if (~exist('options','var'))
options.KernelType = 'linear';
end
% checking the same dimensionality of input vectors:
p = length(u);
q = length(v);
if p ~= q
error('dimension of two vectors must be the same.')
end
% kernels:
if ~isfield(options,'KernelType')
options.KernelType = 'linear'; % default kernel function is linear
end
switch lower(options.KernelType)
case {lower('linear')}
k = u.'*v; % u'*v
case {lower('poly')}
if ~isfield(options,'KernelPars')
options.KernelPars = 2; % default value for degree of polynomial is 2
end
k = (u.'*v)^options.KernelPars; % (u'*v)^n
case {lower('polyplus')}
if ~isfield(options,'KernelPars')
options.KernelPars = 2; % default value for degree of polynomial is 2
end
k = (u.'*v+1)^options.KernelPars; % (u'*v+1)^n
case {lower('sphnorm_poly')}
if ~isfield(options,'KernelPars')
options.KernelPars = [2 1]; % default value for degree of polynomial is 2 and default parameter for spherical normalization is 1
end
k = (1/2^options.KernelPars(1))*(((u.'*v+options.KernelPars(2)^2)/sqrt((u.'*u+options.KernelPars(2)^2)*(v.'*v+options.KernelPars(2)^2)))+1)^options.KernelPars(1);
% ((u'*v+d^2)/sqrt((u'*u+d^2)(v'*v+d^2))+1)^n/2^n
case {lower('rbf')}
if ~isfield(options,'KernelPars') % default value for sigma is 1
options.KernelPars = 1;
end
k = exp((-(u-v).'*(u-v))/(2*options.KernelPars^2));
% e^{-(|u-v|^2)/2(sigma)^2}
case {lower('wave')}
if ~isfield(options,'KernelPars') % default value for sigma is 1
options.KernelPars = 2 ; % default value for dilation factor is 2 and default mother wavelet is morlet function
end
% if strcmp(options.KernelPars(2),'morl')
pro = 1;
for i = 1:p
pro = pro*(cos(1.75*((u(i)-v(i))/options.KernelPars))*exp(-((u(i)-v(i))^2)/(2*options.KernelPars^2)));
end
k = pro; % Pro(psi(ui-vi)/a), psi(x) = cos(1.75x)exp(-x^2/2)
% elseif strcmp(options.KernelPars(2),'mexh')
% pro = 1;
% for i = 1:p
% pro = pro*(((2/sqrt(3))*pi^(-0.25))*(1-((u(i)-v(i))/options.KernelPars(1))^2)*exp((-((u(i)-v(i))/options.KernelPars(1))^2)/2));
% end
% k = pro;
% elseif strcmp(options.KernelPars(2),'haar')
% pro = 1;
% for i = 1:p
% if (((u(i)-v(i))/options.KernelPars(1))> = 0)&&(((u(i)-v(i))/options.KernelPars(1))<0.5)
% pro = pro*1;
% elseif (((u(i)-v(i))/options.KernelPars(1))> = 0.5)&&(((u(i)-v(i))/options.KernelPars(1))<1)
% pro = pro*(-1);
% else
% pro = pro*0;
% end
% end
% k = pro;
% else
% error('use one of these wavelets ("morl" or "mexh" or "haar") as valid wavelet types')
% end
otherwise
error('KernelType does not exist!');
end