def get_opt_prob(subfolder, bench_id, num_iter=None): ''' get optimier and problem. Args: subfolder (str): subfolder with configuration files. bench_id (int): benchmark id. num_iter (None|int): load the data for specific iteration, None for random data. Return: climin.Minizer, Probdef: optimizer and problem. ''' configfile = 'benchmarks/%s/config-sample.ini' % subfolder config = load_config(configfile) # modification to parameters folder = os.path.dirname(configfile) sys.path.insert(0, folder) from config import modifyconfig_and_getnn rbm = modifyconfig_and_getnn(config, bench_id) optimizer, problem = pconfig(config, rbm) # load saved data if num_iter is not None: data = np.load(folder + '/variables-%d%d.npy' % (bench_id, num_iter)) problem.rbm.set_variables(data) return optimizer, problem
def analyse_weights2(configfile, bench_id, num_iter): config = load_config(configfile) # folder to store data, containing config.py folder = os.path.dirname(configfile) e0 = config['hamiltonian']['EG'] sys.path.insert(0, folder) from config import modifyconfig_and_getnn rbm = modifyconfig_and_getnn(config, bench_id) optimizer, problem = pconfig(config, rbm) h, sr, rbm, vmc = problem.hamiltonian, problem.sr, problem.rbm, problem.vmc # load data data = np.load(folder + '/variables-%d%d.npy' % (bench_id, num_iter)) rbm.set_variables(data) # compute overlap H, e0, v0, configs = analyse_exact(h, do_printsign=False) vec = rbm.tovec(mag=h.mag) vec = vec / np.linalg.norm(vec) overlap = abs(vec.T.conj().dot(v0)) print('overlap = %s' % overlap) # show weights in conv layers. keys = [1, 2, 4, 5, 6, 7] # configs, config_indexer = h.get_config_table() #configs = np.array([[1,-1]*(h.nsite//2)]) #configs = np.array([[1,-1,-1,1]*(h.nsite//4)]) plt.ion() num_try = 100 for i in range(num_try): config = configs[np.random.choice(len(configs))] plt.clf() ys = rbm.forward(config, full_output=True) for ik, k in enumerate(keys): ax = plt.subplot('61%d' % (ik + 1), sharex=ax if ik < 4 and ik > 0 else None) if k == 7: vmin, vmax = 0, 5e5 elif k == 5: vmin, vmax = 0, 2e5 elif k == 6: vmin, vmax = 0, 5e5 elif k == 4: vmin, vmax = 0, 5 else: vmin, vmax = None, None if ik < 2: plt.pcolor(np.atleast_2d(np.squeeze(ys[k]).real), vmin=vmin, vmax=vmax) else: plt.pcolor(np.atleast_2d(np.squeeze(abs(ys[k]))), vmin=vmin, vmax=vmax) plt.colorbar() plt.pause(0.01) pdb.set_trace()
def bverr(self, subfolder, bench_id, extension='png'): '''show energy error function.''' # get configuration and foler configfile = 'benchmarks/%s/config-sample.ini' % subfolder folder = os.path.dirname(configfile) config = load_config(configfile) # modification to parameters sys.path.insert(0, folder) from config import modifyconfig_and_getnn rbm = modifyconfig_and_getnn(config, bench_id) EG = config['hamiltonian']['EG'] if EG is None: h = load_hamiltonian(model='J1J2', J1=1, J2=config['hamiltonian']['J2'], size=config['hamiltonian']['size']) H, EG, v, configs = analyse_exact(h, do_printsign=False, num_eng=1) context = DShow( (5, 3.5), filename="notes/img/ERRL-%s-%s.%s" % (subfolder, bench_id, extension)) if extension is not '-' else contextlib.ExitStack() with context: show_el(datafiles=['%s/el-%i.dat' % (folder, bench_id)], nsite=np.prod(config['hamiltonian']['size']), EG=EG, legends=['id = %s' % bench_id], show_err=True, xlogscale=False)
def bve(self, subfolder, bench_id, extension='png'): '''show energy function.''' # get configuration and foler configfile = 'benchmarks/%s/config-sample.ini' % subfolder folder = os.path.dirname(configfile) config = load_config(configfile) # modification to parameters sys.path.insert(0, folder) from config import modifyconfig_and_getnn rbm = modifyconfig_and_getnn(config, bench_id) EG = config['hamiltonian']['EG'] context = DShow( (5, 3.5), filename="notes/img/EL-%s-%s.%s" % (subfolder, bench_id, extension)) if extension is not '-' else contextlib.ExitStack() with context: show_el(datafiles=['%s/el-%i.dat' % (folder, bench_id)], nsite=np.prod(config['hamiltonian']['size']), EG=config['hamiltonian']['EG'], legends=['id = %s' % bench_id], show_err=False, xlogscale=True)
def show_kernel_bentch(configfile, bentch_id): config = load_config(configfile) # folder to store data, containing config.py folder = os.path.dirname(configfile) # modification to parameters sys.path.insert(0, folder) from config import modifyconfig_and_getnn rbm = modifyconfig_and_getnn(config, bentch_id) show_kernel(rbm)
def analyse_poly(configfile, num_iter, bench_id_list): config = load_config(configfile) # folder to store data, containing config.py folder = os.path.dirname(configfile) e0 = config['hamiltonian']['EG'] labels = [ 'polynomial', 'legendre', 'hermite', 'chebyshev', 'laguerre', 'hermiteE' ] legends = [] sys.path.insert(0, folder) from config import modifyconfig_and_getnn plt.ion() for ib, (bench_id, label) in enumerate(zip(bench_id_list, labels)): rbm = modifyconfig_and_getnn(config, bench_id) optimizer, problem = pconfig(config, rbm) h, sr, rbm, vmc = problem.hamiltonian, problem.sr, problem.rbm, problem.vmc if ib == 0: H, e0, v0, configs = analyse_exact(h, do_printsign=False) # load data data = np.load(folder + '/variables-%d%d.npy' % (bench_id, num_iter)) rbm.set_variables(data) # compute overlap vec = rbm.tovec(mag=h.mag) vec = vec / np.linalg.norm(vec) overlap = abs(vec.T.conj().dot(v0)) print('overlap = %s' % overlap) # show weights in conv layers. for layer in rbm.layers: if hasattr(layer, 'kernel_dict'): polylayer = layer data = polylayer.get_variables() plt.plot(np.real(data)) legends.append('%s (%.4f)' % (label, overlap)) plt.axhline(y=0, ls='--', color='k') plt.legend(legends) pdb.set_trace() plt.savefig('notes/img/polyweight.png')
def benchmark(self, configfile, bench_id, interactive=False): from problems import load_config config = load_config(configfile) folder = os.path.dirname(configfile) # modification to parameters sys.path.insert(0, folder) from config import modifyconfig_and_getnn rbm = modifyconfig_and_getnn(config, bench_id) n = config['mpi']['num_core'] logfile = '%s/log-%s.log' % (folder, bench_id) options = ' '.join(['%s' % item for item in [configfile, bench_id]]) exec_code = 'python benchmark.py %s' % (options, ) if not interactive: exec_code = 'nohup mpirun -n %s %s > %s &' % (n, exec_code, logfile) print('Excuting: %s' % exec_code) os.system(exec_code)
def main(): from monitors import Print_eng_with_exact, print_eng, show_wf, DumpNetwork configfile, bench_id = sys.argv[1:] np.random.seed(3) dconfig = load_config(configfile) # folder to store data, containing config.py folder = os.path.dirname(configfile) # modification to parameters sys.path.insert(0, folder) from config import modifyconfig_and_getnn rbm = modifyconfig_and_getnn(dconfig, int(bench_id)) e0 = dconfig['hamiltonian']['EG'] import datetime print(datetime.datetime.now()) run_benchmark(dconfig, rbm, int(bench_id),\ monitors = [ Print_eng_with_exact(e0),\ DumpNetwork(folder=os.path.dirname(configfile),token=bench_id,step=1000)\ ], folder=folder, viz=False)
def analyse_weights(configfile, bench_id, num_iter): config = load_config(configfile) # folder to store data, containing config.py folder = os.path.dirname(configfile) e0 = config['hamiltonian']['EG'] sys.path.insert(0, folder) from config import modifyconfig_and_getnn rbm = modifyconfig_and_getnn(config, bench_id) optimizer, problem = pconfig(config, rbm) h, sr, rbm, vmc = problem.hamiltonian, problem.sr, problem.rbm, problem.vmc # load data data = np.load(folder + '/variables-%d%d.npy' % (bench_id, num_iter)) rbm.set_variables(data) # compute overlap H, e0, v0, configs = analyse_exact(h, do_printsign=False) vec = rbm.tovec(mag=h.mag) vec = vec / np.linalg.norm(vec) overlap = abs(vec.T.conj().dot(v0)) print('overlap = %s' % overlap) # show weights in conv layers. convlayer = rbm.layers[2] data = convlayer.weight #.sum(axis=0, keepdims=True) plt.ion() plt.imshow(np.real( abs(data).sum(axis=0, keepdims=True).reshape([-1, convlayer.weight.shape[-1]])), vmin=-0.5, vmax=0.5) nfo, nfi = data.shape[:2] poses = range(nfo * nfi) plt.yticks(poses, [(pos // nfi, pos % nfi) for pos in poses]) plt.ylabel('out, in') plt.colorbar() pdb.set_trace()
def analyse_polycurve(configfile, num_iter, bench_id_list, show_var=False, token=''): config = load_config(configfile) # folder to store data, containing config.py folder = os.path.dirname(configfile) e0 = config['hamiltonian']['EG'] legends = [] sys.path.insert(0, folder) from config import modifyconfig_and_getnn x = np.linspace(-2, 2, 200) plt.ion() for bench_id in bench_id_list: rbm = modifyconfig_and_getnn(config, bench_id) # show weights in conv layers. for nit in np.atleast_1d(num_iter): rbm_var = np.load(folder + '/variables-%d%d.npy' % (bench_id, nit)) rbm.set_variables(rbm_var) for layer in rbm.layers: if hasattr(layer, 'kernel_dict'): polylayer = layer legends.append(layer.kernel + '-%d' % nit) if show_var: data = polylayer.get_variables() plt.plot(data.real) else: data = polylayer.forward(x) plt.plot(x, data.real) plt.legend(legends) #plt.ylim(-20,20) pdb.set_trace() if show_var: plt.savefig('notes/img/polyvar-%s.png' % token) else: plt.savefig('notes/img/polycurve-%s.png' % token)
def show_bench44dncr(): '''Compare complex and real.''' token = 'dn' ids = [1, 6, 7] show_err = True plt.ion() from problems import load_config folder = '../benchmarks/wanglei4dn' config = load_config(folder + '/config-sample.ini') legends = [] for id in ids: for version in ['c', 'r']: show_el(datafiles=[ folder + '/el-%i%s.dat' % (id, 'c' if version == 'c' else '') ], EG=config['hamiltonian']['EG'], show_err=show_err, xlogscale=not show_err, smooth_step=20) legends.append('%s-%s' % (version, id + 1)) plt.legend(legends) plt.ylim(1e-5, 1) pdb.set_trace() plt.savefig('img/errcr-%s.png' % token)