我是矢量化代码的新手,我真的很兴奋一切都快了多少,但我无法从这段特定的代码中获得高速...
这里是住房类...
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 是灰度图像的 nxn 矩阵):
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.image_matrix 的第一个单元格:
return np.log((mc*(1.0/(std*np.sqrt(2.0*np.pi)))*(np.exp(-((self.image_matrix[0,0]-mean)**2.0)/(2.0*var)))).sum())
# ^^^^^
这也是它完全错误的原因。它实际上根本不是 self.image_matrix 上的矢量化计算。不要试图用它的运行时间作为比较点;你总是可以比正确的代码更快地编写错误的代码。
通过消除 np.vectorize 的使用,您可以使第一个版本更快,但不如错误代码快。 log 中的 sum 只需要指定适当的轴:
def likelihood(self):
def f(x):
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(axis=0))
log_likelihood = (f(self.image_matrix)).sum()
这可以通过几种方式进一步简化和优化。例如,可以消除嵌套函数,乘以 1.0/whatever 比除以 whatever 慢,但消除 np.vectorize 是大事。
