コード例 #1
0
ファイル: poe_fig.py プロジェクト: crlsmcl/MJHMC
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
コード例 #2
0
ファイル: poe_fig.py プロジェクト: rueberger/MJHMC
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
コード例 #3
0
ファイル: benchmarks.py プロジェクト: crlsmcl/MJHMC
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)))