?? kernel.m
字號:
function [K] = kernel(ker,x,y)
% Calculate kernel function.
% x: 輸入樣本,n1×d的矩陣,n1為樣本個數,d為樣本維數
% y: 輸入樣本,n2×d的矩陣,n2為樣本個數,d為樣本維數
% ker 核參數(結構體變量)
% the following fields:
% type - linear : k(x,y) = x'*y
% poly : k(x,y) = (x'*y+c)^d
% gauss : k(x,y) = exp(-0.5*(norm(x-y)/s)^2)
% % norm命令計算矩陣或向量(x-y)的2-范數
% tanh : k(x,y) = tanh(g*x'*y+c)
% degree - Degree d of polynomial kernel (positive scalar).
% offset - Offset c of polynomial and tanh kernel (scalar, negative for tanh).
% width - Width s of Gauss kernel (positive scalar).
% gamma - Slope g of the tanh kernel (positive scalar).
% ker = struct('type','linear');
% ker = struct('type','ploy','degree',d,'offset',c);
% ker = struct('type','gauss','width',s);
% ker = struct('type','tanh','gamma',g,'offset',c);
% K: 輸出核參數,n1×n2的矩陣
%-------------------------------------------------------------%
switch ker.type
case 'linear'
K = x*y'; % n*n的矩陣
case 'ploy'
d = ker.degree;
c = ker.offset;
K = (x*y'+c).^d;
case 'gauss'
s = ker.width;
rows = size(x,1);
cols = size(y,1);
tmp = zeros(rows,cols);
for i = 1:rows
for j = 1:cols
tmp(i,j) = norm(x(i,:)-y(j,:));
end
end
K = exp(-0.5*(tmp/s).^2);
case 'tanh'
g = ker.gamma;
c = ker.offset;
K = tanh(g*x*y'+c);
otherwise
K = 0;
end
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -