也称为径向基函数。假设a = 2
,b = 1
和σ = 150
。
我想出了这段代码,但我不确定这是否正确。你能救我吗?
kS = exp( - (pdist2(Xj,Xi).^2) / (sigma^2) )
答案 0 :(得分:1)
注意:原始答案已完全重新定义,因为我误解了问题的定义。
下面列出了Xi
和Xj
之间内核距离的评估。给出了实现该算法的两个代码。第一个代码效率低,但可以很容易地与内核距离的定义相关。第二个代码效率更高,但由于几个矢量化技巧可能不太清楚。
代码假定对问题的以下解释:
Xi
和Xj
是2个数据集,分别包含425和4个点。每个点都属于R^3
(实际向量空间,维度为3)。算法最直接的实现:
% Initialisation.
clear;
clc;
% Construct Xi.
Xi = [randn(425, 1) randn(425, 1) randn(425, 1)];
% Definition of Xj.
Xj = [0.1 0.2 0.3; 0 0 0; -0.1 -0.1 -0.2; 1 -8 4];
% Convert to cell arrays.
Xi = mat2cell(Xi, ones(1, length(Xi(:, 1))), 3);
Xj = mat2cell(Xj, ones(1, length(Xj(:, 1))), 3);
% First, construct the kernel function for the evaluation of individual
% points in Xi and Xj
omega = 150;
a = 2;
kerFunction = @(xi, xj) exp(sum(abs(xi - xj).^a)/(omega^2));
kerDist = 0;
for i = 1 : length(Xj)
for j = 1 : length(Xj)
kerDist = kerDist + kerFunction(Xj{i}, Xj{j});
end
end
for i = 1 : length(Xi)
for j = 1 : length(Xi)
kerDist = kerDist + kerFunction(Xi{i}, Xi{j});
end
end
for i = 1 : length(Xi)
for j = 1 : length(Xj)
kerDist = kerDist - 2*kerFunction(Xi{i}, Xj{j});
end
end
下面介绍了该算法的更有效实现:
clear;
% Define constants.
omega = 150;
a = 2;
% Definition of Xi.
Xi = [randn(425, 1) randn(425, 1) randn(425, 1)];
% Definition of Xj.
Xj = [0.1 0.2 0.3; 0 0 0; -0.1 -0.1 -0.2; 1 -8 4];
% Definition of the characteristics of the data sets.
numPointsXj = length(Xj(:, 1));
numPointsXi = length(Xi(:, 1));
% Define a handle function for the definition of indices for the
% vectorisation of the kernel function.
hdlRepIdxPermutation = @(numPoints, numMatrixRep) ...
repmat( ...
(1 : numPoints : numPoints*(numMatrixRep - 1) + 1)', ...
1, numPoints ...
) + ...
repmat(0 : (numPoints - 1), numMatrixRep, 1);
tic
% Calculate the term that corresponds to K(p, p') in the definition of the
% kernal distance.
repXiRight = repmat(Xi, numPointsXi, 1);
leftIdxPermutationXi = hdlRepIdxPermutation(numPointsXi, numPointsXi);
repXiLeft = repXiRight(leftIdxPermutationXi(:), :);
kerDistComp1 = sum(exp(sum(abs(repXiLeft - repXiRight).^a, 2)/(omega^2)));
% Calculate the term that corresponds to K(q, q') in the definition of the
% kernal distance.
repXjRight = repmat(Xj, numPointsXj, 1);
leftIdxPermutationXj = hdlRepIdxPermutation(numPointsXj, numPointsXj);
repXjLeft = repXjRight(leftIdxPermutationXj(:), :);
kerDistComp2 = sum(exp(sum(abs(repXjLeft - repXjRight).^a, 2)/(omega^2)));
% Calculate the term that corresponds to K(p, q) in the definition of the
% kernal distance.
repXjRight = repmat(Xj, numPointsXi, 1);
repXiLeft = repmat(Xi, numPointsXj, 1);
leftIdxPermutationXi = hdlRepIdxPermutation(numPointsXi, numPointsXj);
repXiLeft = repXiLeft(leftIdxPermutationXi(:), :);
kerDistComp3 = -2*sum(exp(sum(abs(repXiLeft - repXjRight).^a, 2)/(omega^2)));
kerDist = kerDistComp1 + kerDistComp2 + kerDistComp3;
toc
disp(kerDist);