def show_symmetry_table(tablefile): data = np.loadtxt(tablefile) with DShow((6, 4), 'notes/img/symm-table.png') as ds: ax = ds.ax ax.axis('off') col_labels = ['%d' % d for d in data[0, 1:]] row_labels = ['%s' % d for d in data[1:, 0]] width = 0.3 * (1 + len(col_labels)) height = 0.1 * (1 + len(row_labels)) the_table = plt.table(cellText=data[1:, 1:], colWidths=[0.3] * len(col_labels), rowLabels=row_labels, colLabels=col_labels, cellLoc='center', rowLoc='center', loc='center', bbox=[0., 0., width, height]) plt.ylim(0, height) plt.xlim(0, width * 2) # inverse * 2^0 # flip * 2^1 # translate * 2^2 mapper = {-1.0: '-', 1.0: '+'} J2_list = data[1:, 0] nsite_list = data[0, 1:] data = data[1:, 1:] translate = np.sign(data) flip = np.sign(data % 8 - 4) inverse = np.sign(data % 4 - 2) print('translate - flip - inverse') print(' ' + ' '.join(['%-4d' % nsite for nsite in nsite_list])) for iJ, J2 in enumerate(J2_list): s = '%.2f ' % J2 s += ' '.join([ '%s%s%s' % (mapper[translate[iJ, iS]], mapper[flip[iJ, iS]], mapper[inverse[iJ, iS]]) for iS in range(len(nsite_list)) ]) print(s)
def __call__(self, problem, optimizer): require_new_vv(problem, optimizer.n_iter) require_e0v0(problem) v0 = problem.cache['v0'] num_iter = optimizer.n_iter vvi = 'vv-%s' % num_iter vv = problem.cache[vvi] plt.ion() fig = gcf() fig.set_figwidth(10, forward=True) fig.set_figheight(5, forward=True) fig.clear() plt.subplot(121) compare_wf(vv, v0) plt.subplot(122) scatter_vec_phase(vv, self.vv_pre) plt.xlim(-self.D, self.D) plt.ylim(-self.D, self.D) plt.pause(0.01) self.vv_pre = vv
def run_wanglei4(J2, size, optimize_method='adam', momentum=0., do_plot_wf=True, compare_to_exact=True, learning_rate=1e-2): from models.wanglei4 import WangLei4 # definition of a problem h = load_hamiltonian('J1J2', size=size, J2=J2) rbm = WangLei4(input_shape=size, NF=8, K=3, num_features=[8], version='conv', itype='complex128') # visualize network from poornn import viznn viznn(rbm, filename='data/%s.png' % rbm.__class__.__name__) problem = ModelProbDef(hamiltonian=h, rbm=rbm, reg_method='delta', sr_layerwise=False) sr, rbm, vmc = problem.sr, problem.rbm, problem.vmc vmc.inverse_rate = 0.05 optimizer = get_optimizer(wrt=rbm.get_variables(), fprime=problem.compute_gradient, optimize_method=optimize_method, step_rate=learning_rate, momentum=momentum) # setup canvas if do_plot_wf: plt.ion() fig = plt.figure(figsize=(10, 5)) # Exact Results if compare_to_exact or compare_wf: H, e0, v0, configs = analyse_exact(h, do_printsign=False) el = [] # to store energy vv_pre = None print('\nRunning 0-th Iteration.') for info in optimizer: # `sampels` and `opq_vals` are cached! ei = problem.cache['opq_vals'][0] if do_plot_wf: vv = rbm.tovec(mag=h.mag) vv = vv / np.linalg.norm(vv) fig.clear() plt.subplot(121) compare_wf(vv, v0) plt.subplot(122) scatter_vec_phase(vv, vv_pre) D = 0.8 plt.xlim(-D, D) plt.ylim(-D, D) plt.pause(0.01) vv_pre = vv if compare_to_exact: err = abs(e0 - ei) / (abs(e0) + abs(ei)) * 2 print('E/site = %s (%s), Error = %.4f%%' % (ei / h.nsite, e0 / h.nsite, err * 100)) else: print('E/site = %s' % (ei / h.nsite, )) el.append(ei) num_iter = info['n_iter'] #optimizer.step_rate *= 0.995 if num_iter >= 1000: break print('\nRunning %s-th Iteration.' % (num_iter + 1)) np.savetxt('data/el-%s%s.dat' % (h.nsite, 'p' if h.periodic else 'o'), el) pdb.set_trace()
def run_target_sign(J2, nsite): '''Given Sign train amplitude, the arbituary state version.''' from models.wanglei import WangLei # definition of a problem h = load_hamiltonian('J1J2', size=(nsite, ), J2=J2) rbm = WangLei(input_shape=(h.nsite, ), version='linear', use_conv=True, itype='complex128') problem = ModelProbDef(hamiltonian=h, rbm=rbm, reg_method='delta', sr_layerwise=True) sr, rbm, vmc = problem.sr, problem.rbm, problem.vmc vmc.inverse_rate = 0.05 optimizer = get_optimizer(wrt=rbm.get_variables(), fprime=problem.compute_gradient, optimize_method='gd', step_rate=1e-1, momentum=momentum) do_plot_wf = True compare_to_exact = True # setup canvas if do_plot_wf: plt.ion() fig = plt.figure(figsize=(10, 5)) # Exact Results if compare_to_exact or compare_wf: H, e0, v0, configs = analyse_exact(h, do_printsign=False) el = [] # to store energy vv_pre = None print('\nRunning 0-th Iteration.') for info in optimizer: # `sampels` and `opq_vals` are cached! ei = problem.cache['opq_vals'][0] if do_plot_wf: vv = rbm.tovec(mag=h.mag) vv = vv / np.linalg.norm(vv) fig.clear() plt.subplot(121) compare_wf(vv, v0) plt.subplot(122) scatter_vec_phase(vv, vv_pre) plt.xlim(-0.3, 0.3) plt.ylim(-0.3, 0.3) plt.pause(0.01) vv_pre = vv num_iter = info['n_iter'] #optimizer.step_rate *= 0.995 if num_iter >= 200: break print('\nRunning %s-th Iteration.' % (num_iter + 1)) np.savetxt('data/el-%s%s.dat' % (h.nsite, 'p' if h.periodic else 'o'), el) pdb.set_trace()
def run_rtheta_switch(J2, nsite, rtheta_training_ratio, switch_step, momentum=0., \ do_plot_wf=True, compare_to_exact=True, do_check_sample=False): from models.wanglei2 import WangLei2 # definition of a problem h = load_hamiltonian('J1J2', size=(nsite, ), J1=1., J2=J2) rbm = WangLei2(input_shape=(h.nsite, ), num_feature_hidden=4, use_msr=False, theta_period=2, with_linear=False, itype='float64') #rbm.thnn = get_exact_thnn4(fixed_var=True) problem = ModelProbDef(hamiltonian=h, rbm=rbm, reg_method='sd') sr, rbm, vmc = problem.sr, problem.rbm, problem.vmc vmc.inverse_rate = 0.05 optimizer = get_optimizer(wrt=rbm.get_variables(), fprime=problem.compute_gradient, optimize_method='gd', step_rate=3e-3, momentum=momentum) # setup canvas if do_plot_wf or do_check_sample: plt.ion() fig = plt.figure(figsize=(10, 5)) # Exact Results if compare_to_exact or compare_wf: H, e0, v0, configs = analyse_exact(h, do_printsign=False, num_eng=5) el = [] # to store energy vv_pre = None print('\nRunning 0-th Iteration.') sr.rtheta_training_ratio = [rtheta_training_ratio[0], 0] for info in optimizer: # `sampels` and `opq_vals` are cached! ei = problem.cache['opq_vals'][0] if do_plot_wf or do_check_sample: amp = [] signs = [] for config in h.configs: amp.append(rbm.forward(config)) signs.append(rbm.get_sign(config)) amp = np.asarray(amp) amp = amp / np.linalg.norm(amp) vv = amp * signs #vv = rbm.tovec(mag=h.mag) if do_plot_wf: fig.clear() plt.subplot(121) #compare_wf(amp, v0) compare_wf(vv, v0) plt.subplot(122) scatter_vec_phase(vv, vv_pre) plt.xlim(-0.8, 0.8) plt.ylim(-0.8, 0.8) plt.pause(0.01) vv_pre = vv if do_check_sample: fig.clear() check_sample(rbm, h, problem.cache['samples']) plt.pause(0.01) if compare_to_exact: err = abs(e0 - ei) / (abs(e0) + abs(ei)) * 2 print('E/site = %s (%s), Error = %.4f%%' % (ei / h.nsite, e0 / h.nsite, err * 100)) else: print('E/site = %s' % (ei / h.nsite, )) el.append(ei) k = info['n_iter'] if k >= 800: break if k % (2 * switch_step) < switch_step: print('\nRunning %s-th Iteration (optimize amplitudes).' % (k + 1)) sr.rtheta_training_ratio = [rtheta_training_ratio[0], 0] else: print('\nRunning %s-th Iteration (optimize signs).' % (k + 1)) sr.rtheta_training_ratio = [0, rtheta_training_ratio[1]] np.savetxt('data/el-%s%s.dat' % (h.nsite, 'p' if h.periodic else 'o'), el) pdb.set_trace()
def rbm_given_sign(J2, nsite): from models.poorrbm import RBM do_plot_wf = True compare_to_exact = True # definition of a problem h = load_hamiltonian('J1J2', size=(nsite, ), J2=J2) # Exact Results if compare_to_exact or compare_wf: H, e0, v0, configs = analyse_exact(h, do_printsign=False) rbm = RBM(input_shape=(h.nsite, ), num_feature_hidden=4, itype='float64', sign_func=sign_func_from_vec(h.configs, v0)) problem = ModelProbDef(hamiltonian=h, rbm=rbm, reg_method='delta', sr_layerwise=False) sr, rbm, vmc = problem.sr, problem.rbm, problem.vmc vmc.inverse_rate = 0.05 optimizer = get_optimizer(wrt=rbm.get_variables(), fprime=problem.compute_gradient, optimize_method='gd', step_rate=3e-2, momentum=momentum) # setup canvas if do_plot_wf: plt.ion() fig = plt.figure(figsize=(10, 5)) el = [] # to store energy vv_pre = None print('\nRunning 0-th Iteration.') for info in optimizer: # `sampels` and `opq_vals` are cached! ei = problem.cache['opq_vals'][0] if do_plot_wf: vv = rbm.tovec(mag=h.mag) vv = vv / np.linalg.norm(vv) fig.clear() plt.subplot(121) compare_wf(vv, v0) plt.subplot(122) scatter_vec_phase(vv, vv_pre) plt.xlim(-0.3, 0.3) plt.ylim(-0.3, 0.3) plt.pause(0.01) vv_pre = vv if compare_to_exact: err = abs(e0 - ei) / (abs(e0) + abs(ei)) * 2 print('E/site = %s (%s), Error = %.4f%%' % (ei / h.nsite, e0 / h.nsite, err * 100)) else: print('E/site = %s' % (ei / h.nsite, )) el.append(ei) num_iter = info['n_iter'] #optimizer.step_rate *= 0.995 if num_iter >= 2000: break print('\nRunning %s-th Iteration.' % (num_iter + 1)) np.savetxt('data/el-%s%s.dat' % (h.nsite, 'p' if h.periodic else 'o'), el) pdb.set_trace()
def run_rtheta(J2, nsite, rtheta_training_ratio, momentum=0.): from models.wanglei2 import WangLei2 # definition of a problem h = load_hamiltonian('J1J2', size=(nsite, ), J2=J2) rbm = WangLei2(input_shape=(h.nsite, ), num_feature_hidden=4, use_msr=False, theta_period=2, with_linear=False, itype='float64') problem = ModelProbDef(hamiltonian=h, rbm=rbm, reg_method='sd') sr, rbm, vmc = problem.sr, problem.rbm, problem.vmc sr.rtheta_training_ratio = rtheta_training_ratio optimizer = get_optimizer(wrt=rbm.get_variables(), fprime=problem.compute_gradient, optimize_method='gd', step_rate=1e-2, momentum=momentum) do_plot_wf = True compare_to_exact = True do_check_sample = False # setup canvas if do_plot_wf or do_check_sample: plt.ion() fig = plt.figure(figsize=(10, 5)) # Exact Results if compare_to_exact or compare_wf: H, e0, v0, configs = analyse_exact(h, do_printsign=False, num_eng=10) el = [] # to store energy vv_pre = None print('\nRunning 0-th Iteration.') for info in optimizer: # `sampels` and `opq_vals` are cached! ei = problem.cache['opq_vals'][0] if do_plot_wf or do_check_sample: amps = [] thetas = [] signs = [] for config in h.configs: amps.append(rbm.forward(config)) thetas.append(rbm.thnn.forward(config)) signs.append(np.exp(1j * thetas[-1])) amps = np.asarray(amps) amps = amps / np.linalg.norm(amps) vv = amps * signs #vv = rbm.tovec(mag=h.mag) if do_plot_wf: fig.clear() plt.subplot(121) #compare_wf(amps, v0) compare_wf(vv, v0) plt.subplot(122) scatter_vec_phase(vv, vv_pre, winding=np.int32( np.floor(np.array(thetas) / 2 / np.pi))) plt.xlim(-0.8, 0.8) plt.ylim(-0.8, 0.8) plt.pause(0.01) vv_pre = vv if do_check_sample: fig.clear() check_sample(rbm, h, problem.cache['samples']) plt.pause(0.01) if compare_to_exact: err = abs(e0 - ei) / (abs(e0) + abs(ei)) * 2 print('E/site = %s (%s), Error = %.4f%%' % (ei / h.nsite, e0 / h.nsite, err * 100)) else: print('E/site = %s' % (ei / h.nsite, )) el.append(ei) if info['n_iter'] >= 800: break print('\nRunning %s-th Iteration.' % (info['n_iter'] + 1)) np.savetxt('data/el-%s%s.dat' % (h.nsite, 'p' if h.periodic else 'o'), el) pdb.set_trace()