Exemple #1
0
def main():
    # Generate synthetic data
    x = 2 * npr.rand(N, D) - 1  # data features, an (N,D) array
    x[:, 0] = 1
    th_true = 10.0 * np.array([0, 1, 1])
    y = np.dot(x, th_true[:, None])[:, 0]
    t = npr.rand(N) > (1 / (1 + np.exp(y))
                       )  # data targets, an (N) array of 0s and 1s

    # Obtain joint distributions over z and th
    model = ff.LogisticModel(x, t, th0=th0, y0=y0)

    # Set up step functions
    th = np.random.randn(D) * th0
    z = ff.BrightnessVars(N)
    th_stepper = ff.ThetaStepMH(model.log_p_joint, stepsize)
    z__stepper = ff.zStepMH(model.log_pseudo_lik, q)

    plt.ion()
    ax = plt.figure(figsize=(8, 6)).add_subplot(111)
    while True:
        th = th_stepper.step(th, z)  # Markov transition step for theta
        z = z__stepper.step(th, z)  # Markov transition step for z
        update_fig(ax, x, y, z, th, t)
        plt.draw()
        plt.pause(0.05)
Exemple #2
0
def main():
    global N
    global D

    x, t = data.load_logistic_data()
    print x.shape, t.shape
    N, D = x.shape
    y0 = 1.5  # \xce in paper

    performance_results = []

    model_mcmc = ff.LogisticModel(x, t, th0=th0, y0=y0, name="regular mcmc")
    mcmc_performance = run_model(model_mcmc)
    performance_results.append(mcmc_performance)

    model_untuned_flymc = ff.LogisticModel(x,
                                           t,
                                           th0=th0,
                                           y0=y0,
                                           name="untuned_flymc")
    untuned_flymc_performance = run_model(model_untuned_flymc, q=0.1,
                                          fly=True)  # q = prob(dim -> bright)
    performance_results.append(untuned_flymc_performance)

    tmp_model = ff.LogisticModel(x, t, th0=th0)
    th_map = optimize.minimize(
        fun=lambda th: -1.0 * tmp_model.log_p_marg(th),
        x0=np.random.randn(D) * th0,
        jac=lambda th: -1.0 * tmp_model.D_log_p_marg(th),
        method='BFGS',
        options={
            'maxiter': 100,
            'disp': True
        })

    model_tuned_flymc = ff.LogisticModel(x,
                                         t,
                                         th0=th0,
                                         th_map=th_map.x,
                                         name="tuned_flymc")
    tuned_flymc_performance = run_model(model_tuned_flymc, q=0.01, fly=True)
    performance_results.append(tuned_flymc_performance)

    save_results(performance_results)
Exemple #3
0
def logistic_regression_chain(x,
                              t,
                              N_iter=100,
                              stepsize=1,
                              th0=1,
                              q=0.1,
                              y0=1,
                              seed=None):

    # Set seed
    npr.seed(seed)

    # Obtain joint distributions over z and th and set step functions
    model = ff.LogisticModel(x, t, th0=th0, y0=y0)
    z__stepper = ff.zStepMH(model.log_pseudo_lik, q)
    th_stepper = ff.ThetaStepMH(model.log_p_joint, stepsize)

    # Initialize
    N, D = x.shape
    th = np.random.randn(D) * th0
    z = ff.BrightnessVars(N)

    # run chain
    th_chain = np.zeros((N_iter, ) + th.shape)
    z_chain = np.zeros((N_iter, N), dtype=bool)

    for i in range(N_iter):
        th = th_stepper.step(th, z)
        z = z__stepper.step(th, z)
        # Record the intermediate results
        th_chain[i, :] = th.copy()
        z_chain[i, z.bright] = 1

    print "th0 = ", th0, "frac accepted is", th_stepper.frac_accepted, "bright frac is:", np.mean(
        z_chain)
    return th_chain, z_chain
def main():
    mndata = MNIST('.')
    mndata.load_training()
    #mndata.load_testing()

    ss_idx = filter(lambda i: mndata.train_labels[i] in [7, 9],
                    range(len(mndata.train_images)))
    data_ss = np.array(mndata.train_images)[ss_idx, :]
    label_ss = np.array(mndata.train_labels)[ss_idx]

    pca = PCA(n_components=10)  # TODO: change to 50
    pca.fit(data_ss)
    x = pca.transform(data_ss)
    x = np.concatenate((x, np.ones((x.shape[0], 1))), axis=1)
    t = label_ss == 7

    N, D = x.shape
    y0 = 1.5  # \xce in paper

    # Generate synthetic data
    # x = 2 * npr.rand(N,D) - 1  # data features, an (N,D) array
    # x[:, 0] = 1
    # th_true = 10.0 * np.array([0, 1, 1])
    # y = np.dot(x, th_true[:, None])[:, 0]
    # t = npr.rand(N) > (1 / ( 1 + np.exp(y)))  # data targets, an (N) array of 0s and 1s

    # Obtain joint distributions over z and th

    # Set up step functions
    def run_model(model, th_init=np.random.randn(D) * th0, q=0.1, fly=False):
        th = th_init
        if fly:
            z = ff.BrightnessVars(N)
        else:
            z = ff.BrightnessVars(N, range(N))
        th_stepper = ff.ThetaStepMH(model.log_p_joint, stepsize)
        if fly:
            z__stepper = ff.zStepMH(model.log_pseudo_lik, q)
        ths = []
        num_rejects = 0
        for i in range(N_steps):
            num_lik_prev = model.num_lik_evals
            if i % N_ess == 0 and i > 0:
                #print pypmc.tools.convergence.ess(ths) # TODO: is this correct?
                #print ess(ths)
                np.savetxt('trace-untuned-{0}.csv'.format(i), np.array(ths))
                ths = []
            th = th_stepper.step(th, z)  # Markov transition step for theta
            num_rejects += th_stepper.num_rejects
            if fly:
                z = z__stepper.step(th, z)  # Markov transition step for z
            ths.append(th)
            print "\t\t".join(
                map(lambda x: "{0:.5f}".format(x), [
                    i,
                    len(z.bright), model.num_lik_evals - num_lik_prev,
                    1.0 - num_rejects / float(i + 1),
                    -1.0 * model.log_p_marg(th, increment_ctr=False)
                ]))
        return th

    def ess(th_list):
        th = np.array(th_list)
        th_mean = np.mean(th, axis=0)

        def autocorr(x, t):
            return np.mean(
                (x[0:len(x) - t, :] - th_mean) * (x[t:len(x), :] - th_mean))

        return 1.0 * th.shape[0] / (
            1.0 +
            2.0 * sum(map(lambda t: autocorr(th, t), range(1, th.shape[0]))))

    # print ess([
    #     np.array([1]),
    #     np.array([1.1]),
    #     np.array([0.9]),
    #     np.array([1])
    #     ])

    print "Running MCMC"
    #model_mcmc = ff.LogisticModel(x, t, th0=th0, y0=y0)
    #print run_model(model_mcmc)

    print "Running untuned FlyMC"
    #model_flymc = ff.LogisticModel(x, t, th0=th0, y0=y0)
    #print run_model(model_flymc, q=0.1, fly=True) # q = prob(dim -> bright)

    print "Running MAP-tuned FlyMC"
    _model = ff.LogisticModel(x, t, th0=th0)
    th_map = optimize.minimize(fun=lambda th: -1.0 * _model.log_p_marg(th),
                               x0=np.random.randn(D) * th0,
                               jac=lambda th: -1.0 * _model.D_log_p_marg(th),
                               method='BFGS',
                               options={
                                   'maxiter': 100,
                                   'disp': True
                               })
    model_flymc_map = ff.LogisticModel(x, t, th0=th0, th_map=th_map.x)
    print run_model(
        model_flymc_map,
        #th_init=th_map.x, # TODO: is it okay to initialize at the MAP?
        q=0.01,
        fly=True)
 def random_model(self, th_map=None):
     x = npr.randn(self.N, self.D)
     t = npr.randint(2, size=self.N)
     return ff.LogisticModel(x, t, th_map=th_map)
def create_toy_logistic_model():
    x = np.array([[2.2, 1.7], [-1.5, 2.9], [1.4, -1.2]])
    t = np.array([1, 0, 1])
    th0 = 1.2
    y0 = 2
    return ff.LogisticModel(x, t, th0=th0, y0=y0)
def main():
    def run_model(model, q=0.1, fly=False):
        '''
        function to run model
        '''
        th = np.random.randn(D) * th0
        if fly:
            z = ff.BrightnessVars(N, range(int(q * N)))
        else:
            z = ff.BrightnessVars(N, range(N))
        th_stepper = ff.ThetaStepMH(model.log_p_joint, stepsize)
        if fly:
            z__stepper = ff.zStepMH(model.log_pseudo_lik, q)
        th_lists = []  # trace of th
        num_rejects_list = []  #
        num_iter_list = []  # number of num_lik_evals for each iteration
        for _ in range(N_steps):
            num_lik_prev = model.num_lik_evals
            if _ % N_ess == 0 and _ > 0:
                #print pypmc.tools.convergence.ess(th_lists) # TODO: is this correct?
                #print ess(th_lists)
                np.savetxt('trace-untuned-{0}.csv'.format(_),
                           np.array(th_lists))
                th_lists = []
            th = th_stepper.step(th, z)  # Markov transition step for theta

            if fly:
                z = z__stepper.step(th, z)  # Markov transition step for z
            th_lists.append(th)
            num_rejects_list.append(th_stepper.num_rejects)
            num_iter_list.append(model.num_lik_evals - num_lik_prev)
            print "Accept rate: {0}".format(1.0 - sum(num_rejects_list) /
                                            float(_ + 1))
            print "Likelihood evals in iter {0}: {1}".format(
                _, model.num_lik_evals - num_lik_prev)
            print "Neg log posterior: {0}".format(
                -1.0 * model.log_p_marg(th, increment_ctr=False))
            print "Number bright points: {0}".format(len(z.bright))

        return th, num_iter_list, th_lists

    # preprocess data and save to .npy file
    # so tha we dont have to PCA them each time
    # preprocess()

    x = np.load('logistic_x.npy')
    t = np.load('logistic_t.npy')
    print x.shape, t.shape
    N, D = x.shape
    y0 = 1.5  # \xce in paper

    def ess(th_list):
        th = np.array(th_list)
        th_mean = np.mean(th, axis=0)

        def autocorr(x, t):
            return np.mean(
                (x[0:len(x) - t, :] - th_mean) * (x[t:len(x), :] - th_mean))

        return 1.0 * th.shape[0] / (
            1.0 +
            2.0 * sum(map(lambda t: autocorr(th, t), range(1, th.shape[0]))))

    # print ess([
    #     np.array([1]),
    #     np.array([1.1]),
    #     np.array([0.9]),
    #     np.array([1])
    #     ])

    # model_mcmc = ff.LogisticModel(x, t, th0=th0, y0=y0)
    # print model_mcmc.num_lik_evals
    # th_mcmc, num_iter_list_mcmc, th_lists_mcmc = run_model(model_mcmc)
    # print model_mcmc.num_lik_evals

    model_flymc = ff.LogisticModel(x, t, th0=th0, y0=y0)
    # print model_flymc.num_lik_evals
    th_flymc, num_iter_list_flymc, th_lists_flymc = run_model(
        model_flymc, q=0.1, fly=True)  # q = prob(dim -> bright)
    # print model_flymc.num_lik_evals

    #_model = ff.LogisticModel(x, t, th0=th0)
    #th = np.random.randn(D) * th0
    #th_map = optimize.minimize(lambda x: -1*_model.log_p_marg(x), th)
    #model_flymc_map = ff.LogisticModel(x, t, th0=th0, th_map=th_map.x)
    #print run_model(model_flymc_map, q=0.01, fly=True)
    #print model_flymc_map.num_lik_evals

    # output traces to .csv
    # print num_iter_list_flymc
    # with open('num_iter_list_mcmc', 'wb') as f:
    #     writer = csv.writer(f)
    #     writer.writerows(num_iter_list_flymc)

    # plt.plot(num_iter_list_mcmc)
    plt.plot(num_iter_list_flymc)
    plt.show()