我有一个3D网格,X,Y,Z。我想创建一个新的3D数组,它是X,Y和Z的函数。该函数由位于不同点的几个3D高斯和组成。目前,我有一个for循环,它在高斯分布的不同点上运行,并且我有一个中心位置数组r0(nGauss,1:3)
[X,Y,Z]=meshgrid(-10:.1:10);
Psi=0*X;
for index = 1:nGauss
Psi = Psi + Gauss3D(X,Y,Z,[r0(index,1),r0(index,2),r0(index,3)]);
end我的3D高斯函数是
function output=Gauss3D(X,Y,Z,r0)
output=exp(-(X-r0(1)).^2 + (Y-r0(2)).^2 + (Z-r0(3)).^2);
end我很高兴重新设计这个函数,这是我的代码中最慢的部分,并且必须多次发生,但我不知道如何向量化它,使它运行得更快。如有任何建议,我们将不胜感激
*NB原始函数有平方根,已修改为实际的高斯*
发布于 2018-08-01 06:37:33
注意!我已经修改了你的代码来创建一个高斯,这是:
output=exp(-sqrt((X-r0(1)).^2 + (Y-r0(2)).^2 + (Z-r0(3)).^2));
这并不意味着是高斯。我将其更改为:
output = exp(-((X-r0(1)).^2 + (Y-r0(2)).^2 + (Z-r0(3)).^2));(注意没有sqrt)。这是一个sigma = sqrt(1/2)的高斯分布。
如果这不是你想要的,那么这个答案可能对你不是很有用,因为你的函数不会像高斯函数那样快到0,因此更难截断,并且它是不可分的。
向量化这段代码是毫无意义的,正如其他答案所证明的那样。MATLAB的JIT完全有能力以最快的速度运行它。但是,您可以通过注意高斯非常快地接近于零并且是可分离的来显著减少计算量:
exp评估都会产生一个非常小的数字。你不需要计算这些,只需填写0.exp(-x.^2-y.^2)与exp(-x.^2).*exp(-y.^2)相同,计算起来要便宜得多。让我们来检验一下这两件事。下面是测试代码:
function gaussian_test
N = 100;
r0 = rand(N,3)*20 - 10;
% Original
tic
[X,Y,Z] = meshgrid(-10:.1:10);
Psi1 = zeros(size(X));
for index = 1:N
Psi1 = Psi1 + Gauss3D(X,Y,Z,r0(index,:));
end
t = toc;
fprintf('original, time = %f\n',t)
% Fast, large truncation
tic
[X,Y,Z] = deal(-10:.1:10);
Psi2 = zeros(numel(X),numel(Y),numel(Z));
for index = 1:N
Psi2 = Gauss3D_fast(Psi2,X,Y,Z,r0(index,:),5);
end
t = toc;
fprintf('tuncation = 5, time = %f\n',t)
fprintf('mean abs error = %f\n',mean(reshape(abs(Psi2-Psi1),[],1)))
fprintf('mean square error = %f\n',mean(reshape((Psi2-Psi1).^2,[],1)))
fprintf('max abs error = %f\n',max(reshape(abs(Psi2-Psi1),[],1)))
% Fast, smaller truncation
tic
[X,Y,Z] = deal(-10:.1:10);
Psi3 = zeros(numel(X),numel(Y),numel(Z));
for index = 1:N
Psi3 = Gauss3D_fast(Psi3,X,Y,Z,r0(index,:),3);
end
t = toc;
fprintf('tuncation = 3, time = %f\n',t)
fprintf('mean abs error = %f\n',mean(reshape(abs(Psi3-Psi1),[],1)))
fprintf('mean square error = %f\n',mean(reshape((Psi3-Psi1).^2,[],1)))
fprintf('max abs error = %f\n',max(reshape(abs(Psi3-Psi1),[],1)))
% DIPimage, same smaller truncation
tic
Psi4 = newim(201,201,201);
coords = (r0+10) * 10;
Psi4 = gaussianblob(Psi4,coords,10*sqrt(1/2),(pi*100).^(3/2));
t = toc;
fprintf('DIPimage, time = %f\n',t)
fprintf('mean abs error = %f\n',mean(reshape(abs(Psi4-Psi1),[],1)))
fprintf('mean square error = %f\n',mean(reshape((Psi4-Psi1).^2,[],1)))
fprintf('max abs error = %f\n',max(reshape(abs(Psi4-Psi1),[],1)))
end % of function gaussian_test
function output = Gauss3D(X,Y,Z,r0)
output = exp(-((X-r0(1)).^2 + (Y-r0(2)).^2 + (Z-r0(3)).^2));
end
function Psi = Gauss3D_fast(Psi,X,Y,Z,r0,trunc)
% sigma = sqrt(1/2)
x = X-r0(1);
y = Y-r0(2);
z = Z-r0(3);
mx = abs(x) < trunc*sqrt(1/2);
my = abs(y) < trunc*sqrt(1/2);
mz = abs(z) < trunc*sqrt(1/2);
Psi(my,mx,mz) = Psi(my,mx,mz) + exp(-x(mx).^2) .* reshape(exp(-y(my).^2),[],1) .* reshape(exp(-z(mz).^2),1,1,[]);
% Note! the line above uses implicit singleton expansion. For older MATLABs use bsxfun
end这是我机器上的输出,为了可读性进行了重新排序(我仍然使用MATLAB R2017a):
| time(s) | mean abs | mean sq. | max abs
--------------+----------+----------+----------+----------
original | 5.035762 | | |
tuncation = 5 | 0.169807 | 0.000000 | 0.000000 | 0.000005
tuncation = 3 | 0.054737 | 0.000452 | 0.000002 | 0.024378
DIPimage | 0.044099 | 0.000452 | 0.000002 | 0.024378正如您所看到的,使用高斯的这两个属性,我们可以将时间从5.0秒减少到0.17秒,加速30倍,几乎没有明显的差异(在5*sigma处截断)。通过允许较小的误差,可以进一步获得3倍的加速比。截断值越小,速度越快,但误差越大。
我添加了最后一个方法,来自DIPimage (我是作者)的gaussianblob函数,只是为了显示该选项,以防您需要从代码中挤出一点额外的时间。该函数是用C++实现的。我使用的这个版本你需要自己编译。我们的current official release仍然在M-file代码中实现这个功能,而且速度不是很快。
进一步改进的机会是如果坐标的小数部分始终相同(w.r.t.像素网格)。在这种情况下,您可以绘制一次高斯,并将其移动到每个质心。
另一种选择涉及在更大的尺度上计算一次高斯,并对其进行插值,以生成生成输出所需的每一个一维高斯。我没有实现这一点,我不知道它是否会更快,或者时间差是否会很大。在过去,exp很贵,我不确定现在是否还是这样。
发布于 2018-08-01 06:06:24
所以,我基于我上面的答案@Durkee。我喜欢这些类型的问题,所以我考虑了一下如何使每个扩展都隐式地进行,我有下面的一行函数。使用此函数,我将调用时间缩短了.11秒,这完全可以忽略不计。你的看起来挺不错的。我的唯一优势可能是代码如何在更精细的网格上缩放。
xLin = [-10:.1:10]';
tic
psi2 = sum(exp(-sqrt((permute(xLin-r0(:,1)',[3 1 4 2])).^2 ...
+ (permute(xLin-r0(:,2)',[1 3 4 2])).^2 ...
+ (permute(xLin-r0(:,3)',[3 4 1 2])).^2)),4);
toc我计算机上的相对运行时间是(所有东西都保持不变):
Original - 1.234085
Other - 2.445375
Mine - 1.120701发布于 2018-08-01 04:25:44
这是一个不同寻常的问题,在我的计算机上,未矢量化的代码实际上比矢量化的代码工作得更好,这是我的脚本
clear
[X,Y,Z]=meshgrid(-10:.1:10);
Psi=0*X;
nGauss = 20; %Sample nGauss as you didn't specify
r0 = rand(nGauss,3); % Just make this up as it doesn't really matter in this case
% Your original code
tic
for index = 1:nGauss
Psi = Psi + Gauss3D(X,Y,Z,[r0(index,1),r0(index,2),r0(index,3)]);
end
toc
% Vectorize these functions so we can use implicit broadcasting
X1 = X(:);
Y1 = Y(:);
Z1 = Z(:);
tic
val = [X1 Y1 Z1];
% Change the dimensions so that r0 operates on the right elements
r0_temp = permute(r0,[3 2 1]);
% Perform the gaussian combination
out = sum(exp(-sqrt(sum((val-r0_temp).^2,2))),3);
toc
% Check to make sure both functions match
sum(abs(vec(Psi)-vec(out)))
function output=Gauss3D(X,Y,Z,r0)
output=exp(-sqrt((X-r0(1)).^2 + (Y-r0(2)).^2 + (Z-r0(3)).^2));
end
function out = vec(in)
out = in(:);
end如您所见,这可能是您所能得到的最大程度的矢量化。整个功能都是使用广播和矢量化操作来完成的,这通常会将性能提高10-100倍。然而,在这种情况下,这不是我们所看到的
Elapsed time is 1.876460 seconds.
Elapsed time is 2.909152 seconds.这实际上显示了未矢量化的版本更快。
这可能有几个原因,我绝对不是专家。
顺便说一句,我在我的GTX 1060 GPU上用单精度进行了测试(在大多数GPU上,单精度比双精度快10倍)
Elapsed time is 0.087405 seconds.
Elapsed time is 0.241456 seconds.再一次,非矢量化版本更快,很抱歉我不能帮助你,但是看起来你的代码和你将要得到的一样好,除非你降低网格上的容差。
https://stackoverflow.com/questions/51617566
复制相似问题