2016-05-11 3 views
0

私は期待値最大化アルゴリズムをmatlabに実装しています。アルゴリズムは214096 x 2のデータ行列で動作していますが、計算の際には(214096 x 2)*(2 x 2)*(2 x 214096)の行列が乗算され、メモリ不足のエラーが発生します。この問題を解決する方法はありますか?期待値最大化アルゴリズムmatlabメモリ不足エラー

Equation

MATLABコード:それは

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

がどのように機能するか

  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

あなたが単に行列のためのベクトルを代入することにより、ベクトルベースの方程式を適用しようとしたようだが、これではありませんmahalanobis norm of(x-mu)とし、行列の各行ごとにこの値を取得したいとします。このようにX、

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

、今あなたがのポイントワイズ乗算を行う必要がある(X - ミュー)、ない内積、そして最後に、第2の軸(列)の上に、あなたが終わるこの方法をまとめますN要素ベクトル、それぞれXの対応する行のマハラノビスノルムを含んでいます。

+0

ありがとう@lejlot、これを修正しています – Umar