<jupyter_start><jupyter_text>Energy<jupyter_code># unobsered configurations:
def difference_2d_arrays(a1,a2):
    # find the difference between 2d arrays
    a1_rows = a1.view([('', a1.dtype)] * a1.shape[1])
    a2_rows = a2.view([('', a2.dtype)] * a2.shape[1])
    a3 = np.setdiff1d(a1_rows, a2_rows).view(a1.dtype).reshape(-1, a1.shape[1])
    return a3
seq_unobs = difference_2d_arrays(seq_all,seqs)
print(seq_unobs.shape)

ops_unobs = EM.operators(seq_unobs)
print(ops_unobs.shape)
# energy of unobserved configurations as function of eps
energy_unobs = -ops_unobs.dot(w_eps.T)
energy_unobs_av = energy_unobs.mean(axis=0)

ops_all = EM.operators(seq_all)
energy_all = -ops_all.dot(w_eps.T)
energy_all_av = energy_all.mean(axis=0)
# energy of test set
energy_test = -ops_test.dot(w_eps.T)
energy_test_av = energy_test.mean(axis=0)
# random configurations
seq_random = np.random.choice([1.0,-1.0],size=(int(n_seq/2),n_var),replace=True)
print(seq_random.shape)

ops_random = EM.operators(seq_random)
energy_random = -ops_random.dot(w_eps.T)
energy_random_av = energy_random.mean(axis=0)
nx,ny = 5,1
fig, ax = plt.subplots(ny,nx,figsize=(nx*3,ny*2.8))

ax[0].plot(eps[:cut],energy_av[:cut],'ko-')
ax[1].plot(eps[:cut],energy_unobs_av[:cut],'ko-')
ax[2].plot(eps[:cut],energy_all_av[:cut],'ko-')
ax[3].plot(eps[:cut],energy_test_av[:cut],'ko-')
ax[4].plot(eps[:cut],energy_random_av[:cut],'ko-')

for i in range(nx):
    ax[i].set_xlabel('epsilon')
    ax[i].set_ylabel('energy mean')

ax[0].set_title('observed configs')
ax[1].set_title('unobserved configs')
ax[2].set_title('every configs')
ax[3].set_title('test configs')
ax[4].set_title('random configs')

plt.tight_layout(h_pad=0.7, w_pad=1.5)<jupyter_output><empty_output><jupyter_text>Histogram of Energy<jupyter_code># optimal eps
i_eps = np.argmax(e_eps)
print(i_eps,eps[i_eps])<jupyter_output>3 0.4<jupyter_text>Observed configurations<jupyter_code>## Histogram of energy of observed configurations
bins = np.linspace(-5.0,-1.0,11,endpoint=True)
bins2 = bins[:-1] #+ 0.03

histo = np.array([np.histogram(energy[:,i],bins)[0]/energy.shape[0] for i in range(len(eps))])
print(histo.shape)

i1 = i_eps - 3
i2 = i_eps + 3

nx,ny = 1,1
fig, ax = plt.subplots(ny,nx,figsize=(nx*4,ny*2.8))

ax.plot(bins2,histo[i1],'k^--',label='eps=%1.1f'%eps[i1])
ax.plot(bins2,histo[i_eps],'ro-',label='eps=%1.1f'%eps[i_eps])
ax.plot(bins2,histo[i2],'bv--',label='eps=%1.1f'%eps[i2])
fig.legend()
ax.set_title('observed configs')

ax.set_xlabel('energy')
ax.set_ylabel('histogram')<jupyter_output>(9, 10)<jupyter_text>Unobserved configurations<jupyter_code># Histogram of energy of unobserved configurations
bins = np.linspace(-1.5,1.5,11,endpoint=True)
bins2 = bins[:-1] #+ 0.03
histo_unobs = np.array([np.histogram(energy_unobs[:,i],bins)[0]/energy_unobs.shape[0] for i in range(len(eps))])
print(histo_unobs.shape)

i1 = i_eps - 3
i2 = i_eps + 3

nx,ny = 1,1
fig, ax = plt.subplots(ny,nx,figsize=(nx*4,ny*2.8))

ax.plot(bins2,histo_unobs[i1],'k^--',label='eps=%1.1f'%eps[i1])
ax.plot(bins2,histo_unobs[i_eps],'ro-',label='eps=%1.1f'%eps[i_eps])
ax.plot(bins2,histo_unobs[i2],'bv--',label='eps=%1.1f'%eps[i2])
fig.legend()
ax.set_title('unobserved configs')

ax.set_xlabel('energy')
ax.set_ylabel('histogram')<jupyter_output>(9, 10)<jupyter_text>Entropy<jupyter_code>## Entropy of training set as functions of epsilon
prob = np.exp(energy)
prob /= prob.sum(axis=0)[np.newaxis,:]
entropy = -(prob*np.log(prob)).sum(axis=0)

## Entropy of test set as functions of epsilon
prob_test = np.exp(energy_test)
prob_test /= prob_test.sum(axis=0)[np.newaxis,:]
entropy_test = -(prob_test*np.log(prob_test)).sum(axis=0)
nx,ny = 2,1
fig, ax = plt.subplots(ny,nx,figsize=(nx*3,ny*2.8))
ax[0].plot(eps[:cut],entropy[:cut],'ko-')
ax[1].plot(eps[:cut],entropy_test[:cut],'ko-')

for i in range(nx):
    ax[i].set_xlabel('epsilon')
    ax[i].set_ylabel('entropy')

ax[0].set_title('training set')
ax[1].set_title('test set')

plt.tight_layout(h_pad=0.7, w_pad=1.5)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20034843205574912, 'fraction_numerical': 0.03683424589347934, 'mean_word_length': 7.202040816326531, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '382508', 'license': 'MIT', 'n_tokens_mistral': 1845, 'n_tokens_neox': 1787, 'n_words': 230, 'path': 'Ref/1find_eps_energy_DKL_entropy_histogram.ipynb', 'repo_name': 'danhtaihoang/e-machine', 'size': '4018'}