コード例 #1
0
ファイル: mnist_ais.py プロジェクト: surban/ml
rbmutil.enter_rbm_plot_directory("mnist", cfg.n_hid, cfg.use_pcd, cfg.n_gibbs_steps,
                                 "ais.txt", clean=False)

# load RBM
rbm = RestrictedBoltzmannMachine(0, cfg.n_vis, cfg.n_hid, 0) 
rbmutil.load_parameters(rbm, "weights-%02i.npz" % epoch)

# init AIS estimator
print "Calculating base RBM biases using %d samples with %d Gibbs steps " \
    "inbetween..." % (ais_base_samples, ais_base_gibbs_steps_between_samples)
ais = AnnealedImportanceSampler(rbm, ais_base_samples, ais_base_chains,
                                ais_base_gibbs_steps_between_samples)
print "Saving base RBM biases to %s..." % filename
np.savez_compressed(filename, 
                    base_bias_vis=gp.as_numpy_array(ais.base_bias_vis))
print "Base RBM log partition function:  %f" % ais.base_log_partition_function()

# check base rbm log partition function
if check_base_rbm_partition_function:
    baserbm = RestrictedBoltzmannMachine(0, cfg.n_vis, cfg.n_hid, 0)
    baserbm.weights = gp.zeros(baserbm.weights.shape)
    baserbm.bias_hid = gp.zeros(baserbm.bias_hid.shape)
    baserbm.bias_vis = ais.base_bias_vis
    print "Base RBM log partition function using partition_func:  %f" % baserbm.partition_function(20, 50).ln()

# perform estimation of partition function
print "Estimating partition function using %dx %d AIS runs with %d intermediate "\
    "RBMs and %d Gibbs steps..." % (ais_iterations, ais_runs, len(ais_betas), ais_gibbs_steps)

with open("ais_iterations.csv", 'w') as outfile:
    outfile.write("iterations\tlog Z\n")
コード例 #2
0
ファイル: mnist_base_rbm.py プロジェクト: surban/ml
rbmutil.enter_rbm_plot_directory("mnist", cfg.n_hid, cfg.use_pcd, cfg.n_gibbs_steps,
                                 "base_rbm.txt", clean=False)

# Build RBM
rbm = RestrictedBoltzmannMachine(0, cfg.n_vis, cfg.n_hid, 0) 
rbmutil.load_parameters(rbm, "weights-%02i.npz" % epoch)

# calculate base RBM biases
print "Calculating base RBM biases using %d samples with %d Gibbs steps " \
    "inbetween..." % (ais_base_samples, ais_base_gibbs_steps_between_samples)
base_biases = np.zeros((iterations, ml.rbm.n_vis))
base_log_pf = []
for i in range(iterations):
    ais = AnnealedImportanceSampler(rbm, ais_base_samples, ais_base_chains,
                                    ais_base_gibbs_steps_between_samples)
    lpf = ais.base_log_partition_function()
    print "%2d: Base RBM log partition function:  %f" % (i, lpf)
    base_biases[i,:] = gp.as_numpy_array(ais.base_bias_vis)
    base_log_pf.append(lpf)

# calculate mean biases
base_biases_mean = np.mean(base_biases, axis=0)
base_biases_std = np.std(base_biases, axis=0)
base_log_pf_mean = np.mean(base_log_pf)
base_log_pf_std = np.std(base_log_pf)

print
print "Base RBM bias mean: \n", base_biases_mean
print
print "Base RBM bias std deviation: \n", base_biases_std
print