我试图用PyTorch
中的GPU
的加速度来优化函数(fit
)。这是一个直接的Python
代码,我在这里对fit
进行求值:
import numpy as np
...
for j in range(P):
e[:,j] = z - h[:,j];
fit[j] = 1/(sqrt(2*pi)*sigma*N)*np.sum(exp(-(e[:,j]**2)/(2*sigma**2)));
^{pr2}$The dimension of the variables are:
z
[Nx1],h
[NxP],e
[NxP],fit
[1xP]. whereP
is the number of dimension offit
andN
is the length of each dimension.I am aware that
for
loops should be avoided, so where it is my attempt to do it usingPyTorch
throughtorch.cuda.FloatTensor
.
Unfortunately it is not working. What is wrong? Thank you!
我猜您在下面的行中得到了大小不匹配的错误。在
在您的例子中,
z
是一个向量(形状Nx1
的二维张量,h
是形状NxP
的二维张量。所以,不能直接从z
中减去h
。在可以执行以下操作以避免大小不匹配错误。在
^{pr2}$这里,
z.expand(*h.size())
将通过复制列向量P
次,将张量z
从Nx1
转换为NxP
形状。在相关问题 更多 >
编程相关推荐