remnenmf.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. function [ T , RMSE ] = remnenmf( X, X_theo, W, F_theo, Omega_G, Omega_F, Phi_G, Phi_F, G, F, config)
  2. %% loading the config parameters
  3. Tmax = config.Tmax;
  4. delta_measure = config.delta_measure;
  5. InnerMinIter = config.InnerMinIter;
  6. InnerMaxIter = config.InnerMaxIter;
  7. M_loop = config.M_loop;
  8. nu = config.nu;
  9. %%
  10. r = nu;
  11. X0 = X;
  12. Omega_G = (Omega_G == 1); % Logical mask is faster than indexing in matlab.
  13. Omega_F = (Omega_F == 1); % Logical mask is faster than indexing in matlab.
  14. nOmega_G = ~Omega_G; % Logical mask is faster than indexing in matlab.
  15. nOmega_F = ~Omega_F; % Logical mask is faster than indexing in matlab.
  16. [~, num_sensor] = size(F);
  17. num_sensor = num_sensor-1;
  18. em_iter_max = round(Tmax / delta_measure) ;
  19. T = nan(1,em_iter_max);
  20. RMSE = nan(1+config.numSubSensor,em_iter_max);
  21. nW = (1-W);
  22. % X = G*F+W.*(X0-G*F);
  23. X = X0 + nW.*(G*F);
  24. GG = G' * G;
  25. GX = G' * X ;
  26. GradF = GG * F - GX;
  27. FF = F * F';
  28. XF = X * F' ;
  29. GradG = nOmega_G.*(G * FF - XF);
  30. d = Grad_P([GradG',GradF],[G',F]);
  31. StoppingCritF = 1.e-3*d;
  32. StoppingCritG = 1.e-3*d;
  33. T_E = [];
  34. T_M = [];
  35. tic
  36. i = 1;
  37. niter = 0;
  38. RMSE(:,i) = vecnorm(F(:,1:end-1)- F_theo(:,1:end-1),2,2)/sqrt(num_sensor);
  39. T(i) = toc;
  40. while toc<Tmax
  41. t_e = toc;
  42. % Estimation step
  43. X = X0 + nW.*(G*F);
  44. % if i>1
  45. % [L,R]=RSI_compression(X,r,L,R);
  46. % else
  47. % [L,R]=RSI_compression(X,r);
  48. % end
  49. [L,R]=RSI_compression(X,r);
  50. % Compress left and right
  51. X_L = L * X;
  52. X_R = X * R;
  53. T_E = cat(1,T_E,toc - t_e);
  54. % Maximization step
  55. for j =1:M_loop
  56. t_m = toc;
  57. % F_R = F * R;
  58. % FF = F_R * F_R';
  59. FF = F * F';
  60. XF = X_R * (F * R)' - Phi_G * FF;
  61. % G(Omega_G) = 0; % Convert G to \Delta G
  62. [ GradG , iterG ] = MaJ_G_EM_NeNMF( FF , XF , GradG , InnerMinIter , InnerMaxIter , StoppingCritG , nOmega_G); % Update \Delta G
  63. % G(Omega_G) = Phi_G(Omega_G); % Convert \Delta G to G
  64. G = GradG + Phi_G;
  65. niter = niter + iterG;
  66. if(iterG<=InnerMinIter)
  67. StoppingCritG = 1.e-1*StoppingCritG;
  68. end
  69. % G_L = L * G;
  70. % GG = G_L' * G_L;
  71. GG = G' * G;
  72. GX = (L * G)' * X_L - GG * Phi_F;
  73. F(Omega_F) = 0; % Convert F to \Delta F
  74. % F = F - Phi_F;
  75. [ F , iterF ] = MaJ_F_EM_NeNMF( GG , GX , F , InnerMinIter , InnerMaxIter , StoppingCritF , nOmega_F); % Update \Delta F
  76. F(Omega_F) = Phi_F(Omega_F); % Convert \Delta F to F
  77. % F = F + Phi_F;
  78. niter = niter + iterF;
  79. if(iterF<=InnerMinIter)
  80. StoppingCritF = 1.e-1*StoppingCritF;
  81. end
  82. if toc - i*delta_measure >= delta_measure
  83. i = i+1;
  84. if i > em_iter_max
  85. break
  86. end
  87. T(i) = toc;
  88. RMSE(:,i) = vecnorm(F(:,1:end-1) - F_theo(:,1:end-1),2,2)/sqrt(num_sensor);
  89. end
  90. T_M = cat(1,T_M,toc - t_m);
  91. end
  92. end
  93. niter
  94. disp(['rem E step : ',num2str(mean(T_E))])
  95. disp(['rem M step : ',num2str(mean(T_M))])
  96. end
  97. function [ L,R ] = RSI_compression(X,r,varargin)
  98. % Tepper, M., & Sapiro, G. (2016). Compressed nonnegative
  99. % matrix factorization is fast and accurate. IEEE Transactions
  100. % on Signal Processing, 64(9), 2269-2283.
  101. % see: https://arxiv.org/pdf/1505.04650
  102. % The corresponding code is originally created by the authors
  103. % Then, it is modified by F. Yahaya.
  104. % Date: 13/04/2018
  105. %
  106. compressionLevel=2;
  107. [m,n]=size(X);
  108. l = min(min(n,m), max(compressionLevel, r ));
  109. switch nargin
  110. case 2
  111. OmegaL = randn(n,l);
  112. OmegaR = randn(l, m);
  113. q = 4;
  114. case 4
  115. OmegaL = varargin{2};
  116. OmegaR = varargin{1};
  117. q = 1;
  118. end
  119. Y = X * OmegaL;
  120. for i=1:q
  121. [Y,~]=qr(Y,0);
  122. S=X'*Y;
  123. [Z,~]=qr(S,0);
  124. Y=X* Z;
  125. end
  126. [L,~]=qr(Y,0);
  127. L=L';
  128. Y = OmegaR * X;
  129. for i=1:q
  130. [Y,~]=qr(Y',0);
  131. S=X*Y;
  132. [Z,~]=qr(S,0);
  133. Y=Z'*X;
  134. end
  135. Y=Y';
  136. [R,~] = qr(Y,0);
  137. end