EM アルゴリズムを使用して、特定のデータセットで 4 つのコンポーネントを含むガウス混合モデルをトレーニングしたいと考えています。このセットは 3 次元で、300 個のサンプルが含まれています。
rank(sigma) = 2
問題は、EM アルゴリズムの約 6 ラウンド後、共分散行列 sigma が matlab ( 3 ではなく)に従って特異値に近くなることです。これは、ガウス分布を評価する複雑な値のような望ましくない結果につながりますgm(k,i)
。
さらに、ガウスのログを使用してアンダーフローの問題を説明しました - E-step を参照してください。これが正しいかどうかはわかりませんが、責任 p(w_k | x^(i), theta) の exp を別の場所に持っていく必要があるかどうかはわかりません。
私の EM アルゴリズムの実装がこれまでのところ正しいかどうか教えてもらえますか? そして、特異共分散シグマに近い問題をどのように説明するのでしょうか?
これが EM アルゴリズムの私の実装です。
最初に、kmeans を使用してコンポーネントの平均と共分散を初期化しました。
load('data1.mat');
X = Data'; % 300x3 data set
D = size(X,2); % dimension
N = size(X,1); % number of samples
K = 4; % number of Gaussian Mixture components
% Initialization
p = [0.2, 0.3, 0.2, 0.3]; % arbitrary pi
[idx,mu] = kmeans(X,K); % initial means of the components
% compute the covariance of the components
sigma = zeros(D,D,K);
for k = 1:K
sigma(:,:,k) = cov(X(idx==k,:));
end
E-stepでは、次の式を使用して責任を計算しています。
w_k は k ガウス成分です。
x^(i) は単一のデータポイント (標本)
theta は、ガウス混合モデルのパラメータを表します: mu、Sigma、pi。
対応するコードは次のとおりです。
% variables for convergence
converged = 0;
prevLoglikelihood = Inf;
prevMu = mu;
prevSigma = sigma;
prevPi = p;
round = 0;
while (converged ~= 1)
round = round +1
gm = zeros(K,N); % gaussian component in the nominator
sumGM = zeros(N,1); % denominator of responsibilities
% E-step: Evaluate the responsibilities using the current parameters
% compute the nominator and denominator of the responsibilities
for k = 1:K
for i = 1:N
Xmu = X-mu;
% I am using log to prevent underflow of the gaussian distribution (exp("small value"))
logPdf = log(1/sqrt(det(sigma(:,:,k))*(2*pi)^D)) + (-0.5*Xmu*(sigma(:,:,k)\Xmu'));
gm(k,i) = log(p(k)) * logPdf;
sumGM(i) = sumGM(i) + gm(k,i);
end
end
% calculate responsibilities
res = zeros(K,N); % responsibilities
Nk = zeros(4,1);
for k = 1:K
for i = 1:N
% I tried to use the exp(gm(k,i)/sumGM(i)) to compute res but this leads to sum(pi) > 1.
res(k,i) = gm(k,i)/sumGM(i);
end
Nk(k) = sum(res(k,:));
end
Nk(k)
は、M ステップで指定された式を使用して計算され、M ステップで新しい確率を計算するために使用されp(k)
ます。
Mステップ
% M-step: Re-estimate the parameters using the current responsibilities
for k = 1:K
for i = 1:N
mu(k,:) = mu(k,:) + res(k,i).*X(k,:);
sigma(:,:,k) = sigma(:,:,k) + res(k,i).*(X(k,:)-mu(k,:))*(X(k,:)-mu(k,:))';
end
mu(k,:) = mu(k,:)./Nk(k);
sigma(:,:,k) = sigma(:,:,k)./Nk(k);
p(k) = Nk(k)/N;
end
収束を確認するために、次の式を使用して対数尤度が計算されます。
% Evaluate the log-likelihood and check for convergence of either
% the parameters or the log-likelihood. If not converged, go to E-step.
loglikelihood = 0;
for i = 1:N
loglikelihood = loglikelihood + log(sum(gm(:,i)));
end
% Check for convergence of parameters
errorLoglikelihood = abs(loglikelihood-prevLoglikelihood);
if (errorLoglikelihood <= eps)
converged = 1;
end
errorMu = abs(mu(:)-prevMu(:));
errorSigma = abs(sigma(:)-prevSigma(:));
errorPi = abs(p(:)-prevPi(:));
if (all(errorMu <= eps) && all(errorSigma <= eps) && all(errorPi <= eps))
converged = 1;
end
prevLoglikelihood = loglikelihood;
prevMu = mu;
prevSigma = sigma;
prevPi = p;
end % while
ガウス混合モデルの EM アルゴリズムの Matlab 実装に何か問題がありますか?