<jupyter_start><jupyter_text>now trying to run the reweighting code<jupyter_code>def hist_weights(network):
    sample_x, log_w = network.sample_log_w(temperature=1.0, num_samples=100000)
    log_w -= log_w.max()
    bins = np.linspace(-2.5, 2.5, 100)
    bin_means = (bins[:-1] + bins[1:]) /2
    sample_x_index = np.digitize(sample_x[:, 0], bins)
    whist = np.zeros(len(bins) + 1)
    for i in range(len(log_w)):
        whist[sample_x_index[i]] += np.exp(log_w[i])
    return bin_means, whist[1:-1]
# reweights for the discrete sequences: 
sample_x, log_w = network.sample_log_w(temperature=1.0, num_samples=100000)
if not is_discrete: 
    inp = sample_x.reshape( sample_x.shape[0], gen_model.L, -1 )
    sample_x_seqs = gen_model.decode( torch.tensor(inp).float() ).numpy()
    x = sample_x_seqs.argmax(axis=-1)
else: 
    x = hard_max(sample_x)

log_w -= log_w.max()

counter = seq_counter(x)

# account for how much weight in total is in each bin. 
whist = np.zeros_like(counter)
for i in range(len(log_w)):
    whist[x[i][0], x[i][1]] += np.exp(log_w[i] - log_w.max())
 
sns.heatmap(whist.T)
# now using the weights I need to resample the sequences. 
# get probabilities and then convert these back to scores. 

single_index_dict = dict()
for i in range(counter.shape[0] * counter.shape[1]):
    single_index_dict[i] = np.array( [i//counter.shape[0], i%counter.shape[1]])

#test_sample_rew(network, gen_model, temperature=1.0, plot=False)
single_index_dict;
whist.sum()
whist = whist/whist.sum()
n_boots = 10
energies = []
for n in range(n_boots):
    rand_inds = np.random.choice( np.arange(len(single_index_dict)), size =x.shape[0], p = whist.flatten(), replace=True )
    rand_seqs = []
    for r in rand_inds:
        rand_seqs.append(single_index_dict[r])
    rand_seqs = np.asarray(rand_seqs)
    rand_counter = seq_counter(rand_seqs)
    rand_counter = rand_counter / rand_counter.sum()
    E = -np.log(rand_counter)
    energies.append(E)
energies = np.asarray(energies)
np.mean( np.array([np.nan, 5, 6]))
# compute the mean and std! 
emean = energies.mean(axis=0)
estd = energies.std(axis=0)
emean[ np.invert(np.isfinite(emean)) ] = np.nan
estd[ np.invert(np.isfinite(estd)) ] = np.nan
# plotting the energies after reweighted bootstrapping
emean = emean - emean[np.isfinite(emean)].min() + sample_energies.min()
sns.heatmap(-emean.T)
emean.shape
top = -emean.flatten()
top = top[np.isfinite(top)].flatten()
top
plt.scatter(np.arange(len(top)), np.sort(top) )
plt.show()
# plotting probabilities of one of the reweighted bootstraps
plt.hist(gen_model.energy(rand_seqs), bins=100)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1500568397120121, 'fraction_numerical': 0.019325502084122773, 'mean_word_length': 4.534591194968553, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '413955', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1086, 'n_tokens_neox': 1040, 'n_words': 211, 'path': 'notebooks/.ipynb_checkpoints/Reweighting-checkpoint.ipynb', 'repo_name': 'TrentBrick/protein-gen', 'size': '2639'}