Ejemplo n.º 1
0
# create a folder to save the results
dir_save = model_rbm.make_dir_save(parent_dir_save, prefix, learn_rate_a,
                                   learn_rate_b, learn_rate_W, reg_lambda_W,
                                   reg_alpha_W, visible_type_fixed_param,
                                   hidden_type_fixed_param, maxiter,
                                   normalization_method)
# make a copy of this script in dir_save
shutil.copy(workdir + "main_test_ExpRBM_FASHIONMNIST.py", dir_save)
shutil.copy(workdir + "restricted_boltzmann_machine.py", dir_save)

# plot some some training samples
train_set_x_3way = numpy.reshape(train_set_x[:, 0:100], newshape=(28, 28, 100))
cl.plot_image_subplots(dir_save + "fig_" + prefix + "_train_samples.pdf",
                       data=train_set_x_3way[:, :, 0:100],
                       figwidth=6,
                       figheight=6,
                       colormap="gray",
                       num_col=10,
                       wspace=0.01,
                       hspace=0.001)

# train RBM
model_rbm.train(X=train_set_x,
                X_validate=test_set_x,
                batch_size=batch_size,
                pcdk=pcdk,
                NS=NS,
                maxiter=maxiter,
                learn_rate_a=learn_rate_a,
                learn_rate_b=learn_rate_b,
                learn_rate_W=learn_rate_W,
                change_rate=change_rate,
Xg, XMg = model_dbn.generate_x(pcdk=10 * sampling_pcdk,
                               NS=sampling_NS,
                               X0=None,
                               persistent=True,
                               rand_init=True,
                               init=True)
for s in range(sampling_time):
    Xg, XMg = model_dbn.generate_x(pcdk=sampling_pcdk, NS=sampling_NS)
    # plot sampled data
    sample_set_x_3way = numpy.reshape(XMg, newshape=(28, 28, 100))
    print(s)
    cl.plot_image_subplots(dir_save + "/fig_" + prefix +
                           "_pretrain_generated_samples_randinit_" + str(s) +
                           ".pdf",
                           data=sample_set_x_3way,
                           figwidth=6,
                           figheight=6,
                           colormap=None,
                           num_col=10,
                           wspace=0.01,
                           hspace=0.001)

# estimate the lower bound of the log-likelihood
T = 10000
S = 10
model_dbn.estimate_log_likelihood(X=test_set_x,
                                  Hr=None,
                                  HMr=None,
                                  a_hat_gen=None,
                                  b_hat_gen=None,
                                  estimate_logZ=True,
                                  base_rate_type="prior",
                                        NS=num_sampled_points,
                                        X0=None,
                                        init_sampling=True)

for s in range(sampling_time):
    chainX, chainXM = model_mdbn.generate_x(pcdk=pcdk_sampling,
                                            init_sampling=False)
    # plot sampled data
    sample_set_x_3way = numpy.reshape(chainXM[0],
                                      newshape=(28, 28, num_sampled_points))
    filename = dir_save + prefix + "_generated_samples_randinit_" + str(
        rand_init) + "_" + str(s) + "_V0.pdf"
    cl.plot_image_subplots(filename,
                           data=sample_set_x_3way,
                           figwidth=6,
                           figheight=6,
                           colormap=None,
                           num_col=num_col,
                           wspace=0.01,
                           hspace=0.001)
    #filename=dir_save+"mdbn_MNIST_generated_samples_randinit_"+str(rand_init)+"_"+str(s)+"_V1_1D.txt"
    #sample_set_y_1way=numpy.argmax(chainXM[1],axis=0)
    #numpy.savetxt(filename,sample_set_y_1way,fmt="%s",delimiter="\t")
    filename = dir_save + prefix + "_generated_samples_randinit_" + str(
        rand_init) + "_" + str(s) + "_V1_2D.txt"
    sample_set_y_2way = numpy.reshape(
        numpy.argmax(chainXM[1], axis=0),
        newshape=(num_col, int(num_sampled_points / num_col)))
    numpy.savetxt(filename, sample_set_y_2way, fmt="%s", delimiter="\t")
    filename = dir_save + prefix + "_generated_samples_randinit_" + str(
        rand_init) + "_" + str(s) + "_V1_prob.txt"
    numpy.savetxt(filename,
Ejemplo n.º 4
0
                       clamp_visible=False,
                       persistent=True,
                       rand_init_X=rand_init,
                       rand_init_H=False,
                       init_sampling=True)
for s in range(sampling_time):
    chainX, chainH, chainXM, chainXP, chainHM, chain_length_ = model_dbm.pcd_sampling(
        pcdk=1000, init_sampling=False)
    # plot sampled data
    sample_set_x_3way = numpy.reshape(chainXM, newshape=(28, 28, 100))
    print(s)
    cl.plot_image_subplots(dir_save +
                           "/fig_DBM_MNIST_generated_samples_randinit_" +
                           str(rand_init) + "_" + str(s) + ".pdf",
                           data=sample_set_x_3way,
                           figwidth=6,
                           figheight=6,
                           colormap=None,
                           num_col=10,
                           wspace=0.01,
                           hspace=0.001)

# estimate log-likelihood
loglh_test, logZ_test, Z_std_test, energy_test, entropy_test = model_dbm.estimate_log_likelihood(
    X=test_set_x,
    NMF=100,
    base_type="prior",
    beta=None,
    step_base=0.999,
    T=10000,
    S=10)
loglh_train, logZ_train, Z_std_train, energy_train, entropy_train = model_dbm.estimate_log_likelihood(