<jupyter_start><jupyter_text>TuningCurve_Noncyclic class<jupyter_code>fp = 5.0 # 1.0 for gaussian, e.g.
fm = 0.1
numNeuro = 3
numBin = 9
tuning0 = np.random.uniform(fm, fp, (numNeuro, numBin))
weight0 = np.random.uniform(0, 1, numBin)#1.0*np.ones(numBin)/numBin
weight0 /= np.sum(weight0)

fig = plt.figure()
_ = plot_funcs_in_figure(fig, tuning0, weight0, nrow=numNeuro, ncol=1, 
                         fp = fp + 0.01, fm = fm - 0.01)


inv_cov_matrix = TuningCurve_Noncyclic.sigma_to_inv_cov(sigma=0.15, nNeuro=3)
inv_cov_matrix
tc0 = TuningCurve_Noncyclic('Poisson', tuning0, weight0)
# tc0 = TuningCurve_Noncyclic('GaussianHomo', tuning0, weight0, inv_cov_matrix)
tc0.plot()
tc0.plot_bars()
tc0.plot_circles(grid_shape=(3, 3, 3), max_point_size=100)
_ = tc0.plot_3dcube()<jupyter_output><empty_output><jupyter_text>TuningCurveOptimizer_Noncyclic class<jupyter_code>tc_opt = TuningCurveOptimizer_Noncyclic('Poisson', tc0, fp, fm, num_threads = 8)
# tc_opt = TuningCurveOptimizer_Noncyclic('GaussianHomo', tc0, fp, fm, num_threads = 8)
# dir(tc_opt) # all attributes
len(dir(tc_opt))
tc_opt.check_list_len()
tc_opt.plot_res_id(0)
tc = tc_opt.take_res_id(0)
tc.plot()<jupyter_output><empty_output><jupyter_text>maximizing the mutual information through sgd & blahut-arimoto<jupyter_code>tc_opt.iterate(total_num_iter=5, 
               sgd_learning_rate=2.0,
               sgd_batch_size=1000, # batch size for sgd
               sgd_iter_steps=100, # number of steps for sgd in every cycle
               ba_batch_size=1000, # batch size for Monte Carlo in Blahut-Arimoto
               ba_iter_steps=10, # number of steps for Blahut-Arimoto in every cycle
               save_steps=1, # save every several steps
               alter_save=True, # save the results for two alternative steps separately 
              )
tc_opt.check_list_len()
tc_opt.mark_list
tc_opt.plot_info(alternate=True)
tc_opt.plot_info()
# reset to previous results
tc_opt.reset(2)
tc_opt.check_list_len()
# live plotting during iteration
%matplotlib notebook

ncol = 1
nrow = tc_opt.numNeuro
fig = plt.figure(figsize = (ncol*6, nrow*2))
ax_list = []
for i in range(tc_opt.numNeuro):
    ax = fig.add_subplot(nrow,ncol,i+1)
    ax_list.append(ax)

for i in range(tc_opt.numNeuro):
    xx, yy = pc_fun_weights(tc_opt.tuning[i,:], tc_opt.weight)
    ax_list[i].plot(xx, yy)
    ax_list[i].set_ylim([tc_opt.fm-0.1, tc_opt.fp+0.1])
#     ax_list[i].scatter(np.arange(numBin), tuning[i, :])

fig.canvas.draw()

tc_opt.iterate(total_num_iter=20, sgd_learning_rate=0.1, laplacian_coeff=0.01,              
               plot_live=True,
               plot_steps=1, # plot every several steps
               live_ax_list=ax_list,
               live_fig=fig,
              )
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Maximizing the lower bound of mutual information<jupyter_code>tc_opt.lower_bound_iterate(total_num_iter=50,
                           lbgd_learning_rate=5.0,
                           lbgd_iter_steps=10,
                           lbw_iter_steps=1,
                           save_steps=10,
                           #compute_lbinfo=False,
                          )
tc_opt.mark_list
tc_opt.check_list_len()
tc_opt.lower_bound_iterate(total_num_iter=50,
                           lbgd_learning_rate=5.0,
                           lbgd_iter_steps=10,
                           lbw_iter_steps=1,
                           save_steps=10,
                           alter_save=False, # do not save alternatively
                           compute_lbinfo=False, # do not save the lower bound
                          )
fig = plt.figure()
ax = fig.add_subplot(111)
tc_opt.plot_info(ax, alternate=True)
tc_opt.plot_info(ax, alternate=True, mark1='lbgd', mark2='lbw', color1='g', color2='k')<jupyter_output><empty_output><jupyter_text>plotting and saving results<jupyter_code>tc_opt.plot_res_id(-1)
_ = tc_opt.take_res_id(-1).plot_3dcube()
fig = plt.figure()
ax = fig.add_subplot(111)
_ = tc_opt.take_res_id(-1).plot_hist(ax, bins=20)#neuro_id=0
# plot animations
# tc_opt.plot_animation()
tc_opt.plot_animation_cube(file_name='temp', add_time=False)
# save results
tc_opt.save_res_dict(file_name='temp', add_time=False)
# load results
tc_opt =TuningCurveOptimizer_Noncyclic.load_res_dict("temp.npy")<jupyter_output><empty_output><jupyter_text>2d laplacian<jupyter_code>tc_opt.reset(0)
tc_opt.laplacian_2d, tc_opt.laplacian_shape
tc_opt.iterate(total_num_iter=20, sgd_learning_rate=0.25, laplacian_coeff=0.01,
               save_steps=10,
               sgd_batch_size=10000, ba_batch_size=10000,
               laplacian_2d=True,
               laplacian_shape=(3,3,3),
              )
tc_opt.laplacian_2d, tc_opt.laplacian_shape
%matplotlib inline
_ = tc_opt.take_res_id(-1).plot_circles(grid_shape=tc_opt.laplacian_shape, max_point_size=100)<jupyter_output><empty_output><jupyter_text>Inhomogeneous Gaussian<jupyter_code>fp = 1.0#5.0
fm = 0.001#0.1
numNeuro = 3
numBin = 9
tuning0 = np.random.uniform(fm, fp, (numNeuro, numBin))
weight0 = np.random.uniform(0, 1, numBin)#1.0*np.ones(numBin)/numBin
weight0 /= np.sum(weight0)
inv_cov_matrix = np.zeros((numNeuro,numNeuro, numBin))
for j in range(numBin):
    sigma_j = 0.1+0.05*j
    
#     inv_cov_matrix[:,:,j] = TuningCurve_Noncyclic.sigma_to_inv_cov(sigma=0.1+0.05*j, nNeuro=numNeuro)
    inv_cov_matrix[:,:,j] += 0.01
    np.fill_diagonal(inv_cov_matrix[:,:,j], 1.0/sigma_j**2)
    print(inv_cov_matrix[:,:,j])
tc0 = TuningCurve_Noncyclic('GaussianInhomo', tuning0, weight0, inv_cov_matrix)
tc0.plot()
tc_opt = TuningCurveOptimizer_Noncyclic('GaussianInhomo', tc0, fp, fm, num_threads = 8)
tc_opt.iterate(total_num_iter=50, 
               sgd_learning_rate=0.2, 
               laplacian_coeff=0.01,
               sgd_batch_size=1000, 
               ba_batch_size=1000,
               save_steps=10,
               )
tc_opt.plot_res_id(-1)
# no correlation case:
# 'Poisson-like' variance
inv_cov_diag = 1.0/tuning0 # shape = (numNeuro, numBin)
tc0 = TuningCurve_Noncyclic('GaussianInhomoNoCorr', tuning0, weight0, inv_cov_diag)
tc0.plot()
tc_opt = TuningCurveOptimizer_Noncyclic('GaussianInhomoNoCorr', tc0, fp, fm, num_threads = 8)
tc_opt.iterate(total_num_iter=50, 
               sgd_learning_rate=0.2, 
               laplacian_coeff=0.01,
               sgd_batch_size=1000, 
               ba_batch_size=1000,
               save_steps=10,
               )
tc_opt.plot_res_id(-1)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11486278137526981, 'fraction_numerical': 0.03700277520814061, 'mean_word_length': 3.350771294433266, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '188778', 'license': 'MIT', 'n_tokens_mistral': 2617, 'n_tokens_neox': 2482, 'n_words': 360, 'path': 'examples/alternative_algorithm_class_example.ipynb', 'repo_name': 'zhaoliuUT/tuning', 'size': '6486'}