def show_matrices_recovered(W_r, H_r, W, H, cfg=config.default_config(), permute=True): if permute: idx = get_permute(W_r, H_r, W, H, cfg["munkres"]) else: idx = np.array([range(W.shape[1]), range(W.shape[1])]) # f, axarr = plt.subplots(nrows=1, ncols=1) # axarr[0, 0].imshow(1-W_r, cmap='gray') # axarr[0, 0].set_title('W real') # axarr[0, 1].imshow(1-H_r, cmap='gray') # axarr[0, 1].set_title('H real') plt.matshow(1 - W[:, idx[:, 1]], cmap=plt.cm.gray)
def show_matrices_recovered(W_r, H_r, W, H, cfg=config.default_config(), permute=True): if permute: idx = get_permute(W_r, H_r, W, H, cfg['munkres']) else: idx = np.array([range(W.shape[1]), range(W.shape[1])]) #f, axarr = plt.subplots(nrows=1, ncols=1) #axarr[0, 0].imshow(1-W_r, cmap='gray') #axarr[0, 0].set_title('W real') #axarr[0, 1].imshow(1-H_r, cmap='gray') #axarr[0, 1].set_title('H real') plt.matshow(1-W[:, idx[:, 1]], cmap=plt.cm.gray)
def run(F, Phi, Theta, Phi_r=None, Theta_r=None, cfg=config.default_config()): """Em-algo method. - Return: val hdist it Phi Theta status - Used params: """ #F_norm = normalize_cols(F) T = Theta.shape[0] eps = cfg['eps'] schedule = cfg['schedule'].split(',') meas = cfg['measure'].split(',') val = np.zeros((cfg['max_iter']+2, len(meas))) hdist = np.zeros((2, cfg['max_iter']+2))#Phi - first row, Theta - second for i, fun_name in enumerate(meas): fun = getattr(measure, fun_name) val[0, i] = fun(F, np.dot(Phi, Theta)) if cfg['compare_real']: #m = Munkres() idx = get_permute(Phi_r, Theta_r, Phi, Theta, cfg['munkres']) hdist[0][0] = hellinger(Phi[:, idx[:, 1]], Phi_r[:, idx[:, 0]]) hdist[1][0] = hellinger(Theta[idx[:, 1],:], Theta_r[idx[:, 0],:]) if cfg['print_lvl'] > 1: print('Initial loss:', val[0]) status = 0 methods_num = len(schedule) it = -1 for it in range(cfg['max_iter']+1): if cfg['print_lvl'] > 1: print('Iteration', it+1) ####Phi_old = deepcopy(Phi) ####Theta_old = deepcopy(Theta) method_name = schedule[it % methods_num] if cfg['print_lvl'] > 1: print('Method:', method_name) method = getattr(methods, method_name) (Phi, Theta) = method(F, Phi, Theta, method_name, cfg) #jogging of weights if cfg['jogging'] == 1 and it < 10: joh_alpha = 0.25 cfg['phi_sparsity'] = 0.05 cfg['theta_sparsity'] = 0.1 Phi_jog, Theta_jog = gen_init(cfg) Phi = (1-joh_alpha**(it+1))*Phi + joh_alpha**(it+1)*Phi_jog Theta = (1-joh_alpha**(it+1))*Theta + joh_alpha**(it+1)*Theta_jog for j, fun_name in enumerate(meas): fun = getattr(measure, fun_name) val[it+1, j] = fun(F, np.dot(Phi, Theta))#fun(F_norm, np.dot(Phi, Theta)) if cfg['compare_real']: idx = get_permute(Phi_r, Theta_r, Phi, Theta, cfg['munkres']) hdist[0][it+1] = hellinger(Phi[:, idx[:, 1]], Phi_r[:, idx[:, 0]]) hdist[1][it+1] = hellinger(Theta[idx[:, 1], :], Theta_r[idx[:, 0], :]) if cfg['print_lvl'] > 1: print(val[it+1]) if all(val[it, :] < eps): if cfg['print_lvl'] > 1: print('By cost.') status = 1 break '''if abs(Phi_old - Phi).max() < eps and abs(Theta_old - Theta).max() < eps: if cfg['print_lvl'] > 1: print('By argument.') status = 2 break''' #del W_old #del H_old if cfg['print_lvl'] > 1: print('Final:') #Phi = normalize_cols(Phi) #Theta = normalize_cols(Theta) #for j, fun_name in enumerate(meas): # fun = getattr(measure, fun_name) # val[it+2:, j] = fun(F, np.dot(Phi, Theta))#fun(F_norm, np.dot(Phi, Theta)) #if cfg['compare_real']: # idx = get_permute(Phi_r, Theta_r, Phi, Theta, cfg['munkres']) # hdist[0][it+2:] = hellinger(Phi[:, idx[:, 1]], Phi_r[:, idx[:, 0]]) # hdist[1][it+2:] = hellinger(Theta[idx[:, 1],:], Theta_r[idx[:, 0], :]) return (val, hdist, it, Phi, Theta, status)
def run(V, W, H, W_r=None, H_r=None, cfg=config.default_config()): T = H.shape[0] eps = cfg['eps'] schedule = cfg['schedule'].split(',') meas = cfg['measure'].split(',') val = np.zeros((cfg['max_iter'] + 2, len(meas))) hdist = np.zeros((cfg['max_iter'] + 2, 1)) for i, fun_name in enumerate(meas): fun = getattr(measure, fun_name) val[0, i] = fun(V, np.dot(W, H)) if cfg['compare_real']: #m = Munkres() idx = get_permute(W_r, H_r, W, H, cfg['munkres']) hdist[0] = hellinger(W[:, idx[:, 1]], W_r[:, idx[:, 0]]) / T if cfg['print_lvl'] > 1: print('Initial loss:', val[0]) status = 0 methods_num = len(schedule) it = -1 for it in range(cfg['max_iter']): if cfg['print_lvl'] > 1: print('Iteration', it + 1) W_old = deepcopy(W) H_old = deepcopy(H) method_name = schedule[it % methods_num] if cfg['print_lvl'] > 1: print('Method:', method_name) method = getattr(methods, method_name) (W, H) = method(V, W, H, method_name, cfg) if (it + 1) % cfg['normalize_iter'] == 0: W = normalize_cols(W) H = normalize_cols(H) for j, fun_name in enumerate(meas): fun = getattr(measure, fun_name) val[it + 1, j] = fun(V, np.dot(W, H)) if cfg['compare_real']: idx = get_permute(W_r, H_r, W, H, cfg['munkres']) hdist[it + 1] = hellinger(W[:, idx[:, 1]], W_r[:, idx[:, 0]]) / T if cfg['print_lvl'] > 1: print(val[it + 1]) if all(val[it, :] < eps): if cfg['print_lvl'] > 1: print('By cost.') status = 1 break if abs(W_old - W).max() < eps and abs(H_old - H).max() < eps: if cfg['print_lvl'] > 1: print('By argument.') status = 2 break #del W_old #del H_old if cfg['print_lvl'] > 1: print('Final:') W = normalize_cols(W) H = normalize_cols(H) for j, fun_name in enumerate(meas): fun = getattr(measure, fun_name) val[it + 2:, j] = fun(V, np.dot(W, H)) if cfg['compare_real']: idx = get_permute(W_r, H_r, W, H, cfg['munkres']) hdist[it + 2:] = hellinger(W[:, idx[:, 1]], W_r[:, idx[:, 0]]) / T return (val, hdist, it, W, H, status)
def run(V, W, H, W_r=None, H_r=None, cfg=config.default_config()): T = H.shape[0] eps = cfg['eps'] schedule = cfg['schedule'].split(',') meas = cfg['measure'].split(',') val = np.zeros((cfg['max_iter']+2, len(meas))) hdist = np.zeros((cfg['max_iter']+2, 1)) for i, fun_name in enumerate(meas): fun = getattr(measure, fun_name) val[0, i] = fun(V, np.dot(W, H)) if cfg['compare_real']: #m = Munkres() idx = get_permute(W_r, H_r, W, H, cfg['munkres']) hdist[0] = hellinger(W[:, idx[:, 1]], W_r[:, idx[:, 0]]) / T if cfg['print_lvl'] > 1: print('Initial loss:', val[0]) status = 0 methods_num = len(schedule) it = -1 for it in range(cfg['max_iter']): if cfg['print_lvl'] > 1: print('Iteration', it+1) W_old = deepcopy(W) H_old = deepcopy(H) method_name = schedule[it % methods_num] if cfg['print_lvl'] > 1: print('Method:', method_name) method = getattr(methods, method_name) (W, H) = method(V, W, H, method_name, cfg) if (it+1) % cfg['normalize_iter'] == 0: W = normalize_cols(W) H = normalize_cols(H) for j, fun_name in enumerate(meas): fun = getattr(measure, fun_name) val[it+1, j] = fun(V, np.dot(W, H)) if cfg['compare_real']: idx = get_permute(W_r, H_r, W, H, cfg['munkres']) hdist[it+1] = hellinger(W[:, idx[:, 1]], W_r[:, idx[:, 0]]) / T if cfg['print_lvl'] > 1: print(val[it+1]) if all(val[it, :] < eps): if cfg['print_lvl'] > 1: print('By cost.') status = 1 break if abs(W_old - W).max() < eps and abs(H_old - H).max() < eps: if cfg['print_lvl'] > 1: print('By argument.') status = 2 break #del W_old #del H_old if cfg['print_lvl'] > 1: print('Final:') W = normalize_cols(W) H = normalize_cols(H) for j, fun_name in enumerate(meas): fun = getattr(measure, fun_name) val[it+2:, j] = fun(V, np.dot(W, H)) if cfg['compare_real']: idx = get_permute(W_r, H_r, W, H, cfg['munkres']) hdist[it+2:] = hellinger(W[:, idx[:, 1]], W_r[:, idx[:, 0]]) / T return (val, hdist, it, W, H, status)