A0 /= A0.sum(axis=1)[:, None] S0 = np.random.rand(k, n) # mixture model: amplitudes positive # and sum up to one at every pixel pA = partial(proxmin.operators.prox_unity_plus, axis=1) pS = proxmin.operators.prox_plus if problem == "nmf": prox = [pS, pS] elif problem == "mixmf": prox = [pA, pS] grad = partial(proxmin.nmf.grad_likelihood, Y=Y) traceback = Traceback() all_args = {"prox": prox, "max_iter": 1000, "callback": traceback, "e_rel": 1e-4} b1 = 0.9 b2 = 0.999 pgm_args = {"accelerated": True} adaprox_args = {"b1": b1, "b2": b2, "prox_max_iter": 100} runs = ( (proxmin.pgm, dict(all_args, **pgm_args), "PGM"), (proxmin.adaprox, dict(all_args, **adaprox_args, scheme="adam"), "Adam"), ( proxmin.adaprox, dict(all_args, **adaprox_args, scheme="padam", p=0.125), "PAdam", ), (proxmin.adaprox, dict(all_args, **adaprox_args, scheme="amsgrad"), "AMSGrad"), )
np.random.seed(101) # set up test data trueA = np.array([generateAmplitudes(k) for i in range(b)]) trueS = np.array([generateComponent(n) for i in range(k)]) trueY = np.dot(trueA, trueS) Y = add_noise(trueY, noise) # if noise is variable, specify variance matrix of the same shape as Y W = None # initialize and run NMF A0 = np.array([generateAmplitudes(k) for i in range(b)]) S0 = np.array([generateComponent(n) for i in range(k)]) p1 = partial(po.prox_unity_plus, axis=1) proxs_g = [[p1], None] tr = Traceback(2) A, S = nmf(Y, A0, S0, W=W, prox_A=p1, e_rel=1e-6, e_abs=1e-6 / noise**2, traceback=tr) # sort components to best match inputs A, S = match(A, S, trueS) # show data and model fig = plt.figure(figsize=(6, 7)) ax = fig.add_subplot(311) ax.set_title("True Components S")
if len(sys.argv)==2: boundary = sys.argv[1] if boundary not in ["line", "circle"]: raise ValueError("Expected either 'line' or 'circle' as an argument") else: boundary = "circle" max_iter = 100 # step sizes and proximal operators for boundary step_f = steps_f12() prox_g = partial(prox_lim, boundary=boundary) prox_gradf_ = partial(prox_gradf_lim, boundary=boundary) # PGM without boundary tr = Traceback() xy = xy0.copy() pa.pgm(xy, prox_gradf, step_f, max_iter=max_iter, relax=1, traceback=tr) plotResults(tr, "PGM no boundary") # PGM tr = Traceback() xy = xy0.copy() pa.pgm(xy, prox_gradf_, step_f, max_iter=max_iter, traceback=tr) plotResults(tr, "PGM", boundary=boundary) # APGM tr = Traceback() xy = xy0.copy() pa.pgm(xy, prox_gradf_, step_f, max_iter=max_iter, accelerated=True, traceback=tr) plotResults(tr, "PGM accelerated", boundary=boundary)