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!