コード例 #1
0
ファイル: mnist_base_rbm.py プロジェクト: surban/ml
# enter output directory
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
コード例 #2
0
ファイル: mnist_ais.py プロジェクト: surban/ml
#np.seterr(all='raise')
#gp.acceptable_number_types = 'no nans or infs'


# enter output directory
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 "\
コード例 #3
0
ファイル: mnist_performance.py プロジェクト: surban/ml
for i in range(n_iterations):
    seed = int(time.time())
    print "iteration %d / %d with seed %d" % (i, n_iterations, seed)

    # create output directory
    rbmutil.enter_rbm_plot_directory("mnist-%015d" % seed, cfg.n_hid, cfg.use_pcd, 
                                     cfg.n_gibbs_steps, "performance.txt")

    # train rbm
    print "Training ml.rbm..."
    rbm = mnist_rbm.train_rbm(seed=seed, plot_samples=False)

    # estimate PF using AIS
    print "Estimating partition function using %d AIS runs with %d intermediate "\
          "RBMs and %d Gibbs steps..." % (ais_runs, len(ais_betas), ais_gibbs_steps)
    ais = AnnealedImportanceSampler(rbm, ais_base_samples, ais_base_chains,
                                    ais_base_gibbs_steps_between_samples)    
    lpf, lpf_m_3s, lpf_p_3s = ais.log_partition_function(ais_betas, ais_runs, 
                                                         ais_gibbs_steps)    
    ml.rbm.log_pf = lpf

    # calculate log probability of training and test set
    tr_lp = gp.mean(rbm.normalized_log_p_vis(mnist_rbm.X))
    tst_lp = gp.mean(rbm.normalized_log_p_vis(mnist_rbm.TX))
    print "Average log p(x from training set) =  %f" % tr_lp
    print "Average log p(x from test set) =      %f" % tst_lp

    # accumulate statistics
    tr_lps.append(tr_lp)
    tst_lps.append(tst_lp)

    # save statistics