def visualize_beta(): L = 10 nn = pickle.load(open('nn5_final.p', 'rb')) #for w in nn.weights[:1] : # with np.printoptions(precision=2, suppress=True) : # print(w.reshape((-1,L))) #plt.imshow(nn.weights[0].reshape((L,-1))) #plt.show() N = 5000 training_fraction = 0.4 ising = Ising(L, N) #X, y = ising.generateTrainingData1D() D, ry = ising.generateDesignMatrix1D() #y /= L ols = LeastSquares(method='ols', backend='manual') ols.setLambda(0.1) ols.fit(D, ry) print(ising.states.shape) #W = nn.weights[0].reshape((-1,L))*nn.weights[1] W = ols.beta.reshape((-1, L)) J = ising.J for i in range(10): row = ising.states[i, :] des = D[i, :] E = ry[i] print(row.shape) row = np.expand_dims(row, 1) print("s W s:", row.T @ W @ row) print("s (W+W')/2 s:", row.T @ (W + W.T) / 2 @ row) print("s J s:", row.T @ J @ row) #print("D w: ", W.T @ des * nn.weights[1]) #print("pred: ", nn.predict(np.expand_dims(des.T,1))) print("E: ", E) print("") for i in range(N): row = ising.states[i, :] des = D[i, :] E = ry[i] atol = 1e-14 rtol = 1e-14 #assert np.allclose(row.T @ W @ row, row.T @ (W+W.T)/2 @ row, atol=atol, rtol=rtol) #assert np.allclose(row.T @ (W+W.T)/2 @ row, row.T @ J @ row, atol=atol, rtol=rtol) with np.printoptions(precision=2, suppress=True): for a in np.linalg.eig(W): print(a) with np.printoptions(precision=2, suppress=True): print("det=", np.linalg.det(W)) print("cond=", np.linalg.cond(W)) print("") print("J:\n:", J) print("W+W'/2\n", (W + W.T) / 2) print("J+J'/2\n", (J + J.T) / 2) print("Tr D:", np.sum(np.diag((W + W.T) / 2)))
def visualize_unbalanced(): L = 1000 possible_E = len(np.arange(-L, L + 1, 4)) N = 2 * possible_E print(N) plt.rc('text', usetex=True) ising = Ising(L, N) ising.generateTrainingData1D() #s = ising.states E = ising.E #M = np.sum(s,1) ind = np.argsort(E) #s = s[ind,:] E = E[ind] / L plt.plot(E, np.linspace(0, 1, len(E)), 'r-', label=r'Even sampling') ising = Ising(L, N) ising.generateStates1D() ising.computeEnergy1D() s_u = ising.states E_u = ising.E M_u = np.sum(s_u, 1) E_u = np.sort(E_u) / L #s_u = s[ind_u,:] #E_u = E[ind_u] plt.plot(E_u, np.linspace(0, 1, len(E)), 'b-', label=r'Naive sampling') plt.axis([-1, 1, 0, 1]) plt.xlabel(r'Normalized energy, $E/L$', fontsize=10) plt.ylabel(r'Cumulative distribution, $P(E_i\ge E)$', fontsize=10) plt.legend(fontsize=10) plt.savefig(os.path.join(os.path.dirname(__file__), 'figures', 'visualize_sampling.png'), transparent=True, bbox_inches='tight') plt.show()
def simulate(): """ Run several Ising model simulations with different temperatures. Store them in 1 hdf5 file. """ temperatures = numpy.linspace(args.tmin, args.tmax, args.steps) with HDF5Handler(args.filename) as handler: for index, T in enumerate(temperatures): h5path = "/"+"sim_"+str(index).zfill(4)+"/" # h5path thus looks like: # "/sim_0000/", "/sim_0001/", etc. handler.prefix = h5path i = Ising(args.shape, args.sweeps, temperature=T, handler=handler, aligned=args.aligned, mode=args.algorithm, saveinterval=args.saveinterval, skip_n_steps=args.skip) if args.verbose: i.print_sim_parameters() widgets=drawwidget(" T = {} {}/{} ".format(round(T,4), index+1, len(temperatures))) pbar = pb.ProgressBar(widgets=widgets, maxval=args.sweeps).start() i.evolve(pbar) pbar.finish() handler.file.flush()
def animate_ising(size, beta, frames): """ animate the ising model for specific conditions """ ising = Ising(size, beta) fig = plt.figure(figsize=(5, 5)) ax = fig.add_subplot(1, 1, 1) def display(): """ display funtion for animation """ ax.axis('off') ax.pcolor(ising.data) return fig def animate(t): """ animate function for animation """ ax.clear() print(t) ising.metropolis() fig = display() ax.set_title('beta= ' + str(beta) + ', t = ' + str(t)) return fig ani = animation.FuncAnimation(fig, animate, save_count=frames) ani.save('ising' + str(beta) + '.GIF', writer='imagemagick', fps=1, dpi=300)
def before_after(size, beta, file_name): """ simulate for size and beta """ ising = Ising(size, beta) before = ising.data.copy() # do metropolis 200 times for _ in range(200): ising.metropolis() # draw fig, axes = plt.subplots(1, 2, figsize=(18, 8)) ax1 = axes[0] ax2 = axes[1] ax1.pcolor(before) ax1.set_title("before") ax2.pcolor(ising.data) ax2.set_title("after") plt.savefig(file_name, dpi=300, bbox_inches='tight') plt.close()
def worker(tasks_queue, done_queue): """ Pulls a task from the task queue and initiates the ising model simulation. Ising.evolve() is run within the context of HDF5Handler so a handler can be passed to Ising object. The HDF5Handler context block is run within the context of Pbar to track the progress of the simulation. """ for task, hash_ in iter(tasks_queue.get, 'STOP'): process_id = int((mp.current_process().name)[-1]) #find nicer way writer = Writer((0, process_id), TERM) with Pbar(task, writer) as bar: with HDF5Handler(filename=ARGS.tempdir+'/'+hash_+'.hdf5') as h: time_start = time.time() isingsim = Ising(shape=task['shape'], sweeps=task['mcs'], temperature=task['temperature'], aligned=task['aligned'], algorithm=task['algorithm'], handler=h, saveinterval=task['saveinterval'], skip_n_steps=task['skip_n_steps']) isingsim.evolve(pbar=bar) runtime = round(time.time() - time_start, 2) subs = {'temp' : task["temperature"], 'shape' : task['shape'], 'algo' : task['algorithm'], 'aligned' : task['aligned'], 'mcs' : task['mcs'], 'runtime' : runtime, 'timestamp': time.strftime("%d %b %Y %H:%M:%S") } s = "T={temp:.3f} {shape} {algo} {aligned} {mcs} {runtime:.2f} {timestamp} " job_report = s.format(**subs) logging.info(job_report) done_queue.put(job_report)
import numpy as np from ising import Ising DATA_POINTS = 10000 SIZE = 8 for temp in np.linspace(1.0, 3.5, 26): data = np.zeros(shape=(DATA_POINTS, SIZE**2)) R = Ising(temp, SIZE) R.run(32**3) print('T = {} equilibration done'.format(temp)) for i in range(DATA_POINTS): R = Ising(temp, SIZE) R.run(32) data[i] = R.generate_data() np.save('data/train_temp_%g' % (temp), data) print('T = {} data collected'.format(temp))
def test_ising() : ising = Ising() err = None try: s, E = ising.generateStates1D() except ValueError as e: # This should result in a value error, we catch it and ignore. err = e assert str(err) == "System size and/or number of states not specified." L = 5 N = 10 s, E = ising.generateStates1D(L,N) # Ensure the correct number of states are generated, and that they all # contain the correct number of spins. assert s.shape[0] == N assert s.shape[1] == L assert E.shape[0] == N # The energy can never be more than L or lower than -L. assert np.max(E) <= L assert np.min(E) >= -L N = 10 L = 15 ising.generateStates1D(L,N) states = np.ones(shape=(N,L)) ising.states = states E = ising.computeEnergy1D() # All spins pointing in the same direction should yield E = -L assert np.all(E == np.ones(N) * (-L)) # Flipping a single spin should give energy E = -L + 4, since # two interactions which previously gave -1 energy contributions # now give +1. np.fill_diagonal(ising.states, -1) E = ising.computeEnergy1D() assert np.all(E == np.ones(N) * (-L+4)) # Flipping another one *not* adjacent to the first flipped gives # another +4 to the energy. np.fill_diagonal(ising.states[:,2:], -1) E = ising.computeEnergy1D() assert np.all(E == np.ones(N) * (-L+2*4)) # Flipping one spin in between two flipped ones, i.e. # # -1 1 -1 --> -1 -1 -1 # # gives -2 change in the energy. np.fill_diagonal(ising.states[:,1:], -1) E = ising.computeEnergy1D() assert np.all(E == np.ones(N) * (-L+2*4-4)) L = 3 N = 3 ising = Ising(L,N) states = np.array([ [10, 200, 3000], [40, 500, 6000], [70, 800, 9000] ]) ising.generateStates1D() ising.states = states ising.computeEnergy1D() X, y = ising.generateDesignMatrix1D(L, N) # Make sure every combination of 10, 200, 3000 multiplied together # exists in the first row of X, every combination of 40, 500, 6000 # is contained in the second row, etc. for row in range(3) : for i in states[row,:] : for j in states[row,:] : assert np.any(X[row,:] == i*j)
def takamura(a_germanet, a_N, a_cc_file, a_pos, a_neg, a_neut, a_plot=None, a_pos_re=NONMATCH_RE, a_neg_re=NONMATCH_RE): """Method for generating sentiment lexicons using Takamura's approach. @param a_germanet - GermaNet instance @param a_N - number of terms to extract @param a_cc_file - file containing coordinatively conjoined phrases @param a_pos - initial set of positive terms to be expanded @param a_neg - initial set of negative terms to be expanded @param a_neut - initial set of neutral terms to be expanded @param a_plot - name of file in which generated statics plots should be saved (None if no plot should be generated) @param a_pos_re - regular expression for matching positive terms @param a_neg_re - regular expression for matching negative terms @return \c 0 on success, non-\c 0 otherwise """ # estimate the number of terms to extract seed_set = a_pos | a_neg # create initial empty network ising = Ising() # populate network from GermaNet print("Adding GermaNet synsets...", end="", file=sys.stderr) _tkm_add_germanet(ising, a_germanet) print("done (Ising model has {:d} nodes)".format(ising.n_nodes), file=sys.stderr) # populate network from corpus print("Adding coordinate phrases from corpus...", end="", file=sys.stderr) _tkm_add_corpus(ising, a_cc_file) print("done (Ising model has {:d} nodes)".format(ising.n_nodes), file=sys.stderr) # reweight edges ising.reweight() # set fixed weights for words pertaining to the positive, negative, and # neutral set for ipos in a_pos: if ipos in ising: ising[ipos][FXD_WGHT_IDX] = 1. else: ising.add_node(ipos, 1.) ising[ipos][HAS_FXD_WGHT] = 1 if a_pos_re != NONMATCH_RE: _annotate_re(a_pos_re, ising, 1) for ineg in a_neg: if ineg in ising: ising[ineg][FXD_WGHT_IDX] = -1. else: ising.add_node(ineg, -1.) ising[ineg][HAS_FXD_WGHT] = 1 if a_pos_re != NONMATCH_RE: _annotate_re(a_neg_re, ising, -1.) for ineut in a_neut: if ineut in ising: ising[ineut][FXD_WGHT_IDX] = 0. else: ising.add_node(ineut, 0.) ising[ineut][HAS_FXD_WGHT] = 1 ising.train(a_plot=a_plot) # nodes = [inode[ITEM_IDX] # for inode in sorted(ising.nodes, key = lambda x: x[WGHT_IDX]) # if inode[ITEM_IDX] not in seed_set] seed_set |= a_neut nodes = [ inode for inode in sorted( ising.nodes, key=lambda x: abs(x[WGHT_IDX]), reverse=True) if inode[ITEM_IDX] not in seed_set ] seed_set.clear() # populate polarity sets and flush all terms to an external file i = 0 if a_N < 0: a_N = len(nodes) # generate final set of polar terms max_w = max(inode[WGHT_IDX] for inode in nodes) + 1. min_w = max(inode[WGHT_IDX] for inode in nodes) - 1. # add all original seed terms ret = [(iterm, POSITIVE, max_w) for iterm in a_pos] + \ [(iterm, NEGATIVE, min_w) for iterm in a_neg] # add remaining automatically derived terms for inode in nodes: if isnan(inode[WGHT_IDX]): print(inode[ITEM_IDX].encode(ENCODING), "\t", inode[WGHT_IDX], file=sys.stderr) else: if i < a_N: if inode[WGHT_IDX] > 0: ret.append((inode[ITEM_IDX], POSITIVE, inode[WGHT_IDX])) elif inode[WGHT_IDX] < 0: ret.append((inode[ITEM_IDX], NEGATIVE, inode[WGHT_IDX])) else: continue i += 1 else: break return ret
temp = float(temp) # Temperature under q seed = int(seed) # Random seed print "n = %d\ntemp0 = %s\ntemp = %d\nseed = %s\nres_dir=%s\n" % \ (n, temp0, temp, seed, res_dir) rand.seed(seed) l = 10 # Dimension of 2D-lattice d = l**2 # Dimension of random vector # ------------------------- Draw MCMC samples ------------------------- # print "Drawing MCMC samples ..." model_p = Ising(d) model_p.set_ferromagnet(l, temp0) samples_p = model_p.sample(num_iters=1E5, num_samples=n) # Set q to perturbed dist or true p true_dist = rand.binomial(n=1, p=.5) # 0 for p, 1 for q model_q = Ising(d) temp_q = temp if true_dist else temp0 model_q.set_ferromagnet(l, temp_q) samples_q = model_q.sample(num_iters=1E5, num_samples=n) # ------------------------- Perform KDSD test ------------------------- # print "Performing KDSD test ..."
import numpy as np from ising import Ising Neq = 2000 Nav = 2000 Ls = [8, 16, 32] Ts = np.linspace(2.1, 2.3, 21) gamma = np.zeros(Ts.shape) j = 1 for L in Ls: shape = (L, L) for i, T in enumerate(Ts): system = Ising(shape, T) system.equilibrate(Neq) m2, m4 = system.sample(None, None, Nav)[2:] gamma[i] = m4 / (m2 * m2) print(j, "/", len(Ls) * len(Ts), " finished") j += 1 np.savetxt("gamma_L={}.txt".format(L), gamma)
lam_m = np.exp(beta) - np.sqrt(np.exp(2 * beta) - 2 * np.sinh(2 * beta)) lam_p_div_lam_m = lam_p / lam_m lam_m_div_lam_p = 1 / lam_p_div_lam_m return (lam_m_div_lam_p**r + lam_m_div_lam_p**L * lam_p_div_lam_m**r) / (1 + lam_m_div_lam_p**L) Neq = 50000 Nav = 10000 L = 16 shape = (L, ) T = 0.5 r_disc = np.arange(1, 9, 1) r_cont = np.linspace(1, 8, 1000) system = Ising(shape, T) system.equilibrate(Neq) C_ij = [] for r in r_disc: C_ij.append(system.sample((0, ), (r, ), Nav)[0]) sns.set() plt.plot(r_disc, C_ij, '-o', color='royalblue') plt.plot(r_cont, C(r_cont, L, T), label=r'$T=0.5$', color='royalblue', alpha=0.7) T = 1 system = Ising(shape, T)
from matplotlib import pyplot as plt from ising import Ising #from kurt import bootstrap_jacknife import os argv = docopt.docopt(__doc__, version="1.0") L = [int(L_) for L_ in argv["-L"]] bi = float(argv["-i"]) STEP = float(argv["-S"]) Nsteps = int(argv["-Q"]) beta = [bi + k * STEP for k in range(Nsteps)] THERMA = int(argv["-t"]) NMC = int(argv["-n"]) START = argv["-s"] OutDir = argv["-D"] if not os.path.isdir(OutDir): print('Your directory seems not to exist :-( ') exit() if os.listdir(OutDir): goon = input('Your directory is not empty! (~_^) Run anyway? (y/n) ') if not goon == 'y': exit() for i in beta: for j in L: ising = Ising(j, i, THERMA, NMC, START) #ising.run(OutDir, i==beta[len(beta)-1] and j==L[len(L)-1] ) ising.run(OutDir, False)
import numpy as np import matplotlib.pyplot as plt import seaborn as sns from ising import Ising Ts = np.logspace(-5, 5 + 4, 11 + 4) L = 16 shape = (L, L) Neq = 2000 Nav = 2000 m = np.zeros(Ts.shape) m2 = np.zeros(Ts.shape) for i, T in enumerate(Ts): system = Ising(shape, T) system.equilibrate(Neq) m[i], m2[i] = system.sample(None, None, Nav)[1:-1] print(i + 1, "/", len(Ts), " finished") np.savetxt("temperatures.txt", Ts) np.savetxt("magnetization.txt", m) np.savetxt("magnetization2.txt", m2)
def train_net_predict_energy(L=10, N=5000): ising = Ising(L, N) X, y = ising.generateTrainingData1D() y /= L n_samples, n_features = X.shape nn = NeuralNetwork(inputs=L, neurons=L * L, outputs=1, activations='sigmoid', cost='mse', silent=False) nn.addLayer(neurons=L * L) nn.addLayer(neurons=L * L) nn.addOutputLayer(activations='identity') validation_skip = 10 epochs = 1000 nn.fit(X.T, y, shuffle=True, batch_size=1000, validation_fraction=0.2, learning_rate=0.001, verbose=False, silent=False, epochs=epochs, validation_skip=validation_skip, optimizer='adam') # Use the net to predict the energies for the validation set. x_validation = nn.x_validation y_validation = nn.predict(x_validation) target_validation = nn.target_validation # Sort the targets for better visualization of the network output. ind = np.argsort(target_validation) y_validation = np.squeeze(y_validation.T[ind]) target_validation = np.squeeze(target_validation.T[ind]) # We dont want to plot the discontinuities in the target. target_validation[np.where( np.abs(np.diff(target_validation)) > 1e-5)] = np.nan plt.rc('text', usetex=True) plt.figure() plt.plot(target_validation, 'k--', label=r'Target') plt.plot(y_validation, 'r.', markersize=0.5, label=r'NN output') plt.legend(fontsize=10) plt.xlabel(r'Validation sample', fontsize=10) plt.ylabel(r'$E / L$', fontsize=10) #plt.savefig(os.path.join(os.path.dirname(__file__), 'figures', 'nn_1d_energy_predict' + str(L) + '.png'), transparent=True, bbox_inches='tight') # Plot the training / validation loss during training. training_loss = nn.training_loss validation_loss = nn.validation_loss # There are more training loss values than validation loss values, lets # align them so the plot makes sense. xaxis_validation_loss = np.zeros_like(validation_loss) xaxis_validation_loss[0] = 0 xaxis_validation_loss[1:-1] = np.arange(validation_skip, len(training_loss), validation_skip) xaxis_validation_loss[-1] = len(training_loss) plt.figure() plt.semilogy(training_loss, 'r-', label=r'Training loss') plt.semilogy(xaxis_validation_loss, validation_loss, 'k--', label=r'Validation loss') plt.legend(fontsize=10) plt.xlabel(r'Epoch', fontsize=10) plt.ylabel(r'Cost $C(\theta)$', fontsize=10) #plt.savefig(os.path.join(os.path.dirname(__file__), 'figures', 'nn_1d_loss' + str(L) + '.png'), transparent=True, bbox_inches='tight') plt.show()
def R2_versus_lasso(): L = 3 N = 10000 training_fraction = 0.4 ising = Ising(L, N) D, ry = ising.generateDesignMatrix1D() X, y = ising.generateTrainingData1D() y /= L D_train = D[int(training_fraction * N):, :] ry_train = ry[int(training_fraction * N):] D_validation = D[:int(training_fraction * N), :] ry_validation = ry[:int(training_fraction * N)] lasso = LeastSquares(method='lasso', backend='skl') lasso.setLambda(1e-2) lasso.fit(D_train, ry_train) lasso.y = ry_validation lasso_R2 = sklearn.metrics.mean_squared_error( ry_validation / L, lasso.predict(D_validation) / L) n_samples, n_features = X.shape nn = NeuralNetwork(inputs=L * L, neurons=L, outputs=1, activations='identity', cost='mse', silent=False) nn.addLayer(neurons=1) nn.addOutputLayer(activations='identity') validation_skip = 100 epochs = 50000 nn.fit(D.T, ry, shuffle=True, batch_size=2000, validation_fraction=1 - training_fraction, learning_rate=0.0001, verbose=False, silent=False, epochs=epochs, validation_skip=validation_skip, optimizer='adam') plt.rc('text', usetex=True) validation_loss = nn.validation_loss_improving validation_ep = np.linspace(0, epochs, len(nn.validation_loss_improving)) plt.semilogy(validation_ep, validation_loss, 'r-', label=r'NN') plt.semilogy([0, epochs], np.array([lasso_R2, lasso_R2]), 'k--', label=r'Lasso') plt.xlabel(r'Epoch', fontsize=10) plt.ylabel(r'Mean squared error', fontsize=10) plt.legend(fontsize=10) plt.xlim((0, epochs)) ax = plt.gca() ymin, ymax = ax.get_ylim() if ymin > pow(10, -5): ymin = pow(10, -5) #plt.ylim((ymin,ymax)) plt.savefig(os.path.join(os.path.dirname(__file__), 'figures', 'NN_compare_lasso.png'), transparent=True, bbox_inches='tight')