Esempio n. 1
0
def generate_figure_samples(samples_per_frame, n_frames, burnin = int(1e4)):
    """ Generates the figure

    :param samples_per_frame: number of sample steps between each frame
    :param n_frames: number of frames to draw
    :returns: None
    :rtype: None
    """
    n_samples = samples_per_frame * n_frames
    ndims = 36
    nbasis = 72

    rand_val = rand(ndims,nbasis/2,density=0.25)
    W = np.concatenate([rand_val.toarray(), -rand_val.toarray()],axis=1)
    logalpha = np.random.randn(nbasis, 1)
    poe = ProductOfT(nbatch=1, W=W, logalpha=logalpha)

    ## NUTS uses a different number of grad evals for each update step!!
    ## makes it very hard to compare against others w/ same number of update steps
    # # NUTS
    # print "NUTS"
    # nuts_init = poe.Xinit[:, 0]
    # nuts_samples = nuts6(poe.reset(), n_samples, nuts_burnin, nuts_init)[0]
    # nuts_frames = [nuts_samples[f_idx * samples_per_frame, :] for f_idx in xrange(0, n_frames)]
    # x_init = nuts_samples[0, :].reshape(ndims, 1)

    ## burnin
    print "MJHMC burnin"
    x_init = poe.Xinit #[:, [0]]
    mjhmc = MarkovJumpHMC(distribution=poe.reset(), **mjhmc_params)
    mjhmc.state = HMCState(x_init.copy(), mjhmc)
    mjhmc_samples = mjhmc.sample(burnin)
    print mjhmc_samples.shape
    x_init = mjhmc_samples[:, [0]]

    # control HMC
    print "Control"
    hmc = ControlHMC(distribution=poe.reset(), **control_params)
    hmc.state = HMCState(x_init.copy(), hmc)
    hmc_samples = hmc.sample(n_samples)
    hmc_frames = [hmc_samples[:, f_idx * samples_per_frame].copy() for f_idx in xrange(0, n_frames)]

    # MJHMC
    print "MJHMC"
    mjhmc = MarkovJumpHMC(distribution=poe.reset(), resample=False, **mjhmc_params)
    mjhmc.state = HMCState(x_init.copy(), mjhmc)
    mjhmc_samples = mjhmc.sample(n_samples)
    mjhmc_frames = [mjhmc_samples[:, f_idx * samples_per_frame].copy() for f_idx in xrange(0, n_frames)]

    print mjhmc.r_count, hmc.r_count
    print mjhmc.l_count, hmc.l_count
    print mjhmc.f_count, hmc.f_count
    print mjhmc.fl_count, hmc.fl_count


    frames = [mjhmc_frames, hmc_frames]
    names = ['MJHMC', 'ControlHMC']
    frame_grads = [f_idx * samples_per_frame for f_idx in xrange(0, n_frames)]
    return frames, names, frame_grads
Esempio n. 2
0
def generate_figure_samples(samples_per_frame, n_frames, burnin=int(1e4)):
    """ Generates the figure

    :param samples_per_frame: number of sample steps between each frame
    :param n_frames: number of frames to draw
    :returns: None
    :rtype: None
    """
    n_samples = samples_per_frame * n_frames
    ndims = 36
    nbasis = 72

    rand_val = rand(ndims, nbasis / 2, density=0.25)
    W = np.concatenate([rand_val.toarray(), -rand_val.toarray()], axis=1)
    logalpha = np.random.randn(nbasis, 1)
    poe = ProductOfT(nbatch=1, W=W, logalpha=logalpha)

    ## NUTS uses a different number of grad evals for each update step!!
    ## makes it very hard to compare against others w/ same number of update steps
    # # NUTS
    # print "NUTS"
    # nuts_init = poe.Xinit[:, 0]
    # nuts_samples = nuts6(poe.reset(), n_samples, nuts_burnin, nuts_init)[0]
    # nuts_frames = [nuts_samples[f_idx * samples_per_frame, :] for f_idx in xrange(0, n_frames)]
    # x_init = nuts_samples[0, :].reshape(ndims, 1)

    ## burnin
    print "MJHMC burnin"
    x_init = poe.Xinit  # [:, [0]]
    mjhmc = MarkovJumpHMC(distribution=poe.reset(), **mjhmc_params)
    mjhmc.state = HMCState(x_init.copy(), mjhmc)
    mjhmc_samples = mjhmc.sample(burnin)
    print mjhmc_samples.shape
    x_init = mjhmc_samples[:, [0]]

    # control HMC
    print "Control"
    hmc = ControlHMC(distribution=poe.reset(), **control_params)
    hmc.state = HMCState(x_init.copy(), hmc)
    hmc_samples = hmc.sample(n_samples)
    hmc_frames = [hmc_samples[:, f_idx * samples_per_frame].copy() for f_idx in xrange(0, n_frames)]

    # MJHMC
    print "MJHMC"
    mjhmc = MarkovJumpHMC(distribution=poe.reset(), resample=False, **mjhmc_params)
    mjhmc.state = HMCState(x_init.copy(), mjhmc)
    mjhmc_samples = mjhmc.sample(n_samples)
    mjhmc_frames = [mjhmc_samples[:, f_idx * samples_per_frame].copy() for f_idx in xrange(0, n_frames)]

    print mjhmc.r_count, hmc.r_count
    print mjhmc.l_count, hmc.l_count
    print mjhmc.f_count, hmc.f_count
    print mjhmc.fl_count, hmc.fl_count

    frames = [mjhmc_frames, hmc_frames]
    names = ["MJHMC", "ControlHMC"]
    frame_grads = [f_idx * samples_per_frame for f_idx in xrange(0, n_frames)]
    return frames, names, frame_grads
Esempio n. 3
0
def check_variance():
    """ Simple benchmark that estimates the variance a ton of times and writes to file the results

    :returns: Nothing
    :rtype: None
    """
    from mjhmc.misc.gen_mj_init import generate_initialization
    np.random.seed(2015)
    poe = ProductOfT(nbatch=1000, ndims=36, nbasis=36)
    var_estimates = []
    for trial_idx in xrange(100):
        _, var_estimate = generate_initialization(poe.reset())
        var_estimates.append(var_estimate)
        with open("var_log.txt", 'a') as vlog:
            vlog.write("Trial {} variance {}\n".format(trial_idx, var_estimate))
            vlog.write("Variance of variance estimates so far {}".format(np.var(var_estimates)))
Esempio n. 4
0
def sampler_speedometer():
    """ Tests average sample speed of different samplers in different configurations

    :returns: Just prints info
    :rtype: None
    """

    gaussian = Gaussian()
    np.random.seed(2015)
    pot = ProductOfT(ndims=36,nbasis=36)
    mjhmc_gauss = MarkovJumpHMC(distribution=gaussian)
    mjhmc_gauss_nr = MarkovJumpHMC(distribution=gaussian, resample=False)
    control_gauss = ControlHMC(distribution=gaussian)
    mjhmc_pot = MarkovJumpHMC(distribution=pot)
    mjhmc_pot_nr = MarkovJumpHMC(distribution=pot, resample=False)
    control_pot = ControlHMC(distribution=pot)

    m_g_r_avg = time_per_sample(mjhmc_gauss)
    m_g_nr_avg = time_per_sample(mjhmc_gauss_nr)
    c_g_avg = time_per_sample(control_gauss)

    m_p_r_avg = time_per_sample(mjhmc_pot)
    m_p_nr_avg = time_per_sample(mjhmc_pot_nr)
    c_p_avg = time_per_sample(control_pot)

    print "Average times per samples..."
    print "resampled MJHMC numpy gradient: {}".format(m_g_r_avg)
    print "not resampled MJHMC numpy gradient: {}".format(m_g_nr_avg)
    print "control HMC numpy gradient: {}".format(c_g_avg)

    print "resampled MJHMC theano gradient: {}".format(m_p_r_avg)
    print "not resampled MJHMC theano gradient: {}".format(m_p_nr_avg)
    print "control HMC theano gradient: {}".format(c_p_avg)
Esempio n. 5
0
def main(job_id, params):
    ndims = 100
    nbasis = 100
    rand_val = rand(ndims, nbasis / 2, density=0.25)
    print "job id: {}, params: {}".format(job_id, params)
    return obj_func(MarkovJumpHMC,
                    ProductOfT(nbatch=25, ndims=ndims, nbasis=nbasis), job_id,
                    **params)
Esempio n. 6
0
def main(job_id, params):
    ndims = 256
    nbasis = 72
    rand_val = rand(ndims,nbasis/2,density=0.25)
    W = np.concatenate([rand_val.toarray(), -rand_val.toarray()],axis=1)
    logalpha = np.random.randn(nbasis, 1)
    print "job id: {}, params: {}".format(job_id, params)
    return obj_func(ControlHMC, ProductOfT(nbatch=250,ndims=ndims,nbasis=nbasis, W=W, logalpha=logalpha), job_id, **params)
Esempio n. 7
0
def main(job_id, params):
    ndims = 36
    nbasis = 36
    print "job id: {}, params: {}".format(job_id, params)
    weights, lognu = init_weights(ndims, nbasis)
    return obj_func(
        ControlHMC,
        ProductOfT(nbatch=25,
                   ndims=ndims,
                   nbasis=nbasis,
                   lognu=lognu,
                   W=weights), job_id, **params)
Esempio n. 8
0
def ac_plot(n_samples=5000, **kwargs):
    """ Plots the autocorrelation for the best found parameters of the 36
    dimensional product of experts

    :returns: None
    :rtype: None
    """

    from mjhmc.figures.ac_fig import plot_best
    ndims = 36
    nbasis = 36

    np.random.seed(2015)
    poe = ProductOfT(nbatch=25,ndims=ndims,nbasis=nbasis)
    plot_best(poe, num_steps=n_samples, update_params=False, **kwargs)
from mjhmc.figures import ac_fig
from mjhmc.misc.distributions import ProductOfT
import numpy as np

np.random.seed(2015)


#Search for best hyper-parameters

#Parameters for the distribution object
ndims = 36
nbasis = 36
nbatch = 25
POT = ProductOfT(nbasis=nbasis,nbatch=nbatch,ndims=ndims)

#Run a comparison
ac_fig.plot_best(POT,num_steps=100000,update_params=True)