-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKernel.m
103 lines (87 loc) · 3.51 KB
/
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
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
function mat_kernel = Kernel(mat_train , mat_test , kernelType , kPara)
% input parameters:
% mat_train--£the train data, a row represents a sample
% mat_test---- the test data, a row represents a sample
% kernelType-- the kernel type
% kPara------- the kernel parameter(s)
% output parameter:
% mat_kernel-- the kernel matrix
% Written by WangZhe on 2004-09-27.
lenOne = size(mat_train , 1) ;
lenTwo = size(mat_test , 1) ;
temp_mat = [] ;
switch lower(kernelType)
case 'linear'
mat_kernel=mat_train*mat_test';
case 'poly'
temp_mat_kernel=(mat_train*mat_test'+1).^kPara;
mat_kernel=temp_mat_kernel;
% D=diag(1./sqrt(diag(temp_mat_kernel)));
% mat_kernel=D*temp_mat_kernel*D;
case 'rbf'
TrainSampleNum=size(mat_train,1);
TestSampleNum=size(mat_test,1);
mat_temp=sum(mat_train.^2,2)*ones(1,TestSampleNum)...
+ones(TrainSampleNum,1)*sum(mat_test.^2,2)'...
-2*mat_train*mat_test';
mat_kernel=exp(-mat_temp/(2*kPara^2));
case 'sigmoid'
mat_kernel=tan(kPara(1)*mat_train*mat_test'+kPara(2));
case 'exp'
mat_kernel=(1+exp(kPara*mat_train*mat_test')).^(-1);
%
%%%%%%%%%%%%%%%%%%%%%%%%%%% no parameter kernel %%%%%%%%%%%%%%%%%%%%%%%%%%%
%
case 'k10'
for oneId = 1 : lenOne
for twoId = 1 : lenTwo
temp_mat(oneId , twoId) = norm(mat_train(oneId , :) - mat_test(twoId , :)) ;
end
end
z = max(temp_mat(:)) ;
temp_mat = -temp_mat./z ;
for i = 2 : 3
temp_mat = temp_mat + temp_mat.^i./2^(i-1) ;
end
mat_kernel = 1 + temp_mat ;
case 'k11'
for oneId = 1 : lenOne
for twoId = 1 : lenTwo
temp_mat(oneId , twoId) = norm(mat_train(oneId , :) - mat_test(twoId , :)) ;
end
end
z = max(temp_mat(:)) ;
temp_mat = -temp_mat./z ;
for i = 2 : 3
temp_mat = temp_mat + temp_mat.^i./i ;
end
mat_kernel = 1 + temp_mat ;
case 'k12'
for oneId = 1 : lenOne
for twoId = 1 : lenTwo
temp_mat(oneId , twoId) = norm(mat_train(oneId , :) - mat_test(twoId , :)) ;
end
end
z = max(temp_mat(:)) ;
temp_mat = temp_mat./z ;
mat_kernel = zeros(lenOne , lenTwo) ;
for i = 1 : 3
mat_kernel = mat_kernel + (-1)^i./((temp_mat).^i+1) ;
end
mat_kernel = - mat_kernel ;
case 'k13'
for oneId = 1 : lenOne
for twoId = 1 : lenTwo
temp_mat(oneId , twoId) = norm(mat_train(oneId , :) - mat_test(twoId , :)) ;
end
end
z = max(temp_mat(:)) ;
temp_mat = temp_mat./z ;
mat_kernel = 1 - sin(pi.*temp_mat./(2*z)) ;
%
%%%%%%%%%%%%%%%%%%%%%%%%%%% no parameter kernel %%%%%%%%%%%%%%%%%%%%%%%%%%%
%
otherwise
mat_kernel=mat_train*mat_test';
end
end