0

我正在 matlab 中实现期望最大化算法。算法在 214096 x 2 数据矩阵上运行,并且在计算概率时,存在 (214096 x 2) * (2 x 2) * (2 x 214096) 矩阵的乘法,这导致 matlab 中内存不足的错误。有没有办法解决这个问题?

方程

Matlab代码:

          enter image description here  D = size(X,2); % dimension
            N = size(X,1); % number of samples
            K = 4; % number of Gaussian Mixture components ( Also number of clusters )

            % Initialization
            p = [0.2, 0.3, 0.2, 0.3]; % arbitrary pi, probabilities of clusters, apriori probability of cluster
            [idx,mu] = kmeans(X,K); % initial means of the components, theta is mu and variance

            % compute the covariance of the components
            sigma = zeros(D,D,K);
            for k = 1:K
                tempmat = X(idx==k,:);
                sigma(:,:,k) = cov(tempmat);  % Sigma j
                sigma_det(k) = det(sigma(:,:,k));
            end

            % calculate x-mu
            for k=1: K
                            check=length( X(idx == k,1))
                            for  lidx = 1: length( X(idx == k,1))

                                cidx = find( idx == k) ;
                                Xmu(cidx(lidx),:) = X(cidx(lidx),:) - mu(k,:); %( x-mu ) calculation on cluster level
                            end
            end


            % compute P(Cj|x; theta(t)), and take log to simplified calculation

            %Eq 14.14 denominator 
            denom = 0;
            for k=1:K
                calc_sigma_1_2 = sigma_det(k)^(-1/2);
                calc_x_mu = Xmu(idx == k,:);
                calc_sigma_inv = inv(sigma(:,:,k)); 
                calc_x_mu_tran = calc_x_mu.';
                factor = calc_sigma_1_2 * exp (-1/2 * calc_x_mu * calc_sigma_inv * calc_x_mu_tran  ) * p(k);

                denom = denom + factor;
            end


            for k =1:K 
                calc_sigma_1_2 = sigma_det(k)^(-1/2);
                calc_x_mu = Xmu(idx == k,:);
                calc_sigma_inv = inv(sigma(:,:,k)); 
                calc_x_mu_tran = calc_x_mu.';
                factor = calc_sigma_1_2 * exp (-1/2 * calc_x_mu_tran * calc_sigma_inv * calc_x_mu ) * p(k);

                pdf(k) = factor/denom;
            end

            %%%% Equation 14.14 ends
4

1 回答 1

0

您似乎试图通过简单地将向量替换为矩阵来应用基于向量的方程,这不是它的工作原理

(x - mu).' * Inv(sigma) * (x-mu) 

应该是(x-mu) 的马氏范数,并且您希望在矩阵 X 的每一行中获得该值,因此

(X - mu).' * Inv(sigma) =: A <- this is ok, this results in N x d matrix

现在你必须对 A 与 (X - mu) 进行逐点乘法,而不是点积,最后在第二个轴(列)上求和,这样你就得到了N元素向量,每个向量都包含一个对应的马氏范数从 X 开始的行。

于 2016-05-12T00:10:37.283 回答