Ejemplo n.º 1
0
#gp.acceptable_number_types = 'no nans or infs'

# parameters
epoch = cfg.epochs - 1
#epoch = 9
use_ruslan = False

# load dataset
X, TX = rbmutil.load_mnist(False)

# load ruslan's training set
mdata = scipy.io.loadmat("mnist.mat")
X = gp.as_garray(mdata['fbatchdata'])

# enter output directory
rbmutil.enter_rbm_plot_directory("mnist", cfg.n_hid, cfg.use_pcd, cfg.n_gibbs_steps,
                                 "prob.txt", clean=False)

# Build RBM
rbm = RestrictedBoltzmannMachine(0, cfg.n_vis, cfg.n_hid, 0) 

# load Ruslan's RBM
if use_ruslan:
    print "Loading Ruslan's ml.rbm..."
    mdata = scipy.io.loadmat("matlab_epoch%d.mat" % (epoch + 1))
    ml.rbm.bias_vis = gp.as_garray(mdata['visbiases'][0,:])
    ml.rbm.bias_hid = gp.as_garray(mdata['hidbiases'][0,:])
    ml.rbm.weights = gp.as_garray(mdata['vishid'])
else:
    rbmutil.load_parameters(rbm, "weights-%02i.npz" % epoch)

# load pratition function
Ejemplo n.º 2
0
                            np.linspace(0.5, 0.9,  4000, endpoint=False),
                            np.linspace(0.9, 1.0, 10000)))
ais_base_samples = 10000
ais_base_chains = 1000
ais_base_gibbs_steps_between_samples = 1000

# initialize
tr_lps = []
tst_lps = []

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