How to compute gaussian kernel matrix efficiently?
84 ビュー (過去 30 日間)
古いコメントを表示
Hi,
I have a matrix X(10000, 800). I want to compute gramm matrix K(10000,10000), where K(i,j)= exp(-(X(i,:)-X(j,:))^2).
First i used double for loop, but then it just hangs forever. Then I tried this:
[N d] = size(X); aa = repmat(X',[1 N]); bb = repmat(reshape(X',1,[]),[N 1]); K = reshape((aa-bb).^2, [N*N d]); K = reshape(sum(D,2),[N N]); But then it uses a lot of extra space and I run out of memory very soon. Is there any efficient vectorized method for this. I am sure there must be something as this is quite a standard intermediate step for many kernel svms and also in image processing.
2 件のコメント
Shahid Mahmood
2019 年 11 月 21 日
can you explain the whole procedure in detail to compute a kernel matrix in matlab
回答 (2 件)
Matt J
2012 年 10 月 28 日
編集済み: Matt J
2012 年 10 月 28 日
Assuming you really want exp(-norm( X(i,:) - X(j,:) ))^2), then one way is
nsq=sum(X.^2,2);
K=bsxfun(@minus,nsq,(2*X)*X.');
K=bsxfun(@plus,nsq.',K);
K=exp(-K);
3 件のコメント
Farzan Zaheer
2015 年 8 月 4 日
編集済み: Farzan Zaheer
2015 年 8 月 4 日
I am working on Kernel LMS, and I am having issues with the implementation of Kernel. I want to know what exactly is "X2" here. I am implementing the Kernel using recursion.
I am using the following statement,
for n=2:K-1
Kernel(n)=exp(-0.5*(dist(x(:,2:n),x(:,n)')/ker_bw^2));
end
where ker_bw is the kernel bandwidth/sigma and x is input of (1000,1) and I have reshaped the input x as
x = [x(1:end-1),x(2:end)];
as mentioned in the research paper I am following. Any help will be highly appreciated.
Image Analyst
2012 年 10 月 28 日
If you have the Image Processing Toolbox, why not use fspecial()?
h = fspecial('gaussian', hsize, sigma) returns a rotationally symmetric Gaussian lowpass filter of size hsize with standard deviation sigma (positive). hsize can be a vector specifying the number of rows and columns in h, or it can be a scalar, in which case h is a square matrix. The default value for hsize is [3 3]; the default value for sigma is 0.5.
4 件のコメント
Image Analyst
2022 年 6 月 20 日
@amel kaouane it comes from your mind. You think up some sigma that might work, assign it like
sigma = 5;
hsize = 21;
h = fspecial('gaussian', hsize, sigma)
imshow(h, []);
axis('on', 'image')
If you don't like 5 for sigma then just try others until you get one that you like. It's not like I can tell you the perfect value of sigma because it really depends on your situation and image.
amel kaouane
2022 年 6 月 20 日
am looking to get similarity between two time series by using this gaussian kernel, i think it's not the same situation, right?!
参考
カテゴリ
Help Center および File Exchange で Mathematics and Optimization についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!