emwnenmf_m.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import numpy as np
  2. import time
  3. def emwnenmf_m(data, G, F, r, Tmax):
  4. tol = 1e-3
  5. delta_measure = 1
  6. em_iter_max = round(Tmax / delta_measure) + 1 #
  7. T = np.empty(shape=(em_iter_max + 1))
  8. T.fill(np.nan)
  9. RMSE = np.empty(shape=(2, em_iter_max + 1))
  10. RMSE.fill(np.nan)
  11. # RRE = np.empty(shape=(em_iter_max + 1))
  12. # RRE.fill(np.nan)
  13. M_loop = 2 # Number of passage over M step
  14. ITER_MAX = 100 # maximum inner iteration number (Default)
  15. ITER_MIN = 5 # minimum inner iteration number (Default)
  16. dataX = data.X
  17. dataF = data.F
  18. dataidxOF = data.idxOF
  19. dataidxOG = data.idxOG
  20. datanW = data.nW
  21. dataPhi_F = data.Phi_F
  22. dataPhi_G = data.Phi_G
  23. datasparsePhi_F = data.sparsePhi_F
  24. datasparsePhi_G = data.sparsePhi_G
  25. X = dataX + np.multiply(datanW, np.dot(G, F))
  26. XFt = np.dot(X, F.T)
  27. FFt = np.dot(F, F.T)
  28. GtX = np.dot(G.T, X)
  29. GtG = np.dot(G.T, G)
  30. GradG = np.dot(G, FFt) - XFt
  31. GradF = np.dot(GtG, F) - GtX
  32. init_delta = stop_rule(np.hstack((G.T, F)), np.hstack((GradG.T, GradF)))
  33. tolF = tol * init_delta
  34. tolG = tolF # Stopping tolerance
  35. # Iterative updating
  36. k = 0
  37. RMSE[:, k] = np.linalg.norm(F[:, 0:-1] - dataF[:, 0:-1], 2, axis=1) / np.sqrt(F.shape[1] - 1)
  38. T[k] = 0
  39. t = time.time()
  40. niter = 0
  41. # Main loop
  42. while time.time() - t <= Tmax + delta_measure:
  43. # Estimation step
  44. X = dataX + np.multiply(datanW, np.dot(G, F))
  45. # Maximisation step
  46. for _ in range(M_loop):
  47. FFt = F.dot(F.T)
  48. XFt = X.dot(F.T) - dataPhi_G.dot(FFt)
  49. np.put(G, dataidxOG, 0)
  50. G, iterG = maj_G(G, FFt, XFt, ITER_MAX, tolG, dataidxOG)
  51. np.put(G, dataidxOG, datasparsePhi_G)
  52. if iterG <= ITER_MIN:
  53. tolG = 0.1 * tolG
  54. niter = niter + iterG
  55. GtG = G.T.dot(G)
  56. GtX = G.T.dot(X) - GtG.dot(dataPhi_F)
  57. np.put(F, dataidxOF, 0)
  58. F, iterF = maj_F(F, GtG, GtX, ITER_MAX, tolF, dataidxOF)
  59. np.put(F, dataidxOF, datasparsePhi_F)
  60. if iterF <= ITER_MIN:
  61. tolF = 0.1 * tolF
  62. niter = niter + iterF
  63. if time.time() - t - k * delta_measure >= delta_measure:
  64. k = k + 1
  65. if k >= em_iter_max + 1:
  66. break
  67. RMSE[:, k] = np.linalg.norm(F[:, 0:-1] - dataF[:, 0:-1], 2, axis=1) / np.sqrt(F.shape[1] - 1)
  68. T[k] = time.time() - t
  69. print(niter)
  70. return {'RMSE': RMSE, 'T': T}
  71. def stop_rule(X, GradX):
  72. # Stopping Criterions
  73. pGrad = GradX[np.any(np.dstack((X > 0, GradX < 0)), 2)]
  74. return np.linalg.norm(pGrad)
  75. def maj_G(G1, FFt, XFt, ITER_MAX, tolG, idxOG):
  76. Y = G1
  77. alpha1 = 1
  78. L = np.linalg.norm(FFt)
  79. Grad_y = Y.dot(FFt) - XFt
  80. for i in range(1, ITER_MAX + 1):
  81. G2 = np.maximum(Y - (1 / L) * Grad_y, 0)
  82. np.put(G2, idxOG, 0)
  83. alpha2 = (1 + np.sqrt(4 * alpha1 ** 2 + 1)) / 2
  84. Y = G2 + ((alpha1 - 1) / alpha2) * (G2 - G1)
  85. Grad_y = Y.dot(FFt) - XFt
  86. G1 = G2
  87. alpha1 = alpha2
  88. if stop_rule(Y, Grad_y) <= tolG:
  89. break
  90. return G1, i
  91. def maj_F(F1, GtG, GtX, ITER_MAX, tolF, idxOF):
  92. Y = F1
  93. alpha1 = 1
  94. L = np.linalg.norm(GtG)
  95. Grad_y = GtG.dot(Y) - GtX
  96. for i in range(1, ITER_MAX + 1):
  97. F2 = np.maximum(Y - (1 / L) * Grad_y, 0)
  98. np.put(F2, idxOF, 0)
  99. alpha2 = (1 + np.sqrt(4 * alpha1 ** 2 + 1)) / 2
  100. Y = F2 + ((alpha1 - 1) / alpha2) * (F2 - F1)
  101. Grad_y = GtG.dot(Y) - GtX
  102. F1 = F2
  103. alpha1 = alpha2
  104. if stop_rule(Y, Grad_y) <= tolF:
  105. break
  106. return F1, i