How to compute gaussian kernel matrix efficiently?
84 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Shahid Mahmood
il 21 Nov 2019
can you explain the whole procedure in detail to compute a kernel matrix in matlab
Risposte (2)
Matt J
il 28 Ott 2012
Modificato: Matt J
il 28 Ott 2012
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 Commenti
Farzan Zaheer
il 4 Ago 2015
Modificato: Farzan Zaheer
il 4 Ago 2015
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
il 28 Ott 2012
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 Commenti
Image Analyst
il 20 Giu 2022
@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
il 20 Giu 2022
am looking to get similarity between two time series by using this gaussian kernel, i think it's not the same situation, right?!
Vedere anche
Categorie
Scopri di più su Mathematics and Optimization in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!