<jupyter_start><jupyter_text>import necessary modules and data<jupyter_code>import numpy as np
from keras.datasets import mnist
import matplotlib.pyplot as plt
from rbm import RBM, localRBM, FBM, localFBM, uniformFBM
from mcmc import *
# import MNIST data set and make them binary
(x_train, y_train), (x_test, y_test) = mnist.load_data()
def transform_input(x_t):
    x_t = np.sign(x_t-np.full(x_t.shape,15))
    x_t = x_t+np.ones(x_t.shape)
    return x_t/2
x_train = transform_input(x_train)
x_test = transform_input(x_test)
# one of the MNIST sample
plt.imshow(x_train[0])
plt.show()<jupyter_output><empty_output><jupyter_text>compile and train an RBM<jupyter_code>m= RBM([28,28],[20,20])
m.fit(x_train, x_test[:2000], batch=10, epoch=3, cdkstep=1, learningrate=0.005) 
# the weights have some locality features
plt.imshow(np.abs(m.getweights()[0][13]))
plt.colorbar()
plt.show()
# recover of the image
plt.imshow(m.Gibbsupdate(x_train[0:1],nosteps=1)[0].reshape(28,28))
plt.colorbar()
plt.show()
# generate new image after long gibbs updates
plt.imshow(m.Gibbsupdate(x_train[0:1],nosteps=1000)[0].reshape(28,28))
plt.colorbar()
plt.show()<jupyter_output><empty_output><jupyter_text>classical Monte Carlo method<jupyter_code>ising = Ising([10,10],coupling=[0.,0., -1.])
mc = mcmeasure(ising,[mag()]) 
mc.mcmove(1/2.2,50000,method='local_update')
# the m^2 value on the Markov chain
plt.plot(list(map(lambda x:x**2, np.array(mc.values)[:,0])))
plt.show()
analysis(np.array(mc.values)[10000:,0],2)<jupyter_output>the expectation value, standard deviation and autocorrelation time are correspondingly:<jupyter_text>train an RBM as the MC update proposal* Use MCMC generate samples* Train the RBM using these samples* Generate new samples from the RBM* Compare the expectation value of physical observables *Note the difference where MCMC is written in $\pm 1$ configs while RBM is implemented in 1,0 binary variables *<jupyter_code>ising = Ising([10,10],coupling=[0., 0, -1])
mc = writeconf2file(ising,2000,4,'spinsample.npy') 
# note to delete the npy file first if you want to generate configs by yourself with the same file name
mc.mcmove(1/2.3,2000+4*20000,method='wolff_update')
spin = mc.readconf()
# spin = readnpy('spinsample.npy')
spin = spin2binary(spin)
spin.shape
m = RBM([10,10],[20,20])
# the training before is omited, you may need around 100 epochs in total to train this model well
m.fit(spin[:19000], spin[19001:], batch=10, epoch=5, cdkstep=5, learningrate=0.0008) 
bising = Ising([10,10], coupling = [8,0,-4]) # effective Hamiltonian for the binary data as Ising model
(np.mean([bising.energy2d(spin[i]) for i in range(20000)])\
-np.mean([bising.energy2d(np.random.randint(2, size=(10,10))) for i in range(20000)]))
np.mean([m.freeenergy(spin[i]) for i in range(20000)])\
-np.mean([m.freeenergy(np.random.randint(2, size=(10,10))) for _ in range(20000)])<jupyter_output><empty_output><jupyter_text>due to the probability ratio relation, we have $T\Delta F =\Delta E$, which gives us an estimate on temperature of these configuration samples as $143.8/61.16 \approx 2.35$, which is very close to the real temperature. And note all these results come from no prior physics knowledge!<jupyter_code>mc = mcmeasure(ising,[mag()]) 
mc.mcmove(0,20000,method='rbm_update',tool=[m,5,False])
mc.mcmove(1/2.3,20000,method='wolff_update')
mc.mcmove(1/2.3,100000,method='local_update')
plt.plot(list(map(lambda x:x**2, np.array(mc.values)[:,0])))
plt.show()# show results from RBM update, wolff update and local update at the same time
analysis(np.array(mc.values)[2000:20000,0],2)
analysis(np.array(mc.values)[22000:40000,0],2)
analysis(np.array(mc.values)[45000:,0],2)<jupyter_output>the expectation value, standard deviation and autocorrelation time are correspondingly:<jupyter_text>The results is a little lower for RBM case which matches the result it gives a higher estimation on the temperature!   Use the FBM class as a detector of samples ensemble<jupyter_code>f = localFBM([10,10], np.ones((100,100))-np.identity(100))
f.fit(spin[:18000],spin[18000:], epoch=10, presteps=100, batch=300, mcsteps=200, 
      learningrate=0.2, regulation1=0.002)
plt.imshow(f.weights.reshape(10,10,10,10)[0,0])
plt.colorbar()
plt.show()
# the model after very short training has learned the locality of the interaction 
# as well as the periodicity of the model!
plt.imshow(f.bias.reshape(10,10))
plt.colorbar()
plt.show()
g = uniformFBM([10,10])
g.fit(spin[:18000],spin[18000:], epoch=5, presteps=100, batch=300, mcsteps=200, 
      learningrate=0.1)
g.weights[0,1]
# by considering the binary version Ising Hamoltonian as h=8, J=-4, we can get the temperature as 4/1.75=2.28,
# which is very close to real temperature 2.3!<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14306753629286767, 'fraction_numerical': 0.06964022722491059, 'mean_word_length': 6.486614173228347, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '602951', 'license': 'MIT', 'n_tokens_mistral': 1930, 'n_tokens_neox': 1727, 'n_words': 447, 'path': 'demo.ipynb', 'repo_name': 'refraction-ray/RBM-MCMC', 'size': '4753'}