def main():
    import metropolis_hastings_sampler
    sampling_options = {}
    sampling_options["E"] = E
    sampling_options["proposal_stddev"] = 0.1
    sampling_options["thinning_factor"] = 10000
    sampling_options["n_samples"] = 10000
    sampling_options["mcmc_method"] = "metropolis_hastings_E"
    results = metropolis_hastings_sampler.mcmc_generate_samples(sampling_options)


    import cPickle
    output_pkl_name = "ninja_star_metropolis_hasting_samples_n_%d_thin_%d_prop_stddev_%f.pkl" % (sampling_options["n_samples"], sampling_options["thinning_factor"], sampling_options["proposal_stddev"])
    f = open(output_pkl_name, "w")
    cPickle.dump(results['samples'], f)
    f.close()

    print "Acceptance ratio : %f" % results["acceptance_ratio"]
    print "Wrote %s" % (output_pkl_name, )
def main():
    import metropolis_hastings_sampler
    sampling_options = {}
    sampling_options["E"] = E
    sampling_options["proposal_stddev"] = 0.1
    sampling_options["thinning_factor"] = 10000
    sampling_options["n_samples"] = 10000
    sampling_options["mcmc_method"] = "metropolis_hastings_E"
    results = metropolis_hastings_sampler.mcmc_generate_samples(
        sampling_options)

    import cPickle
    output_pkl_name = "ninja_star_metropolis_hasting_samples_n_%d_thin_%d_prop_stddev_%f.pkl" % (
        sampling_options["n_samples"], sampling_options["thinning_factor"],
        sampling_options["proposal_stddev"])
    f = open(output_pkl_name, "w")
    cPickle.dump(results['samples'], f)
    f.close()

    print "Acceptance ratio : %f" % results["acceptance_ratio"]
    print "Wrote %s" % (output_pkl_name, )
예제 #3
0
def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "hv", [
            "help", "n_samples=", "n_chains=", "thinning_factor=", "burn_in=",
            "langevin_lambda=", "mcmc_method=", "proposal_stddev=",
            "dataset_desc=", "output_dir_prefix="
        ])
    except getopt.GetoptError as err:
        # print help information and exit:
        print str(err)  # will print something like "option -a not recognized"
        usage()
        sys.exit(2)

    sampling_options = {}
    sampling_options["n_chains"] = None

    output_options = {}

    verbose = False
    for o, a in opts:
        if o == "-v":
            # unused
            verbose = True
        elif o in ("-h", "--help"):
            usage()
            sys.exit()
        elif o in ("--n_samples"):
            sampling_options["n_samples"] = int(a)
        elif o in ("--thinning_factor"):
            sampling_options["thinning_factor"] = int(a)
        elif o in ("--burn_in"):
            sampling_options["burn_in"] = int(a)
        elif o in ("--langevin_lambda"):
            sampling_options["langevin_lambda"] = float(a)
        elif o in ("--proposal_stddev"):
            sampling_options["proposal_stddev"] = float(a)
        elif o in ("--n_chains"):
            sampling_options["n_chains"] = int(a)
        elif o in ("--mcmc_method"):
            sampling_options["mcmc_method"] = a
            #if not a in ['langevin',
            #             'metropolis_hastings_langevin_E',
            #             'metropolis_hastings_langevin_grad_E',
            #             'metropolis_hastings_E',
            #             'metropolis_hastings_grad_E']:
            #    error("Bad name for mcmc_method.")
        elif o in ("--dataset_desc"):
            if a in ['ninja_star', 'Salah_DAE', 'guillaume_DAE']:
                sampling_options["dataset_desc"] = a
            else:
                "Unrecognized dataset."
        elif o in ("--output_dir_prefix"):
            output_options['output_dir_prefix'] = a
        else:
            assert False, "unhandled option"

    if sampling_options["dataset_desc"] == "ninja_star":

        import ninja_star_distribution
        sampling_options["E"] = ninja_star_distribution.E
        sampling_options["grad_E"] = ninja_star_distribution.grad_E

        if not sampling_options["n_chains"] == None:
            sampling_options["x0"] = np.random.normal(
                size=(sampling_options["n_chains"], 2))
        else:
            sampling_options["x0"] = np.random.normal(size=(2, ))

        # this field should be obsolete
        #output_options["cross_entropy_function"] = ninja_star_distribution.cross_entropy

    elif sampling_options["dataset_desc"] == "butterfly":

        import butterfly_distribution
        sampling_options["E"] = butterfly_distribution.E
        sampling_options["grad_E"] = butterfly_distribution.grad_E

        if not sampling_options["n_chains"] == None:
            sampling_options["x0"] = np.random.normal(
                size=(sampling_options["n_chains"], 2))
        else:
            sampling_options["x0"] = np.random.normal(size=(2, ))

    elif sampling_options["dataset_desc"] == "Salah_DAE":

        import cPickle
        salah_dae_params = cPickle.load(
            open('/data/lisatmp2/rifaisal/share/sm_dae/ae_std=0.4.pkl'))

        import dae_untied_weights
        n_inputs = 784
        n_hiddens = 1024
        Wc = salah_dae_params['e_weights']
        Wb = salah_dae_params['e_weights']
        c = salah_dae_params['e_bias']
        b = salah_dae_params['d_bias']
        s = 1.0 / 16

        the_dae = dae_untied_weights.DAE_untied_weights(
            n_inputs=n_inputs,
            n_hiddens=n_hiddens,
            Wc=Wc,
            Wb=Wb,
            c=c,
            b=b,
            s=s,
            act_func=['sigmoid', 'id'])
        reference_langevin_lambda = 0.4**2
        # overwrite whichever langevin lambda was given as argument
        #sampling_options["langevin_lambda"] = reference_langevin_lambda
        r = lambda x: the_dae.encode_decode(x.reshape((-1, n_inputs))).reshape(
            (n_inputs, ))
        grad_E = lambda x: -(r(x) - x) / reference_langevin_lambda
        sampling_options["grad_E"] = grad_E

        # METHOD 1
        mnist_dataset = cPickle.load(
            open('/data/lisa/data/mnist/mnist.pkl', 'rb'))
        mnist_train = mnist_dataset[0]
        mnist_train_data, mnist_train_labels = mnist_train
        ind = np.random.randint(0, mnist_train_data.shape[0] - 1)
        sampling_options["x0"] = mnist_train_data[ind, :]
        print "Starting the simulation from MNIST digit %d" % mnist_train_labels[
            ind]
        # METHOD 3
        #sampling_options["x0"] = np.random.uniform(low=0.0, high=1.0,size=(n_inputs,))

        # these are the only valid sampling methods because they rely only on grad_E
        assert sampling_options["mcmc_method"] in [
            "langevin", "metropolis_hastings_grad_E",
            "metropolis_hastings_langevin_grad_E"
        ]

    elif sampling_options["dataset_desc"] == "guillaume_DAE":

        import cPickle
        #guillaume_dae_params = cPickle.load(open("/u/alaingui/umontreal/denoising_autoencoder/mcmc_pof/trained_models/mydae_2013_02_07.pkl"))
        guillaume_dae_params = cPickle.load(
            open(
                "/u/alaingui/umontreal/denoising_autoencoder/mcmc_pof/trained_models/mydae_2013_02_08.pkl"
            ))

        import dae_untied_weights
        n_inputs = 784
        n_hiddens = 1024
        Wc = guillaume_dae_params['Wc']
        Wb = guillaume_dae_params['Wb']
        c = guillaume_dae_params['c']
        b = guillaume_dae_params['b']
        s = guillaume_dae_params['s']
        act_func = guillaume_dae_params['act_func']

        the_dae = dae_untied_weights.DAE_untied_weights(n_inputs=n_inputs,
                                                        n_hiddens=n_hiddens,
                                                        Wc=Wc,
                                                        Wb=Wb,
                                                        c=c,
                                                        b=b,
                                                        s=s,
                                                        act_func=act_func)

        # Set to be like that because it's the value used to train
        # the DAE originally. We've already got the pkl file hardcoded
        # with the DAE parameters, so we might as well supply the lambda here.
        reference_langevin_lambda = 0.01
        #sampling_options["langevin_lambda"] = reference_langevin_lambda

        r = lambda X: the_dae.encode_decode(X.reshape((-1, n_inputs))).reshape(
            (n_inputs, ))
        r_prime = lambda x: the_dae.jacobian_encode_decode(x)
        f = lambda X: the_dae.encode(X.reshape((-1, n_inputs))).reshape(
            (n_hiddens, ))
        f_prime = lambda x: the_dae.jacobian_encode(x)
        grad_E = lambda x: -(r(x) - x) / reference_langevin_lambda
        sampling_options["grad_E"] = grad_E
        sampling_options["r"] = r
        sampling_options["r_prime"] = r_prime
        sampling_options["f"] = f
        sampling_options["f_prime"] = f_prime

        # METHOD 1
        mnist_dataset = cPickle.load(
            open('/data/lisa/data/mnist/mnist.pkl', 'rb'))
        mnist_train = mnist_dataset[0]
        mnist_train_data, mnist_train_labels = mnist_train
        ind = np.random.randint(0, mnist_train_data.shape[0] - 1)
        sampling_options["x0"] = mnist_train_data[ind, :]
        print "Starting the simulation from MNIST digit %d" % mnist_train_labels[
            ind]
        # METHOD 3
        #sampling_options["x0"] = np.random.uniform(low=0.0, high=1.0,size=(n_inputs,))

        # these are the only valid sampling methods because they rely only on grad_E
        assert sampling_options["mcmc_method"] in [
            "langevin", "metropolis_hastings_grad_E",
            "metropolis_hastings_langevin_grad_E",
            "metropolis_hastings_svd_grad_E"
        ]

    else:
        error("No dataset was supplied.")

    results = metropolis_hastings_sampler.mcmc_generate_samples(
        sampling_options)
    #
    # Returns a dictionary of this form.
    #
    # {'samples': numpy array (n_chains, n_samples, d),
    #  'elapsed_time': seconds as float,
    #  'proposals_per_second': float,
    #  'acceptance_ratio': float in [0.0,1.0] }

    print "Got the samples. Acceptance ratio was %f" % results[
        'acceptance_ratio']
    print "MCMC proposal speed was 10^%0.2f / s" % (
        np.log(results['proposals_per_second']) / np.log(10), )

    output_image_dir = "%s/%s/%d" % (output_options["output_dir_prefix"],
                                     sampling_options['mcmc_method'],
                                     int(time.time()))
    os.makedirs(output_image_dir)

    import cPickle
    output_pkl_name = os.path.join(output_image_dir, "results_and_params.pkl")
    for key in ["grad_E", "f", "f_prime", "r", "r_prime"]:
        if sampling_options.has_key(key):
            sampling_options[key] = "CANNOT BE PICKLED"
    f = open(output_pkl_name, "w")
    cPickle.dump(
        {
            'results': results,
            'sampling_options': sampling_options,
            'output_options': output_options
        }, f)
    f.close()
    print "Wrote " + output_pkl_name

    samples_only_pkl_name = os.path.join(output_image_dir, "samples.pkl")
    f = open(samples_only_pkl_name, "w")
    cPickle.dump(results['samples'], f)
    f.close()
    print "Wrote " + samples_only_pkl_name

    if sampling_options["dataset_desc"] in ["Salah_DAE", "guillaume_DAE"]:
        digits_image_file = os.path.join(output_image_dir, "digits.png")
        plot_Salah_DAE_samples(results['samples'], digits_image_file)
예제 #4
0
def main():
    try:
        opts, args = getopt.getopt(
            sys.argv[1:],
            "hv",
            [
                "help",
                "n_samples=",
                "n_chains=",
                "thinning_factor=",
                "burn_in=",
                "langevin_lambda=",
                "mcmc_method=",
                "proposal_stddev=",
                "dataset_desc=",
                "output_dir_prefix=",
            ],
        )
    except getopt.GetoptError as err:
        # print help information and exit:
        print str(err)  # will print something like "option -a not recognized"
        usage()
        sys.exit(2)

    sampling_options = {}
    sampling_options["n_chains"] = None

    output_options = {}

    verbose = False
    for o, a in opts:
        if o == "-v":
            # unused
            verbose = True
        elif o in ("-h", "--help"):
            usage()
            sys.exit()
        elif o in ("--n_samples"):
            sampling_options["n_samples"] = int(a)
        elif o in ("--thinning_factor"):
            sampling_options["thinning_factor"] = int(a)
        elif o in ("--burn_in"):
            sampling_options["burn_in"] = int(a)
        elif o in ("--langevin_lambda"):
            sampling_options["langevin_lambda"] = float(a)
        elif o in ("--proposal_stddev"):
            sampling_options["proposal_stddev"] = float(a)
        elif o in ("--n_chains"):
            sampling_options["n_chains"] = int(a)
        elif o in ("--mcmc_method"):
            sampling_options["mcmc_method"] = a
            # if not a in ['langevin',
            #             'metropolis_hastings_langevin_E',
            #             'metropolis_hastings_langevin_grad_E',
            #             'metropolis_hastings_E',
            #             'metropolis_hastings_grad_E']:
            #    error("Bad name for mcmc_method.")
        elif o in ("--dataset_desc"):
            if a in ["ninja_star", "Salah_DAE", "guillaume_DAE"]:
                sampling_options["dataset_desc"] = a
            else:
                "Unrecognized dataset."
        elif o in ("--output_dir_prefix"):
            output_options["output_dir_prefix"] = a
        else:
            assert False, "unhandled option"

    if sampling_options["dataset_desc"] == "ninja_star":

        import ninja_star_distribution

        sampling_options["E"] = ninja_star_distribution.E
        sampling_options["grad_E"] = ninja_star_distribution.grad_E

        if not sampling_options["n_chains"] == None:
            sampling_options["x0"] = np.random.normal(size=(sampling_options["n_chains"], 2))
        else:
            sampling_options["x0"] = np.random.normal(size=(2,))

        # this field should be obsolete
        # output_options["cross_entropy_function"] = ninja_star_distribution.cross_entropy

    elif sampling_options["dataset_desc"] == "butterfly":

        import butterfly_distribution

        sampling_options["E"] = butterfly_distribution.E
        sampling_options["grad_E"] = butterfly_distribution.grad_E

        if not sampling_options["n_chains"] == None:
            sampling_options["x0"] = np.random.normal(size=(sampling_options["n_chains"], 2))
        else:
            sampling_options["x0"] = np.random.normal(size=(2,))

    elif sampling_options["dataset_desc"] == "Salah_DAE":

        import cPickle

        salah_dae_params = cPickle.load(open("/data/lisatmp2/rifaisal/share/sm_dae/ae_std=0.4.pkl"))

        import dae_untied_weights

        n_inputs = 784
        n_hiddens = 1024
        Wc = salah_dae_params["e_weights"]
        Wb = salah_dae_params["e_weights"]
        c = salah_dae_params["e_bias"]
        b = salah_dae_params["d_bias"]
        s = 1.0 / 16

        the_dae = dae_untied_weights.DAE_untied_weights(
            n_inputs=n_inputs, n_hiddens=n_hiddens, Wc=Wc, Wb=Wb, c=c, b=b, s=s, act_func=["sigmoid", "id"]
        )
        reference_langevin_lambda = 0.4 ** 2
        # overwrite whichever langevin lambda was given as argument
        # sampling_options["langevin_lambda"] = reference_langevin_lambda
        r = lambda x: the_dae.encode_decode(x.reshape((-1, n_inputs))).reshape((n_inputs,))
        grad_E = lambda x: -(r(x) - x) / reference_langevin_lambda
        sampling_options["grad_E"] = grad_E

        # METHOD 1
        mnist_dataset = cPickle.load(open("/data/lisa/data/mnist/mnist.pkl", "rb"))
        mnist_train = mnist_dataset[0]
        mnist_train_data, mnist_train_labels = mnist_train
        ind = np.random.randint(0, mnist_train_data.shape[0] - 1)
        sampling_options["x0"] = mnist_train_data[ind, :]
        print "Starting the simulation from MNIST digit %d" % mnist_train_labels[ind]
        # METHOD 3
        # sampling_options["x0"] = np.random.uniform(low=0.0, high=1.0,size=(n_inputs,))

        # these are the only valid sampling methods because they rely only on grad_E
        assert sampling_options["mcmc_method"] in [
            "langevin",
            "metropolis_hastings_grad_E",
            "metropolis_hastings_langevin_grad_E",
        ]

    elif sampling_options["dataset_desc"] == "guillaume_DAE":

        import cPickle

        # guillaume_dae_params = cPickle.load(open("/u/alaingui/umontreal/denoising_autoencoder/mcmc_pof/trained_models/mydae_2013_02_07.pkl"))
        guillaume_dae_params = cPickle.load(
            open("/u/alaingui/umontreal/denoising_autoencoder/mcmc_pof/trained_models/mydae_2013_02_08.pkl")
        )

        import dae_untied_weights

        n_inputs = 784
        n_hiddens = 1024
        Wc = guillaume_dae_params["Wc"]
        Wb = guillaume_dae_params["Wb"]
        c = guillaume_dae_params["c"]
        b = guillaume_dae_params["b"]
        s = guillaume_dae_params["s"]
        act_func = guillaume_dae_params["act_func"]

        the_dae = dae_untied_weights.DAE_untied_weights(
            n_inputs=n_inputs, n_hiddens=n_hiddens, Wc=Wc, Wb=Wb, c=c, b=b, s=s, act_func=act_func
        )

        # Set to be like that because it's the value used to train
        # the DAE originally. We've already got the pkl file hardcoded
        # with the DAE parameters, so we might as well supply the lambda here.
        reference_langevin_lambda = 0.01
        # sampling_options["langevin_lambda"] = reference_langevin_lambda

        r = lambda X: the_dae.encode_decode(X.reshape((-1, n_inputs))).reshape((n_inputs,))
        r_prime = lambda x: the_dae.jacobian_encode_decode(x)
        f = lambda X: the_dae.encode(X.reshape((-1, n_inputs))).reshape((n_hiddens,))
        f_prime = lambda x: the_dae.jacobian_encode(x)
        grad_E = lambda x: -(r(x) - x) / reference_langevin_lambda
        sampling_options["grad_E"] = grad_E
        sampling_options["r"] = r
        sampling_options["r_prime"] = r_prime
        sampling_options["f"] = f
        sampling_options["f_prime"] = f_prime

        # METHOD 1
        mnist_dataset = cPickle.load(open("/data/lisa/data/mnist/mnist.pkl", "rb"))
        mnist_train = mnist_dataset[0]
        mnist_train_data, mnist_train_labels = mnist_train
        ind = np.random.randint(0, mnist_train_data.shape[0] - 1)
        sampling_options["x0"] = mnist_train_data[ind, :]
        print "Starting the simulation from MNIST digit %d" % mnist_train_labels[ind]
        # METHOD 3
        # sampling_options["x0"] = np.random.uniform(low=0.0, high=1.0,size=(n_inputs,))

        # these are the only valid sampling methods because they rely only on grad_E
        assert sampling_options["mcmc_method"] in [
            "langevin",
            "metropolis_hastings_grad_E",
            "metropolis_hastings_langevin_grad_E",
            "metropolis_hastings_svd_grad_E",
        ]

    else:
        error("No dataset was supplied.")

    results = metropolis_hastings_sampler.mcmc_generate_samples(sampling_options)
    #
    # Returns a dictionary of this form.
    #
    # {'samples': numpy array (n_chains, n_samples, d),
    #  'elapsed_time': seconds as float,
    #  'proposals_per_second': float,
    #  'acceptance_ratio': float in [0.0,1.0] }

    print "Got the samples. Acceptance ratio was %f" % results["acceptance_ratio"]
    print "MCMC proposal speed was 10^%0.2f / s" % (np.log(results["proposals_per_second"]) / np.log(10),)

    output_image_dir = "%s/%s/%d" % (
        output_options["output_dir_prefix"],
        sampling_options["mcmc_method"],
        int(time.time()),
    )
    os.makedirs(output_image_dir)

    import cPickle

    output_pkl_name = os.path.join(output_image_dir, "results_and_params.pkl")
    for key in ["grad_E", "f", "f_prime", "r", "r_prime"]:
        if sampling_options.has_key(key):
            sampling_options[key] = "CANNOT BE PICKLED"
    f = open(output_pkl_name, "w")
    cPickle.dump({"results": results, "sampling_options": sampling_options, "output_options": output_options}, f)
    f.close()
    print "Wrote " + output_pkl_name

    samples_only_pkl_name = os.path.join(output_image_dir, "samples.pkl")
    f = open(samples_only_pkl_name, "w")
    cPickle.dump(results["samples"], f)
    f.close()
    print "Wrote " + samples_only_pkl_name

    if sampling_options["dataset_desc"] in ["Salah_DAE", "guillaume_DAE"]:
        digits_image_file = os.path.join(output_image_dir, "digits.png")
        plot_Salah_DAE_samples(results["samples"], digits_image_file)
예제 #5
0
def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "hv", ["help", "omit_ninja_star_from_plots", "n_samples=", "n_chains=", "thinning_factor=", "burn_in=", "langevin_lambda=", "mcmc_method=", "proposal_stddev=", "dataset=", "output_dir_prefix=", "reference_pickled_samples_for_KL=", "reference_stddev_for_KL=", "no_plots"])
    except getopt.GetoptError as err:
        # print help information and exit:
        print str(err) # will print something like "option -a not recognized"
        usage()
        sys.exit(2)

    sampling_options = {}
    sampling_options["n_chains"] = None
    sampling_options["no_plots"] = False

    output_options = {}

    verbose = False
    for o, a in opts:
        if o == "-v":
            # unused
            verbose = True
        elif o in ("-h", "--help"):
            usage()
            sys.exit()
        if o == "--no_plots":
            sampling_options["no_plots"] = True
        elif o in ("--n_samples"):
            sampling_options["n_samples"] = int(a)
        elif o in ("--thinning_factor"):
            sampling_options["thinning_factor"] = int(a)
        elif o in ("--burn_in"):
            sampling_options["burn_in"] = int(a)
        elif o in ("--langevin_lambda"):
            sampling_options["langevin_lambda"] = float(a)
        elif o in ("--proposal_stddev"):
            sampling_options["proposal_stddev"] = float(a)
        elif o in ("--n_chains"):
            sampling_options["n_chains"] = int(a)
        elif o in ("--mcmc_method"):
            sampling_options["mcmc_method"] = a
            #if not a in ['langevin',
            #             'metropolis_hastings_langevin_E',
            #             'metropolis_hastings_langevin_grad_E',
            #             'metropolis_hastings_E',
            #             'metropolis_hastings_grad_E']:
            #    error("Bad name for mcmc_method.")
        elif o in ("--dataset"):
            if a == 'ninja_star':
                import ninja_star_distribution
                sampling_options["dataset_description"] = "ninja_star"
                sampling_options["E"] = ninja_star_distribution.E
                sampling_options["grad_E"] = ninja_star_distribution.grad_E
            if a == 'butterfly':
                import butterfly_distribution
                sampling_options["dataset_description"] = "butterfly"
                sampling_options["E"] = butterfly_distribution.E
                sampling_options["grad_E"] = butterfly_distribution.grad_E
            else:
                "Unrecognized dataset."
        elif o in ("--output_dir_prefix"):
            output_options['output_dir_prefix'] = a
        elif o in ("--reference_pickled_samples_for_KL"):
            output_options['reference_pickled_samples_for_KL'] = a
        elif o in ("--reference_stddev_for_KL"):
            output_options['reference_stddev_for_KL'] = float(a)
        elif o in ("--omit_ninja_star_from_plots"):
            output_options['omit_ninja_star_from_plots'] = True
        else:
            assert False, "unhandled option"

    if sampling_options["dataset_description"] == "ninja_star":
        if not sampling_options["n_chains"] == None:
            sampling_options["x0"] = np.random.normal(size=(sampling_options["n_chains"],2))
        else:
            sampling_options["x0"] = np.random.normal(size=(2,))
        
        # osbsolete
        #output_options["cross_entropy_function"] = ninja_star_distribution.cross_entropy

    elif sampling_options["dataset_description"] == "butterfly":
        if not sampling_options["n_chains"] == None:
            sampling_options["x0"] = np.random.normal(size=(sampling_options["n_chains"],2))
        else:
            sampling_options["x0"] = np.random.normal(size=(2,))
    else:
        error("No dataset was supplied.")


    results = metropolis_hastings_sampler.mcmc_generate_samples(sampling_options)
    #
    # Returns a dictionary of this form.
    #
    # {'samples': numpy array (n_chains, n_samples, d),
    #  'elapsed_time': seconds as float,
    #  'proposals_per_second': float,
    #  'acceptance_ratio': float in [0.0,1.0] }

    print "Got the samples. Acceptance ratio was %f" % results['acceptance_ratio']
    print "MCMC proposal speed was 10^%0.2f / s" % (np.log(results['proposals_per_second']) / np.log(10), )


    if len(results['samples'].shape) == 2:

        # We will evaluate an approximate KL divergence if we are given a
        # reference set of samples from the true distribution.
        if output_options.has_key("reference_pickled_samples_for_KL"):

            import KL_approximation
            import cPickle
            assert os.path.exists(output_options["reference_pickled_samples_for_KL"])
            f = open(output_options["reference_pickled_samples_for_KL"])
            reference_sample = cPickle.load(f)

            # Let's leave out that argument for now.
            #if output_options.has_key("reference_stddev_for_KL"):
            #    reference_stddev_for_KL = output_options["reference_stddev_for_KL"]
            #else:
            #    reference_stddev_for_KL = None

            KL_value = KL_approximation.KL(reference_sample, results['samples'], 0.5, 0.5)
            print "We got a KL divergence value of %f" % KL_value


    output_image_dir = "%s/%s/%d" % (output_options["output_dir_prefix"], sampling_options['mcmc_method'], int(time.time()) )
    os.makedirs(output_image_dir)

    import cPickle
    output_pkl_name = os.path.join(output_image_dir, "results_and_params.pkl")
    f = open(output_pkl_name, "w")
    if sampling_options.has_key("grad_E"):
        sampling_options["grad_E"] = "CANNOT BE PICKLED"
    cPickle.dump({'results':results, 'sampling_options':sampling_options, 'output_options':output_options}, f)
    f.close()
    print "Wrote " + output_pkl_name


    samples_only_pkl_name = os.path.join(output_image_dir, "samples.pkl")
    f = open(samples_only_pkl_name, "w")
    cPickle.dump(results['samples'], f)
    f.close()
    print "Wrote " + samples_only_pkl_name


    if sampling_options["dataset_description"] == "ninja_star" and not (sampling_options["no_plots"]):

        if len(results['samples'].shape) == 2:
    
            output_image_path = os.path.join(output_image_dir, "whole_chain.png")
            plot_one_slice_of_ninja_star_samples(results['samples'],
                                                 output_image_path,
                                                 dpi=200,
                                                 omit_ninja_star_from_plots = output_options.has_key('omit_ninja_star_from_plots'))

        elif len(results['samples'].shape) == 3:

            #plot_one_slice_of_ninja_star_samples_2(results['samples'],
            #                                     lambda n : os.path.join(output_image_dir, "frame_%0.6d.png" % n),
            #                                     dpi=100)

            for n in np.arange(sampling_options['n_samples']):
                output_image_path = os.path.join(output_image_dir, "frame_%0.6d.png" % n)
                plot_one_slice_of_ninja_star_samples(results['samples'][:,n,:],
                                                     output_image_path,
                                                     dpi=100,
                                                     omit_ninja_star_from_plots = output_options.has_key('omit_ninja_star_from_plots'))

                import subprocess
                #export MOVIEDIR=${HOME}/Documents/tmp/metropolis_hastings_E/1359965409
                #ffmpeg -i ${MOVIEDIR}/frame_%06d.png -vcodec libx264 -vpre hq -crf 22 -r 10 ${MOVIEDIR}/seq.mp4
                subprocess.check_output(["ffmpeg", "-i", output_image_dir + r"/frame_%06d.png", "-y", "-vcodec", "libx264", "-vpre", "hq", "-crf", "22", "-r", "10", output_image_dir + r"/assembled.mp4"])

            print "Generated movie at %s." % (output_image_dir + r"/assembled.mp4", )
        else:
            raise("Wrong shape for samples returned !")