def iteration(X, U, V, labels, p, logger): epsilon, gamma = p.epsilon, p.gamma multi_V = p.iter_method == 'mv' t = 0 while True: new_U = solve_U(X, V, gamma, epsilon) _, converged = U_converged(new_U, U) U = new_U delta_V = 100 while delta_V > 1e-1: new_V = update_V(V, U, X, epsilon) delta_V = l21_norm(new_V - V) V = new_V if not multi_V: break metric_now = nmi_acc(U, labels) E_now = E(U, V, X, gamma, epsilon) logger.log_middle(E_now, metric_now) if converged: break t += 1 return U, V, t, metric_now
def iteration(X, U, V, labels, p, logger): from skfuzzy.cluster import cmeans V, U, _, _, _, t, _ = cmeans(X.T, len(V), 1.05, 1e-1, 200, U.T) metric_now = nmi_acc(U.T, labels) return U.T, V.T, t, metric_now
def iteration(X, U, V, labels, p, logger): t = 0 while True: new_U = update_U(X, V, U) delta, converged = U_converged(new_U, U, tol=1e-3) U = new_U V = update_V(X, U, V) metric_now = nmi_acc(U, labels) E_now = E(X, U, V) if converged: break logger.log_middle(E_now, metric_now) t += 1 return U, V, t, metric_now
def iteration(X, U, V, labels, p, logger): gamma = p.gamma t = 0 while True: new_U = update_U(X, V, gamma) delta, converged = U_converged(new_U, U) U = new_U metric_now = nmi_acc(U, labels) V = update_V(X, U, V, gamma) E_now = E(X, U, V, gamma) if converged: break logger.log_middle(E_now, metric_now) t += 1 return U, V, t, metric_now
def iteration(X, U, V, labels, p, logger): N = len(X) C = len(V) gamma, epsilon = p.gamma, p.epsilon capped = p.capped or True S = np.ones((N, C)) t = 0 while True: new_U = solve_U(S, X, V, gamma) delta, converged = U_converged(new_U, U) U = new_U V = update_V(S, U, X) S = update_S(X, V, epsilon, capped) metric_now = nmi_acc(U, labels) E_now = E(U, V, X, gamma, epsilon, capped) if converged: break logger.log_middle(E_now, metric_now) t += 1 return U, V, t, metric_now
}) ), labels ) from basics.ours._numba import solve_U as numba_solve_U, update_V as numba_update_V, E as numba_E from basics.ours._cython import update_V as cython_update_V from utils.metrics import nmi_acc functions = { 'numba_solve_U': lambda X, U, V, _: numba_solve_U(X, V, 0.7, 1e-1), 'numba_update_V': lambda X, U, V, _: numba_update_V(V, U, X, 1e-1), 'cython_update_V': lambda X, U, V, _: cython_update_V(V, U, X, 1e-1), 'numba_E': lambda X, U, V, _: numba_E(U, V, X, 0.7, 1e-1), 'nmi_acc': lambda X, U, V, labels: nmi_acc(U, labels), } if __name__ == '__main__': print('Testing on MNIST-10K...') X, U, V, labels = data() for name, function in functions.items(): print('!!!!', name, timeit( stmt='func(X, U, V, labels)', globals={ 'X': X, 'U': U,
def iteration(X, U, V, labels, p, logger): t = 0 mmax = p.mmax or 3 gamma = p.gamma epsilon = p.epsilon max_iteration = p.max_iterations or 300 # V_old = update_V(V, U, X, epsilon) U_new = U old_E = np.Infinity VAUt = None aa_ndim = reduce(operator.mul, V.shape) aa_shape = (1, aa_ndim) accelerator = None # energy = Energy() while True: U_now = solve_U(X, V_old, gamma, epsilon) _, converged = U_converged(U_now, U_new) print(_) U_new = U_now if converged: return U_new, V_old, t, nmi_acc(U_new, labels) # delta_U, is_converged = U_converged(U_new, U_old) new_E = E(U_new, V_old, X, gamma, epsilon) if new_E >= old_E: V_old = VAUt U_new = solve_U(X, V_old, gamma, epsilon) new_E = E(U_new, V_old, X, gamma, epsilon) accelerator.replace(V_old.reshape(aa_shape)) # if energy.converged(new_E): # return U_new, V_old, t, nmi_acc(U_new, labels) if t > max_iteration: return U_new, V_old, t, nmi_acc(U_new, labels) # energy.add(new_E) logger.log_middle(new_E, nmi_acc(U_new, labels)) VAUt = update_V(V_old, U_new, X, epsilon) if t == 0: accelerator = Anderson(mmax, aa_ndim, VAUt.reshape(aa_shape)) V_new = VAUt else: accelerator.set_G(VAUt.reshape(aa_shape)) V_new = accelerator.compute().reshape(V.shape) V_old = V_new # U_old = U_new old_E = new_E t += 1