Beispiel #1
0
def test_meta_gradient_with_langevin():
    num_samples = 4
    num_langevin_steps = 3

    D = 2
    init_mean = npr.randn(D) * 0.01
    init_log_stddevs = np.log(1*np.ones(D)) + npr.randn(D) * 0.01
    init_log_stepsizes = np.log(0.01*np.ones(num_langevin_steps)) + npr.randn(num_langevin_steps) * 0.01
    init_log_noise_sizes = np.log(.001*np.ones(num_langevin_steps)) + npr.randn(num_langevin_steps) * 0.01
    init_log_gradient_scales = np.log(1*np.ones(D))
    init_gradient_power = 0.9

    sample_and_run_langevin, parser = build_langevin_sampler(logprob_two_moons, D, num_langevin_steps, approx=False)

    sampler_params = np.zeros(len(parser))
    parser.put(sampler_params, 'mean', init_mean)
    parser.put(sampler_params, 'log_stddev', init_log_stddevs)
    parser.put(sampler_params, 'log_stepsizes', init_log_stepsizes)
    parser.put(sampler_params, 'log_noise_sizes', init_log_noise_sizes)
    parser.put(sampler_params, 'log_gradient_scales', init_log_gradient_scales)
    parser.put(sampler_params, 'invsig_gradient_power', inv_sigmoid(init_gradient_power))

    def get_batch_marginal_likelihood_estimate(sampler_params):
        rs = np.random.npr.RandomState(0)
        samples, loglik_estimates, entropy_estimates = sample_and_run_langevin(sampler_params, rs, num_samples)
        marginal_likelihood_estimates = loglik_estimates + entropy_estimates
        return np.mean(marginal_likelihood_estimates)

    check_grads(get_batch_marginal_likelihood_estimate, sampler_params)
Beispiel #2
0
    sampler_learn_rate = 0.01

    init_init_stddev_scale = 1.0
    init_langevin_stepsize = 0.1
    init_langevin_noise_size = 0.001
    init_gradient_power = 0.95

    D = 2
    init_mean = np.zeros(D)
    init_stddevs = np.log(init_init_stddev_scale * np.ones((1,D)))
    init_log_stepsizes = np.log(init_langevin_stepsize * np.ones(num_steps))
    init_log_noise_sizes = np.log(init_langevin_noise_size * np.ones(num_steps))
    init_log_gradient_scales = np.log(np.ones((1,D)))

    logprob_mvn = build_logprob_mvn(mean=np.array([0.2,0.4]), cov=np.array([[1.0,0.9], [0.9,1.0]]))
    sample, parser = build_langevin_sampler(logprob_two_moons, D, num_steps, approx=False)

    sampler_params = np.zeros(len(parser))
    parser.put(sampler_params, 'mean', init_mean)
    parser.put(sampler_params, 'log_stddev', init_stddevs)
    parser.put(sampler_params, 'log_stepsizes', init_log_stepsizes)
    parser.put(sampler_params, 'log_noise_sizes', init_log_noise_sizes)
    parser.put(sampler_params, 'log_gradient_scales', init_log_gradient_scales)
    parser.put(sampler_params, 'invsig_gradient_power', inv_sigmoid(init_gradient_power))

    def get_batch_marginal_likelihood_estimate(sampler_params):
        samples, likelihood_estimates, entropy_estimates = sample(sampler_params, rs, num_samples)
        print "Mean loglik:", np.mean(likelihood_estimates.value),\
              "Mean entropy:", np.mean(entropy_estimates.value)
        plot_density(samples.value, "approximating_dist.png")
        return np.mean(likelihood_estimates + entropy_estimates)
Beispiel #3
0
    print "True normalizing constant:", log_normalizing_constant_of_a_guassian(
        all_cov)
    print "True entropy:", entropy_of_a_gaussian(all_cov)

    prior_func = build_logprob_mvn(all_mean, all_cov)

    init_mean = all_mean
    init_stddevs = np.log(init_init_stddev_scale * np.ones((1, D)))
    init_log_stepsizes = np.log(init_langevin_stepsize *
                                np.ones(num_langevin_steps))
    init_log_noise_sizes = np.log(init_langevin_noise_size *
                                  np.ones(num_langevin_steps))
    init_log_gradient_scales = np.log(np.ones((1, D)))

    sample_and_run_langevin, parser = build_langevin_sampler(
        prior_func, D, num_langevin_steps, approx=True)

    sampler_params = np.zeros(len(parser))
    parser.put(sampler_params, 'mean', init_mean)
    parser.put(sampler_params, 'log_stddev', init_stddevs)
    parser.put(sampler_params, 'log_stepsizes', init_log_stepsizes)
    parser.put(sampler_params, 'log_noise_sizes', init_log_noise_sizes)
    parser.put(sampler_params, 'log_gradient_scales', init_log_gradient_scales)
    parser.put(sampler_params, 'invsig_gradient_power',
               inv_sigmoid(init_gradient_power))

    rs = np.random.npr.RandomState(0)

    def batch_marginal_likelihood_estimate(sampler_params):
        samples, likelihood_estimates, entropy_estimates = sample_and_run_langevin(
            sampler_params, rs, num_samples)
Beispiel #4
0
    matplotlib.image.imsave("all_mean", (all_mean.reshape((28, 28))))
    labels = np.zeros((num_samples, 1))

    cond_like = generative_conditional(labels)

    D = 784
    init_mean = all_mean
    # init_stddevs = np.diag(all_cov)+.1
    # init_mean = np.zeros((1,D))
    init_stddevs = np.log(.0000001 * np.ones((1, D)))
    init_log_stepsizes = np.log(0.0001 * np.ones(num_langevin_steps))
    init_log_noise_sizes = np.log(.00001 * np.ones(num_langevin_steps))

    rs = np.random.npr.RandomState(0)

    sample_and_run_langevin, parser = build_langevin_sampler(
        simple_conditional, D, num_langevin_steps, approx=True)

    sampler_params = np.zeros(len(parser))
    parser.put(sampler_params, 'mean', init_mean)
    parser.put(sampler_params, 'log_stddev', init_stddevs)
    parser.put(sampler_params, 'log_stepsizes', init_log_stepsizes)
    parser.put(sampler_params, 'log_noise_sizes', init_log_noise_sizes)

    def get_batch_marginal_likelihood_estimate(sampler_params):
        samples, marginal_likelihood_estimates = sample_and_run_langevin(
            sampler_params, rs, num_samples)
        matplotlib.image.imsave("optimizing", (samples[0, :].reshape(
            (28, 28))).value)

        return np.mean(marginal_likelihood_estimates)
Beispiel #5
0
        likelihood = nn_loglik(trained_weights, squashed_images, labels)
        return likelihood  # prior + likelihood

    gen_labels = one_hot(np.array([i % 10 for i in range(num_samples)]), 10)
    labeled_likelihood = lambda images: nn_likelihood(images, gen_labels)

    #init_mean = all_mean
    # init_stddevs = np.diag(all_cov)+.1
    init_mean = np.zeros((1, D))
    init_stddevs = np.log(init_init_stddev_scale * np.ones((1, D)))
    init_log_stepsizes = np.log(init_langevin_stepsize *
                                np.ones(num_langevin_steps))
    init_log_noise_sizes = np.log(init_langevin_noise_size *
                                  np.ones(num_langevin_steps))

    sample_and_run_langevin, parser = build_langevin_sampler(
        labeled_likelihood, D, num_langevin_steps, approx=True)

    sampler_params = np.zeros(len(parser))
    parser.put(sampler_params, 'mean', init_mean)
    parser.put(sampler_params, 'log_stddev', init_stddevs)
    parser.put(sampler_params, 'log_stepsizes', init_log_stepsizes)
    parser.put(sampler_params, 'log_noise_sizes', init_log_noise_sizes)
    parser.put(sampler_params, 'log_gradient_scales', init_log_gradient_scales)
    parser.put(sampler_params, 'invsig_gradient_power',
               inv_sigmoid(init_gradient_power))

    rs = np.random.npr.RandomState(0)

    def batch_marginal_likelihood_estimate(sampler_params):
        samples, likelihood_estimates, entropy_estimates = sample_and_run_langevin(
            sampler_params, rs, num_samples)
Beispiel #6
0
    labels = np.zeros((num_samples,1))

    cond_like = generative_conditional(labels)

    D = 784
    init_mean = all_mean
    # init_stddevs = np.diag(all_cov)+.1
    # init_mean = np.zeros((1,D))
    init_stddevs = np.log(.00001*np.ones((1,D)))
    init_log_stepsizes = np.log(0.001*np.ones(num_langevin_steps))
    init_log_noise_sizes = np.log(.001*np.ones(num_langevin_steps))

    rs = np.random.npr.RandomState(0)

    sample_and_run_langevin, parser = build_langevin_sampler(cond_like, D, num_langevin_steps,approx = True)

    sampler_params = np.zeros(len(parser))
    parser.put(sampler_params, 'mean', init_mean)
    parser.put(sampler_params, 'log_stddev', init_stddevs)
    parser.put(sampler_params, 'log_stepsizes', init_log_stepsizes)
    parser.put(sampler_params, 'log_noise_sizes', init_log_noise_sizes)
    #
    # def get_batch_marginal_likelihood_estimate(sampler_params):
    #     samples = [] # np.zeros((num_samples, D))
    #     mls = []
    #     for s in range(num_samples):
    #         z, marginal_likelihood_estimate = sample_and_run_langevin(sampler_params, rs)
    #         samples.append(z.value)
    #         mls.append(marginal_likelihood_estimate)
    #