高斯内核确认

时间:2016-05-27 15:32:50

标签: matlab

我正在尝试实现此内核函数enter image description here

也称为径向基函数。假设a = 2b = 1σ = 150

  • Xi是一个425x3矩阵
  • Xj是4x3矩阵

我想出了这段代码,但我不确定这是否正确。你能救我吗?

kS = exp( - (pdist2(Xj,Xi).^2) / (sigma^2) )

1 个答案:

答案 0 :(得分:1)

注意:原始答案已完全重新定义,因为我误解了问题的定义。

下面列出了XiXj之间内核距离的评估。给出了实现该算法的两个代码。第一个代码效率低,但可以很容易地与内核距离的定义相关。第二个代码效率更高,但由于几个矢量化技巧可能不太清楚。

代码假定对问题的以下解释:

  1. XiXj是2个数据集,分别包含425和4个点。每个点都属于R^3(实际向量空间,维度为3)。
  2. 两个数据集之间的核心距离是根据J.M. Phillips和S. Venkatasubramanian“A Gentle Introduction to the Kernel Distance”一文中给出的定义计算出来的,可以在以下link找到。定义如下:
  3. enter image description here

    算法最直接的实现:

    % 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);