Beispiel #1
0
def plot2d_sinusoid_samples_section(figx, figy, eppts, line_idx, obs_proj_idx_pts, line_obs, samples_section, num_samples, u, v): # Plot the true function, observations, and posterior samples.

    plt.figure(figsize=(figx, figy))
    line_XYZ = np.linspace(u, v, num=10000) #(num, 3) 3rd dim const.
    line_XY = line_XYZ[:,0:2] # same as gpf.project_to_line_coordinates here
    line_L = gpf.create_1d_w_line_coords(line_XY, u, v) # 2D input, 1D output
    Z = gpf.sinusoid(line_XY).reshape(-1, 1) # 2D input 1D output
    line_XYZ_ = gpf.add_dimension(line_XY, Z) # 2D input, 3D output
    line_XYZL = gpf.add_dimension(line_XYZ_, line_L) # 3D input, 4D output
    predictive_index_points_ = np.linspace(-2,2,200, dtype=np.float64)
    predictive_index_points_ = predictive_index_points_[..., np.newaxis]

    plt.scatter(line_XYZL[:,3], line_XYZL[:,2], color='blue', s=0.2, alpha=0.3)  # sin cut

    # print("samples_section[i,:].T",samples_section[1, :].T )
    # # print("eppts[i , 0:2]",eppts[: , 0:2])
    # print("predictive_index_points_.shape : ", predictive_index_points_.shape)
    # print("samples_section.shape : ", samples_section.shape) # 50,1 200 instead 50,2,3
    # print("--------------")
    # plt.scatter(pred_idx_pts[:, 0], obs,
    #             label='Observations')
    for i in range(num_samples):

        plt.plot(line_idx, samples_section[i, :].T,c='r',alpha=.08,label='Posterior Sample' if i == 0 else None)
    leg = plt.legend(loc='upper right')
    plt.scatter(eppts[:, 3], eppts[:, 2], color='purple', s=40)  # observations
    # for lh in leg.legendHandles:
    #     lh.set_alpha(1)
    plt.xlabel(r"Index points ($\mathbb{R}^1$)")
    plt.ylabel("Observation space")
    plt.show()
Beispiel #2
0
def plot_section_observations(figx, figy, eppts, u, v):
    '''
    eppts : projected coordinates, extended with a 4th column
        to represent their distance from the section start point
    XY linspace along section line
    Z corresponding sinusoid value
    L corresponding distance along the line
    '''
    plt.figure(figsize=(figx,figy))
    line_XYZ = np.linspace(u, v, num=10000) #(num, 3) 3rd dim const.
    line_XY = gpf.del_last_dimension(line_XYZ) # same as gpf.project_to_line_coordinates here
    line_L = gpf.create_1d_w_line_coords(line_XY, u, v) # 2D input, 1D output
    Z = gpf.sinusoid(line_XY).reshape(-1, 1) # 2D input 1D output
    line_XYZ_ = gpf.add_dimension(line_XY, Z) # 2D input, 3D output
    line_XYZL = gpf.add_dimension(line_XYZ_, line_L) # 3D input, 4D output

    plt.scatter(eppts[:, 3], eppts[:, 2], color='orange')  # observations
    plt.scatter(line_XYZL[:,3], line_XYZL[:,2], color='red', s=0.1)  # sin cut
    plt.show()
Beispiel #3
0
def TEST_section_cut():
    '''
    3D surface section: observations and true fn in 2D plot
    '''
    #################################################################
    PLOTS = True
    NUM_PTS_GEN = 100
    RANGE_PTS = [-2, 2]
    COORDINATE_RANGE = np.array([[-2., 2.], [-2., 2.]])  # xrange, yrange
    NUM_TRAINING_POINTS = 2800
    BEGIN = np.array([-2, -2, 0])
    END = np.array([2, 2, 0])
    EPSILON = 0.2

    # pts_rand = dg.generate_noisy_2Dsin_data(NUM_PTS_GEN,0.1, COORDINATE_RANGE)  # 3d pointcloud
    # pts_rand[:, 2] *= 0.5  # transform length of 3rd scale but it was better to calc dist for XY only
    # sel_pts_rand = gpf.capture_close_pts(pts_rand)

    obs_idx_pts, obs = dg.generate_noisy_2Dsin_data(NUM_TRAINING_POINTS, 0.001,
                                                    COORDINATE_RANGE)
    obs = obs.reshape(-1, 1)  # to column vector
    pts = gpf.add_dimension(obs_idx_pts, obs)
    sel_pts = gpf.capture_close_pts_XY(pts, BEGIN, END, EPSILON)
    ext_sel_pts = gpf.extend_pts_with_line_coord(sel_pts, BEGIN, END)

    s = np.linspace(-2, 2, 120).reshape(-1, 1)

    # PLOTS
    if PLOTS:
        plts.plot_2d_observations(12, 7, pts, sel_pts, BEGIN, END)
        plts.plot_capture_line(12, 7, sel_pts, BEGIN, END)
        plts.plot_section_observations(12, 7, ext_sel_pts, BEGIN, END)
        # plts.plot_sin_and_sel_pts_at_section(x_new, plts, x_original)
        # GP

    PLOTS = False
Beispiel #4
0
def TEST_section_cut_GP():
    '''
    2D sin curve, 1D section gp fit and plots
    '''
    #################################################################
    PLOTS = True
    NUM_PTS_GEN = 100  # 1000
    RANGE_PTS = [-2, 2]
    COORDINATE_RANGE = np.array([[-2., 2.], [-2., 2.]])  # xrange, yrange
    NUM_TRAINING_POINTS = 60
    BEGIN = np.array([0, -2, 0])
    END = np.array([2, 2, 0])
    EPSILON = 0.2
    AMPLITUDE_INIT = np.array([0.1, 0.1])
    LENGTHSCALE_INIT = np.array([0.1, 0.1])
    LEARNING_RATE = .01
    INIT_OBSNOISEVAR = 1e-6
    INIT_OBSNOISEVAR_ = 0.001
    XEDGES = 60
    YEDGES = 60
    LOGDIR = "./log_dir_gp/gp_plot/"
    NUM_ITERS = 400  # 1000
    PRED_FRACTION = 50  # 50
    LOGCHECKPT = "model.ckpt"
    NUM_SAMPLES = 50  # 50
    PLOTRASTERPOINTS = 60
    PLOTLINE = 200

    sess = gpf.reset_session()
    amp, amp_assign, amp_p, lensc, lensc_assign, lensc_p, emb, emb_assign, emb_p, obs_noise_var \
        = gpf.tf_Placeholder_assign_test(AMPLITUDE_INIT, LENGTHSCALE_INIT, INIT_OBSNOISEVAR_)

    obs_idx_pts, obs = dg.generate_noisy_2Dsin_data(NUM_TRAINING_POINTS, 0.001,
                                                    COORDINATE_RANGE)

    obs_proj_idx_pts = np.linspace(-2, 2, 200)
    obs_proj_idx_pts = gpf.project_to_line_coordinates(obs_proj_idx_pts, BEGIN,
                                                       END)
    obs_proj_idx_pts = gpf.create_1d_w_line_coords(obs_proj_idx_pts, BEGIN,
                                                   END)  # 1D
    print(obs_proj_idx_pts.shape)

    obs_rowvec = obs
    obs = obs.reshape(-1, 1)  # to column vector
    pts = gpf.add_dimension(obs_idx_pts, obs)
    # sel_pts_ = np.array(gpf.capture_close_pts(obs_idx_pts, BEGIN, END, EPSILON) )  # 2D another way to do it
    sel_pts = gpf.capture_close_pts_XY(pts, BEGIN, END, EPSILON)  # 3D

    sel_obs = np.zeros((0, 1))
    for i in sel_pts[:, 2]:
        sel_obs = np.concatenate((sel_obs, i), axis=None)
    ext_sel_pts = gpf.extend_pts_with_line_coord(sel_pts, BEGIN, END)  # 4D

    train_idx_pts_along_section_line = gpf.project_to_line_coordinates(
        sel_pts, BEGIN, END)
    train_idx_pts_along_section_line = gpf.create_1d_w_line_coords(
        train_idx_pts_along_section_line, BEGIN, END)  # 1D

    line_idx = gpf.create_line_coord_linspace(BEGIN, END, PLOTLINE)  # 1D
    line_XY = gpf.create_line_linspace(BEGIN, END, PLOTLINE)  # 2D
    line_obs = gpf.create_Z_sin_along_line_linspace(line_XY)  # 1D
    # GP
    kernel = gpf.create_cov_kernel(amp, lensc)
    gp = gpf.fit_gp(
        kernel, obs_idx_pts,
        obs_noise_var)  # GP fit to 3D XYZ, where Z is sinusoid of XY

    log_likelihood = gp.log_prob(obs_rowvec)
    gpf.tf_summary_scalar("log_likelihood[0, 0]", log_likelihood[0])
    gpf.tf_summary_scalar("log_likelihood[1, 0]", log_likelihood[1])
    train_op = gpf.tf_train_gp_adam(log_likelihood, LEARNING_RATE)

    summ, writer, saver = gpf.tf_summary_writer_projector_saver(
        sess, LOGDIR)  # initializations
    [
        _,
        lensc_,
    ] = gpf.do_assign(sess, lensc_assign, lensc, lensc_p, [0.5, 0.5])
    [obs_noise_var_] = sess.run([obs_noise_var])  # run session graph

    lls = gpf.tf_optimize_model_params(sess, NUM_ITERS, train_op,
                                       log_likelihood, summ, writer, saver,
                                       LOGDIR, LOGCHECKPT, obs, _)
    [amp, lensc, obs_noise_var] = sess.run([amp, lensc, obs_noise_var])

    pred_x = np.linspace(BEGIN[0], END[0], PRED_FRACTION, dtype=np.float64)
    pred_y = np.linspace(BEGIN[1], END[1], PRED_FRACTION, dtype=np.float64)
    pred_idx_pts = gpf.create_meshgrid(
        pred_x, pred_y, PRED_FRACTION)  # posterior = predictions
    pred_sel_idx_pts = gpf.line_coords(sel_pts, BEGIN,
                                       END)  # 1D observations along x.
    # print("line_idx.shape : ", line_idx.shape) #(200, 1)
    # print("obs_idx_pts_along_section_line.shape : ", train_idx_pts_along_section_line.shape) #(100, 1)
    # print("sel_obs.shape : ", sel_obs.shape) #(100, )
    # print(repr(train_idx_pts_along_section_line))
    # print(repr(sel_obs))

    # Gaussian process regression model
    gprm = gpf.tf_gp_regression_model(kernel, pred_idx_pts, obs_idx_pts,
                                      obs_rowvec, obs_noise_var, 0.)
    gprm_section = gpf.tf_gp_regression_model(
        kernel,
        line_idx,  # (200,1)
        train_idx_pts_along_section_line,  # (9,1)
        sel_obs,  # (9,)
        obs_noise_var,
        0.)

    # posterior_mean_predict = im.tfd.gp_posterior_predict.mean()
    # posterior_std_predict = im.tfd.gp_posterior_predict.stddev()

    # samples
    # samples = gprm.sample(NUM_SAMPLES)
    # samples_ = sess.run(samples)

    samples_section = gprm_section.sample(NUM_SAMPLES)
    samples_section_ = sess.run(samples_section)

    print("kernel : ", kernel)
    samples = gprm.sample(NUM_SAMPLES)
    samples_ = sess.run(samples)

    #     print("==================")
    #     ##############
    #     samples_pts = gpf.add_dimension(obs_idx_pts, samples_[0, 0, :]) # obs_idx_pts : (81, 2) XY , sample_[0,0,:](81, 1)
    #     print("sample_pts.shape : ",samples_pts.shape)
    #
    #     samples_l = gpf.capture_close_pts_XY(samples_pts, BEGIN, END, 1*EPSILON)
    #     print("sample_l.shape : ",samples_l.shape)
    #
    #     samples_coord_along_line = np.zeros(samples_l.shape[0])
    #     for i in range(samples_l.shape[0]):
    #         d = samples_l[i,0:2] - BEGIN[0:2]
    #         dist = np.sqrt(np.dot(d,d))
    #         samples_coord_along_line[i] = dist
    #     # samples_l[:,0:2] #1D
    #
    #     samples_z = samples_l[:,2]
    #     s = np.array((samples_coord_along_line, samples_z))
    #     s = np.sort(s)
    #
    #     s = s.T
    #     print("s.shape : ",s.shape)
    #     samples_z = s[:,1]
    #     samples_coord_along_line = s[:,0]
    # ##############

    H = np.zeros([XEDGES, YEDGES])
    for i in range(XEDGES):
        for j in range(YEDGES):
            [_, _, L] = sess.run(
                [lensc_assign, amp_assign, log_likelihood],
                feed_dict={
                    lensc_p: [2 * np.double((1 + i) / XEDGES), lensc[1]],
                    amp_p: [2 * np.double((1 + j) / YEDGES), amp[1]]
                })
            H[i, j] = L[0]
    # assert (amp.shape[1] == (2))
    # assert (lensc.shape[1] == (2))
    # 5th dimension = log-likelihood at points xyzw
    emb_p = im.tf.placeholder(shape=[XEDGES, YEDGES],
                              dtype=np.float32,
                              name='emb_p')
    emb = im.tf.Variable(im.tf.zeros([XEDGES, YEDGES]),
                         name="log_probability_embedding")
    emb_as_op = emb.assign(emb_p, name='emb_as_op')

    [_] = sess.run([emb_as_op], feed_dict={emb_p: H})
    saver.save(sess, im.os.path.join(LOGDIR, LOGCHECKPT), NUM_ITERS + 1)

    # plts.plot_kernel(12,4, kernel,BEGIN, END)

    # PLOTS
    if PLOTS:
        plts.plot_sin3D_rand_points(12, 12, COORDINATE_RANGE, obs_idx_pts, obs,
                                    PLOTRASTERPOINTS)
        plts.plot_2d_observations(12, 12, pts, sel_pts, BEGIN, END)
        plts.plot_capture_line(12, 12, sel_pts, BEGIN, END)
        # plts.plot_section_observations(12, 7, ext_sel_pts, BEGIN, END)
        # GP
        plts.plot_loss_evolution(12, 4, lls)
        plts.plot_marginal_likelihood3D(XEDGES, H)
        # plts.plot_samples2D(12,5,1, pred_idx_pts, obs_idx_pts, obs, samples_, NUM_SAMPLES)

        plts.plot_samples3D(12, 12, 0, pred_idx_pts, samples_, obs_idx_pts,
                            obs, PRED_FRACTION, BEGIN, END)

        plts.plot2d_sinusoid_samples_section(
            12,
            4,
            ext_sel_pts,  # (12,4)
            line_idx,  # (200,1)
            obs_proj_idx_pts,  # (60,2)
            line_obs,  # (200,)
            samples_section_,  # (50,2,200)
            NUM_SAMPLES,  # (50)
            BEGIN,
            END)  # (3,)
        # plts.plot_gp_2D_samples(12,7, pred_sel_idx_pts, sel_obs, line_idx, line_obs, NUM_SAMPLES, samples_l[:,0:2], samples_coord_along_line, samples_z)

    PLOTS = False