emwnenmf.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import numpy as np
  2. import time
  3. def emwnenmf(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. X = data.X + np.multiply(data.nW, np.dot(G, F))
  17. FXt = np.dot(F, X.T)
  18. FFt = np.dot(F, F.T)
  19. GtX = np.dot(G.T, X)
  20. GtG = np.dot(G.T, G)
  21. GradG = np.dot(G, FFt) - FXt.T
  22. GradF = np.dot(GtG, F) - GtX
  23. init_delta = stop_rule(np.hstack((G.T, F)), np.hstack((GradG.T, GradF)))
  24. tolF = tol * init_delta
  25. tolG = tolF # Stopping tolerance
  26. # Iterative updating
  27. G = G.T
  28. GtX = np.dot(G, X) - GtG.dot(data.Phi_F)
  29. k = 0
  30. RMSE[:, k] = np.linalg.norm(F[:, 0:-1] - data.F[:, 0:-1], 2, axis=1) / np.sqrt(F.shape[1] - 1)
  31. T[k] = 0
  32. t = time.time()
  33. # Main loop
  34. while time.time() - t <= Tmax + delta_measure:
  35. # Estimation step
  36. if (k+1) % M_loop:
  37. X = data.X + np.multiply(data.nW, np.dot(G.T, F))
  38. # Maximisation step
  39. # Optimize F with fixed G
  40. np.put(F, data.idxOF, 0)
  41. F, iterF, _ = NNLS(F, GtG, GtX, ITER_MAX, tolF, data.idxOF, False)
  42. np.put(F, data.idxOF, data.sparsePhi_F)
  43. # print(F[:,0:5])
  44. if iterF <= ITER_MIN:
  45. tolF = tolF / 10
  46. # print('Tweaked F tolerance to '+str(tolF))
  47. FFt = np.dot(F, F.T)
  48. FXt = np.dot(F, X.T) - FFt.dot(data.Phi_G.T)
  49. # Optimize G with fixed F
  50. np.put(G.T, data.idxOG, 0)
  51. G, iterG, _ = NNLS(G, FFt, FXt, ITER_MAX, tolG, data.idxOG, True)
  52. np.put(G.T, data.idxOG, data.sparsePhi_G)
  53. if iterG <= ITER_MIN:
  54. tolG = tolG / 10
  55. # print('Tweaked G tolerance to '+str(tolG))
  56. GtG = np.dot(G, G.T)
  57. GtX = np.dot(G, X) - GtG.dot(data.Phi_F)
  58. if time.time() - t - k * delta_measure >= delta_measure:
  59. k = k + 1
  60. if k >= em_iter_max + 1:
  61. break
  62. RMSE[:, k] = np.linalg.norm(F[:, 0:-1] - data.F[:, 0:-1], 2, axis=1) / np.sqrt(F.shape[1] - 1)
  63. T[k] = time.time() - t
  64. return {'RMSE': RMSE, 'T': T}
  65. def stop_rule(X, GradX):
  66. # Stopping Criterions
  67. pGrad = GradX[np.any(np.dstack((X > 0, GradX < 0)), 2)]
  68. return np.linalg.norm(pGrad, 2)
  69. def NNLS(Z, GtG, GtX, iterMax, tol, idxfixed, transposed):
  70. L = np.linalg.norm(GtG, 2) # Lipschitz constant
  71. H0 = Z # Initialization
  72. Grad = np.dot(GtG, Z) - GtX # Gradient
  73. alpha1 = 1
  74. for iter in range(1, iterMax + 1):
  75. H = np.maximum(Z - (1 / L) * Grad, 0) # Calculate squence 'Y'
  76. if transposed: # If Z = G.T
  77. np.put(H.T, idxfixed, 0)
  78. else: # If Z = F
  79. np.put(H, idxfixed, 0)
  80. alpha2 = 0.5 * (1 + np.sqrt(1 + 4 * alpha1 ** 2))
  81. Z = H + ((alpha1 - 1) / alpha2) * (H - H0)
  82. alpha1 = alpha2
  83. Grad = np.dot(GtG, Z) - GtX
  84. H0 = H
  85. # Lin's stopping criteria
  86. pgn = stop_rule(Z, Grad)
  87. if pgn <= tol:
  88. break
  89. return H, iter, Grad
  90. def nmf_norm_fro(X, G, F, *args):
  91. W = args
  92. if len(W) == 0:
  93. f = np.square(np.linalg.norm(X - np.dot(G, F), 'fro')) / np.square(np.linalg.norm(X, 'fro'))
  94. else:
  95. W = W[0]
  96. f = np.square(np.linalg.norm(X - np.multiply(W, np.dot(G, F)), 'fro')) / np.square(np.linalg.norm(X, 'fro'))
  97. return f