2016-05-11 41 views
0

我在matlab中實現期望最大化算法。算法在214096 x 2數據矩陣上運行,並且在計算概率時,存在(214096 x 2)*(2 x 2)*(2 x 214096)矩陣的乘法,這會導致matlab內存不足。有沒有辦法解決這個問題?期望最大化算法matlab內存不足錯誤

Equation

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 
+0

是214096維數/特徵的數量? – lejlot

+0

214096是2個維度中每個維度的觀察次數 – Umar

+0

在EM算法中,您會得到N^2個元素的矩陣嗎?它似乎不正確。你爲什麼需要Gramian? – lejlot

回答

0

看來你試圖通過簡單的矩陣替代向量應用基於矢量方程,這是不是它是如何工作

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

是應該是mahalanobis(x-mu)的範數,並且您想要獲得每行矩陣的這個值X,從而

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

,現在你要做的逐點乘法與(X - 畝),而不是一個點的產品,最後總結了第二軸(列),這樣一來你結束與N元素矢量相關,每個元素包含從X起對應行的mahalanobis範數。

+0

非常感謝@lejlot,即時修復這個問題 – Umar