Matlab 高斯核确认
我正在尝试实现这个内核函数 这也被称为径向基函数。假设Matlab 高斯核确认,matlab,Matlab,我正在尝试实现这个内核函数 这也被称为径向基函数。假设a=2,b=1和σ=150 Xi是一个425x3矩阵 Xj是一个4x3矩阵 我已经想出了这个代码,但我不确定这是正确的。你能帮我吗 kS = exp( - (pdist2(Xj,Xi).^2) / (sigma^2) ) 注意:原来的答案被完全重新定义了,因为我误解了问题的定义 下面给出了Xi和Xj之间内核距离的评估。给出了实现该算法的两个代码。第一个代码效率低下,但很容易与内核距离的定义相关联。第二个代码效率更高,但由于一些矢量化技巧
a=2
,b=1
和σ=150
- Xi是一个425x3矩阵
- Xj是一个4x3矩阵
kS = exp( - (pdist2(Xj,Xi).^2) / (sigma^2) )
注意:原来的答案被完全重新定义了,因为我误解了问题的定义 下面给出了
Xi
和Xj
之间内核距离的评估。给出了实现该算法的两个代码。第一个代码效率低下,但很容易与内核距离的定义相关联。第二个代码效率更高,但由于一些矢量化技巧,可能没有那么清晰
本规范假设问题的解释如下:
Xi
和Xj
是两个数据集,分别包含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);
谢谢你的回答。我试图解决的问题是用上述函数计算两个模式之间的核化距离。例如,在实空间中,两个图案之间的距离可以通过欧几里德距离来计算。但是如果我们把数据映射到高维空间,我们需要一个核化的距离度量。我不确定上面的脚本是否符合我的要求。谢谢你的评论。我可能误解了你的问题。不过,如果您能进一步解释一下您的问题,我将不胜感激。你所说的“核化距离度量”到底是什么意思?它是否符合这里提供的内核距离定义(等式1.1)?这正是我的意思。如果我们不将数据映射到更高的维度,那么两个模式之间的距离就可以很容易地计算出来。因此,核化距离由以下公式给出:| |φ(Xi)-φ(Xj)| |.^2=K(Xi,Xi)+K(Xj,Xj)-2K(Xi,Xj)。显然,对于高斯核,K(Xi,Xi)=1,因此上述关系简化为:d(Xi,Xj)=| |φ(Xi)-φ(Χj)| ^ 2=2*(1-K(Xi,Xj))。这是我正在尝试实现的内核化距离度量。谢谢。我不确定我是否完全理解你对X_I和X_j的解释,而不是内核定义中的X_(I,k)和X_(j,k)。我对这个问题的(新)解释是X_i和X_j是两个数据集。X_i有425个点,X_j有4个点(每个点属于R^3)。您需要计算这两组点之间的内核距离。我之所以认为是这样,是因为内核定义中的和是在k上迭代的,而不是I和j。在这种情况下,d(Xi,Xj)的定义仅适用于单个点,而不适用于整个数据集。