Exemplo n.º 1
0
def sample_and_save(temperature, saved_state, parameters, filename, dtype):
	""" Appends the four observables for a given trained rbm to a file.
	Args:
		n_vis: number of visible nodes
		n_hid: number of hidden nodes
		temperature: temperature of the training data.
		saved_state: torch save file for the rbm.
		parameters: json parameters for sampling.
		filename: path to where data should be saved.
	"""
	L = parameters['ising']['size']
	rbm = rbm_pytorch.RBM(n_vis=L**2, n_hid=parameters['n_hid'])
	rbm.load_state_dict(torch.load(saved_state))

	states = ising_methods_new.sample_from_rbm(rbm, parameters, dtype)
	
	mag, susc, energy, heatc = ising_methods_new.ising_observables(states, L, temperature)

	with open(filename, "a") as file:
		file.write("{:f}\t{:f}\t{:f}\t{:f}\t{:f}\t{:f}\t{:f}\t{:f}\t{:f}\n".format(temperature, 
					mag[0], mag[1], susc[0], susc[1], energy[0], energy[1], heatc[0], heatc[1]))
Exemplo n.º 2
0
temperature = 2.27

try:
	parameters = json.load(open(args.input_json))
except IOError as e:
	print("I/O error({0}) (json): {1}".format(e.errno, e.strerror))
	sys.exit(1)
except:
	print("Unexpected error:", sys.exc_info()[0])
	raise

dtype = torch.FloatTensor
rbm = rbm_pytorch.RBM(n_vis=64, n_hid=parameters['n_hid'])
rbm.load_state_dict(torch.load(parameters['saved_state'], map_location=lambda storage, loc: storage))

states = ising_methods_new.sample_from_rbm(rbm, parameters, dtype)

spin_states = convert_to_spins(states)

mag_history = magnetisation(spin_states).cpu().numpy()
energy_history = ising_energy(spin_states, L).cpu().numpy()

split_mag = np.reshape(mag_history, (-1,parameters['concurrent_states']))
split_energy = np.reshape(energy_history, (-1,parameters['concurrent_states']))

split_susc = np.var(split_mag, axis=1)/(N_spins * temperature)
split_heatc = np.var(split_energy,axis=1)/(N_spins * temperature**2)

split_mag = np.mean(split_mag, axis=1)
#split_mag = pd.Series(split_mag)
Exemplo n.º 3
0
dtype = torch.FloatTensor
rbm10 = rbm_pytorch.RBM(n_vis=64, n_hid=parameters['n_hid'])
rbm10.load_state_dict(
    torch.load("batchsizes/10/trained_rbm.pytorch.200",
               map_location=lambda storage, loc: storage))
rbm100 = rbm_pytorch.RBM(n_vis=64, n_hid=parameters['n_hid'])
rbm100.load_state_dict(
    torch.load("batchsizes/100/trained_rbm.pytorch.200",
               map_location=lambda storage, loc: storage))
rbm1000 = rbm_pytorch.RBM(n_vis=64, n_hid=parameters['n_hid'])
rbm1000.load_state_dict(
    torch.load("batchsizes/1000/trained_rbm.pytorch.200",
               map_location=lambda storage, loc: storage))

states10 = ising_methods_new.sample_from_rbm(rbm10, parameters, dtype)
states100 = ising_methods_new.sample_from_rbm(rbm100, parameters, dtype)
states1000 = ising_methods_new.sample_from_rbm(rbm1000, parameters, dtype)

spin_states10 = convert_to_spins(states10)
spin_states100 = convert_to_spins(states100)
spin_states1000 = convert_to_spins(states1000)

mag_history10 = magnetisation(spin_states10).cpu().numpy()
energy_history10 = ising_energy(spin_states10, L).cpu().numpy()
mag_history100 = magnetisation(spin_states100).cpu().numpy()
energy_history100 = ising_energy(spin_states100, L).cpu().numpy()
mag_history1000 = magnetisation(spin_states1000).cpu().numpy()
energy_history1000 = ising_energy(spin_states1000, L).cpu().numpy()

split_mag10 = np.reshape(mag_history10, (-1, parameters['concurrent_states']))
Exemplo n.º 4
0
args = parser.parse_args()

try:
    parameters = json.load(open(args.input_json))
except IOError as e:
    print("I/O error({0}) (json): {1}".format(e.errno, e.strerror))
    sys.exit(1)
except:
    print("Unexpected error:", sys.exc_info()[0])
    raise

rbm = rbm_pytorch.RBM(n_vis=parameters['ising']['size']**2,
                      n_hid=parameters['n_hid'])

rbm.load_state_dict(
    torch.load(parameters['saved_state'],
               map_location=lambda storage, loc: storage))
x = np.array([[
    0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0,
    1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1,
    1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1
]])
vin = torch.from_numpy(x)

for i in range(6, 7, 1):
    parameters['thermalisation'] = 10**i
    states = ising_methods_new.sample_from_rbm(rbm,
                                               parameters,
                                               v_in=vin,
                                               save_images=True)
Exemplo n.º 5
0
from ising_methods_new import sample_from_rbm, ising_observables
import json
import argparse
import sys
sys.path.append('../Refactor/')
import rbm_pytorch
import torch

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('--json', dest='input_json', default='params.json', help='JSON file describing the sample parameters',
					type=str)

args = parser.parse_args()

try:
	parameters = json.load(open(args.input_json))
except IOError as e:
	print("I/O error({0}) (json): {1}".format(e.errno, e.strerror))
	sys.exit(1)
except:
	print("Unexpected error:", sys.exc_info()[0])
	raise

rbm = rbm_pytorch.RBM(n_vis=parameters['ising']['size']**2, n_hid=parameters['n_hid'])
rbm.load_state_dict(torch.load(parameters['saved_state'], map_location=lambda storage, loc: storage))

states = sample_from_rbm(rbm,parameters)

print(ising_observables(states, 8, 2.27))