2016-05-27 4 views
1

私はこのカーネル関数も放射基底関数として知られているenter image description hereガウスカーネル確認

を実装しようとしています。 a = 2,b = 1およびσ = 150とする。

  • Xiは425x3マトリックス
  • Xjのは、4x3の行列

である私は、このコードを思い付いてきましたが、私はそれが正しいかはわかりません。手伝って頂けますか?

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

答えて

1

注:問題の定義を誤って解釈したため、元の回答は完全に再定義されました。

XiXjの間のカーネル距離の評価を以下に示します。アルゴリズムを実装する2つのコードが示されています。最初のコードは非効率的ですが、カーネルの距離の定義に簡単に関連付けることができます。 2番目のコードははるかに効率的ですが、いくつかのベクトル化のために明確ではないかもしれません。

  1. XiXjは、それぞれ、425および4点を含む2つのデータセットである:

    コードは、問題の以下の解釈を前提としています。各点はR^3(次元3の実ベクトル空間)に属します。

  2. 2つのデータセット間のカーネル距離は、J.M. PhillipsとS. Venkatasubramanianの論文「カーネル距離の穏やかな入門」に記載されている定義に従って計算されます。これは、linkにあります。定義はまた、下に設けられている。

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); 
+0

ご回答ありがとうございます。私が解決しようとしている問題は、上記の関数で2つのパターン間のカーネル化された距離を計算することです。実際の空間では、2つのパターン間の距離は、例えばユークリッド距離によって計算することができる。しかし、データを高次元の空間にマッピングするには、カーネル化された距離メトリックが必要です。私は上記のスクリプトが私が望むことをしているかどうかはわかりません。 – popist

+0

ご意見ありがとうございます。私はあなたの質問を誤解しているかもしれません。しかし、あなたの質問をもう少し詳しく説明できれば幸いです。 「カーネル化された距離メトリック」は、正確にはどういう意味ですか?この[link](https://www.cs.utah.edu/~jeffp/papers/gentleintroKD.pdf)(1.1)で提供されるカーネル距離の定義に同意しますか? – user1391279

+0

これはまさに私が意味していたものです。データをより高い次元にマッピングしないと、2つのパターン間の距離を簡単に計算できます。したがって、カーネル化された距離は次式で与えられます。 – popist