期望最大化算法(GMM-EM)无法找到正确的参数。(高斯混合模型)

3
我正在尝试学习高斯混合模型中参数估计的期望最大化算法(1D)。然而,这个算法似乎很少能找到正确的参数。我想知道我是否做错了什么。
数据是由三个高斯分布在三个不同的位置生成的(x=-10,x=5和x=10)。
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

# dataset is generated with 3 gaussians at with mean at -10, 10 and 5. 
x1 = 1.0 * np.random.randn(10000) - 10.0
x2 = 1.0 * np.random.randn(10000) + 10.0
x3 = 1.0 * np.random.randn(10000) + 5.0
x = np.hstack([x1,x2,x3]) # final data set

我检查了直方图,x是正确的。参数学习使用EM更新完成:

# model and initialization
M = 3 # number of mixtures
alpha = np.ones(M)*.5 # -> likelihood of the mixture
mu = np.random.random(M)*10 # -> mean of the gaussian
sigma = np.ones(M)*1.0 # -> std of the gaussian

w_mt = np.zeros((M,len(x))) # -> q(mixture | data, parameter)

# EM
for i in range(100):
    print "alpha:", alpha, "mu:", mu, "sigma:", sigma

    # E-step
    for m in range(M):
        w_mt[m] = alpha[m] * mlab.normpdf(x,mu[m],sigma[m])
    C = np.sum(w_mt, axis=0) # normalization
    w_mt = w_mt / C

    # M-step
    alpha = np.sum(w_mt,axis=1) / len(x)
    mu = np.sum(w_mt*x,axis=1)/np.sum(w_mt,axis=1)
    sigma = np.sum(w_mt*pow(x - mu[:,np.newaxis],2),axis=1) / np.sum(w_mt,axis=1)

    sigma[sigma < 0.1] = 0.1 # avoid numerical problems

我期望算法(至少有时)能够找到正确的mu(即-10、5、10),其标准差为约1.0。然而,似乎该算法从未能做到这一点。如有任何帮助,将不胜感激。
更新:Ted Kim的修复似乎已经解决了这个问题。我在计算std时忘记取平方根了。如果有人感兴趣,这里是更新后的代码链接:链接

如果你有时间,我使用了很多你的代码,但遇到了一个问题。我在这里发布了:https://stackoverflow.com/questions/47187037/cannot-get-expectation-maximization-to-work - Adam_G
2个回答

4

sigma是标准差,但你代码中的sigma指的是方差(即sigma ** 2)。

尝试一下:

sigma = np.sqrt(np.sum(w_mt*pow(x - mu[:,np.newaxis],2),axis=1) / np.sum(w_mt,axis=1))

0

这也可以通过将平均值 mu 视为向量和协方差矩阵 Sigma 而不是方差来推广到更高维度。

例如,第 k 个高斯的协方差矩阵 Sigma_k 可以在 EMM 步中使用 MLE 计算出来,类似于以下内容,其中 phi [i,k] 表示计算在 EM 算法早期的 E 步中第 i 个数据点属于第 k 个聚类中心的概率。

Sigma [k,:,:] = sum([phi [i,k] * np.outer(X [i,:] - mu [k,:],X [i,:] - mu [k,:])for i in range(n)]) / n_k [k]

下图显示了具有 k=3 聚类中心的 GMM-EM 软查找方式:

enter image description here

更多细节,我们可以参考this博客文章。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接