我是新来的向量化代码,我真的很高兴一切都快多少,但我无法从这段代码中获得高速...向量化数组的函数向量化
这里是住宅类...
class GaussianMixtureModel:
def __init__(self, image_matrix, num_components, means=None):
self.image_matrix = image_matrix
self.num_components = num_components
if(means is None):
self.means = np.zeros(num_components)
else:
self.means = np.array(means)
self.variances = np.zeros(num_components)
self.mixing_coefficients = np.zeros(num_components)
这里就是我有这么远的作品:
def likelihood(self):
def g2(x):
#N =~ 5
#self.mixing_coefficients = 1D, N items
#self.variances = 1D, N items
#self.means = 1D, N items
mc = self.mixing_coefficients[:,None,None]
std = self.variances[:,None,None] ** 0.5
var = self.variances[:,None,None]
mean = self.means[:,None,None]
return np.log((mc*(1.0/(std*np.sqrt(2.0*np.pi)))*(np.exp(-((x-mean)**2.0)/(2.0*var)))).sum())
f = np.vectorize(g2)
#self.image_matrix =~ 400*700 2D matrix
log_likelihood = (f(self.image_matrix)).sum()
return log_likelihood
这里就是我有,给出了一个奇怪的结果(注意self.image_matrix
是灰度图像的N×N的矩阵):
def likelihood(self):
def g2():
#N =~ 5
#self.mixing_coefficients = 1D, N items
#self.variances = 1D, N items
#self.means = 1D, N items
#self.image_matrix = 1D, 400x700 2D matrix
mc = self.mixing_coefficients[:,None,None]
std = self.variances[:,None,None] ** 0.5
var = self.variances[:,None,None]
mean = self.means[:,None,None]
return np.log((mc*(1.0/(std*np.sqrt(2.0*np.pi)))*(np.exp(-((self.image_matrix-mean)**2.0)/(2.0*var)))).sum())
log_likelihood = (g2()).sum()
return log_likelihood
然而,第二个版本是真的快速相比第一(这需要10秒左右...和速度是非常重要的位置,因为这是收敛算法的一部分)
有没有办法复制第一个版本的结果和第二个速度? (我真的不与矢量化不够熟悉,知道为什么第二个版本是不工作)
其他数组涉及的维度是什么? 'self.mixing_coefficients','self.variances'和'self.means'? (“自我”来自哪里?) – user2357112
我会编辑答案,以包括昏暗,但整体类是巨大的......我会尽力获得重要的东西 – bordeo
在你的第二个版本中,你似乎只是使用图像矩阵中的左上角元素('self.image_matrix [0,0]') –