Example #1
0
def graph_GP(t_norm_,
             w_pred_linsp_Var,
             e_xyztp_s,
             amplitude_init=np.array([0.1, 0.1]),
             length_scale_init=np.array([.001, .001]),
             obs_noise_var_init=1e-3,
             LEARNING_RATE=.1,
             NUM_SAMPLES=8
             ):

    with tf.name_scope("GP"):
        # ===================================================================
        # AMP LENSC
        # ===================================================================
        with tf.name_scope("amplitude_lengthscale"):
            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, length_scale_init, obs_noise_var_init)

        # ===================================================================
        # KERNEL
        # ===================================================================
        with tf.name_scope("kernel"):
            kernel = tfkern.MaternOneHalf(amp, lensc)  # ExponentiatedQuadratic # MaternOneHalf

        # ===================================================================
        # GP_FIT
        # ===================================================================
        with tf.name_scope("GP_fit"):
            gp = tfd.GaussianProcess(
                kernel=kernel,  # ([2,],[2,])
                index_points=e_xyztp_s,
                observation_noise_variance=obs_noise_var,
                validate_args=True)

            log_likelihood = gp.log_prob(tf.transpose(tf.cast(t_norm_, dtype=tf.float64)))
            tf.summary.scalar("log_likelihood_0", log_likelihood[0])
            tf.summary.scalar("log_likelihood_1", log_likelihood[1])  # 2D GP input case
            tf.summary.scalar("length_scale", lensc[0])
            tf.summary.scalar("amplitude", amp[0])
            train_op = gpf.tf_train_gp_adam(log_likelihood, LEARNING_RATE)

        # ===================================================================
        # GP REGRESSION MODEL
        # ===================================================================
        with tf.name_scope("GP_regression_model"):
            gprm = gpf.tf_gp_regression_model(kernel,
                                              w_pred_linsp_Var,  # pred_idx_pts 1D_emb:(400,1), 2D_emb:(200,2)
                                              e_xyztp_s, # e_xyztp_s # obs_idx_pts(15,1) (15,2)
                                              t_norm_,  # obs (15,) (15,)
                                              obs_noise_var, 0.)
            samples_1d = gprm.sample(NUM_SAMPLES)

    return amp, amp_assign, amp_p, lensc, lensc_assign, lensc_p, log_likelihood, samples_1d, train_op, obs_noise_var
Example #2
0
    def graph(pred_idx_pts_, obs_idx_pts, obs, values):
        # ===================================================================
        # AMP LENSC
        # ===================================================================
        feat_dims = pred_idx_pts_.shape[2] if 2 < len(
            pred_idx_pts_.shape) else 1
        obs_dims = obs.shape[1] if 1 < len(obs.shape) else obs.shape[0]
        batch_cnt = 1

        amp, amp_assign, amp_p, \
            lensc, lensc_assign, lensc_p, \
            _, _, _, obs_noise_var \
            = gpf.tf_Placeholder_assign_test(
                        AMPLITUDE_INIT[:1],
                        LENGTHSCALE_INIT[:1],  # shape [1]
                        INIT_OBSNOISEVAR_)

        # ===================================================================
        # KERNEL
        # ===================================================================
        kernel = tfkern.ExponentiatedQuadratic(
            amp,
            lensc,
            feature_ndims=
            1,  # number of rightmost dims to include in the squared difference norm
            validate_args=True)  # MaternOneHalf

        # ===================================================================
        # GP_FIT
        # ===================================================================
        """
          index_points: `float` `Tensor` representing finite (batch of) vector(s) of
            points in the index set over which the GP is defined. Shape has the form
            `[b1, ..., bB, e, f1, ..., fF]` where `F` is the number of feature
            dimensions and must equal `kernel.feature_ndims` and `e` is the number
            (size) of index points in each batch. Ultimately this distribution
            corresponds to a `e`-dimensional multivariate normal. The batch shape
            must be broadcastable with `kernel.batch_shape` and any batch dims
            yielded by `mean_fn`.
        """
        # gp = gpf.fit_gp(kernel,
        #                 obs_idx_pts,
        #                 obs_noise_var)

        gp = tfd.GaussianProcess(
            kernel=kernel,  # ([2,],[2,])
            index_points=obs_idx_pts,
            observation_noise_variance=obs_noise_var,
            validate_args=True)

        print('feature_ndims: ', 1)
        print("kernel batch_shape:", kernel.batch_shape)
        print("kernel feature_ndims:", kernel.feature_ndims)
        print("amp shape:", amp.shape)
        print("lensc shape:", lensc.shape)

        print("obs_idx_pts shape:", obs_idx_pts.shape)
        print("values shape:", values.shape)
        print("Batch shape:", gp.batch_shape)
        print("Event shape:", gp.event_shape)

        assert kernel.batch_shape == gp.batch_shape
        assert kernel.feature_ndims == len(amp.shape) or 0 == len(amp.shape)
        assert kernel.feature_ndims == len(lensc.shape) or 0 == len(
            lensc.shape)
        """
        ---
        observed_index_points.shape:  (50, 1)
        observed_values.shape:  (50,)
        gp Batch shape: (1,)
        gp Event shape: (50,)
        kernel batch_shape: (1,)
        kernel feature_ndims: 1
        amp shape: (1,)
        lensc shape: (1,)
        neg_log_likelihood shape: (1,)
        """
        """get_marginal_distribution : 
              Compute the marginal of this GP over function values at `index_points`.        
        Args:
          index_points: `float` `Tensor` representing finite (batch of) vector(s) of
            points in the index set over which the GP is defined. Shape has the form
            `[b1, ..., bB, e, f1, ..., fF]`
             and `e` is the number (size) of index points in each batch."""

        log_likelihood = gp.log_prob(
            value=values
        )  # obs.reshape([batch_cnt, -1, obs_dims]))  # BATCH_SHAPE=(2,)
        print("neg_log_likelihood shape:", log_likelihood.shape)
        if 0 < len(log_likelihood.shape):
            assert log_likelihood.shape == [1]

        tf.summary.scalar("log_likelihood[0, 0]", log_likelihood[0])
        # tf.summary.scalar("log_likelihood[1, 0]", log_likelihood[1])
        tf.summary.scalar("length_scale", lensc[0])
        tf.summary.scalar("amplitude", amp[0])

        train_op = gpf.tf_train_gp_adam(log_likelihood, LEARNING_RATE)

        # ===================================================================
        # GP REGRESSION MODEL
        # ===================================================================
        gprm = gpf.tf_gp_regression_model(
            kernel,
            pred_idx_pts_.reshape([batch_cnt, -1,
                                   feat_dims]),  # (2500,2)  # 1D (50,)
            obs_idx_pts.reshape([batch_cnt, -1,
                                 feat_dims]),  # (90,2)  # 1D (90, 2)
            obs.reshape([batch_cnt, -1, obs_dims]),  # (90,)  # 1D  (90,)
            obs_noise_var,
            0.)

        return train_op, gprm, \
               amp, amp_assign, amp_p, \
               lensc, lensc_assign, lensc_p, \
               obs_noise_var, log_likelihood
Example #3
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
Example #4
0
def TEST_tracer_data_GP_():
    '''
    ----------------------------------------------------
    ## cvs pandas dataframe import ##
    ----------------------------------------------------
    ## 3D input GP fitting problem. ## (sinusoid is 3D)
    1. COORDINATES : project Z coord to XY plane 3D -> 2D
    2. TRACER : 1D
    3. obs : combined 1. & 2. -> 3D
    Coordinates and corresponding tracer value for 1 time step.
    4.1 scale - take average tracer over timesteps, remains 3D
    ----------------------------------------------------
    ## 6D input GP fitting problem ##
    4.2 scale - then scale GP dimensions to get
            Z coordinate,
            T time,
            P pressure ...
    ----------------------------------------------------
    ## Bijectors in tf ##
    transforming rand variables, between input and output rv.s
    unless data is generated by gaussian - assumption - can combine distributions
    forward - compute samples
    inverse - compute probabilities
    ----------------------------------------------------
    Challenges:
    - sampling with MCMC
    - switch to tf2.0
    - plot kernel and find better kernel from data
    ----------------------------------------------------
    ## VAE - variational autoencoders - parameterize probabilistic graphical model with NN ##
    take different distributions, fit it with Monte Carlo Variational Inference
    (tf automatic differentiation)
    - data is embedded into lower dimensional space
    ----------------------------------------------------
    '''
    ## Dimensions and replacements ##
    '''
    - t_idx : 1 D tracer
    - t_sel_idx : selection of close to section tracer training points
    - idx : training input
    - obs : output!
    - linsp : linspace
    - w : distance from BEGIN along section line
    - w_proj : altered xy to w when taking section cut
    - pred_idx : prediction input
    - pred_obs : prediction output
    - o : filler column full of 0s 

    > FROM DATASET : 
    obs_idx_pts (60, 2) : xy_idx
    obs (60, 1) : t_idx
    obs_rowvec  (60,) : t_idx.T
    pts (60, 3) : xyt_idx

    > CALCULATION :
    obs_proj_idx_pts (200, 1) : w_linsp
    sel_pts (7, 3) : xyt_sel_idx
    sel_obs (7,) : t_sel_idx
    ext_sel_pts (7, 4) : xytw_sel_idx
    train_idx_pts_along_section_line (7, 1) : w_sel_idx    
    line_idx (200, 1) : w_pred_idx_linsp
    line_XY (200, 3) : xyo_pred_idx_linsp
    line_obs (200,) : t_pred_obs_line_linsp
    pred_idx_pts (2500, 2) xy_pred_pts
    pred_sel_idx_pts (7, 1) t_pred_pts
    '''
    #################################################################
    PLOTS = True
    TRACER_ONLY = False
    PRESSURE_ONLY = False
    ALL = False
    LOAD_TO_CSV = False
    FILENAME = "all_data_520_720.csv"
    TIMERANGE = [220, 221]

    NUM_PTS_GEN = 1000  # 1000
    RANGE_PTS = [-2, 2]
    COORDINATE_RANGE = np.array([[-2., 2.], [-2., 2.]])  # xrange, yrange
    NUM_TRAINING_POINTS = 60

    AMPLITUDE_INIT = np.array([.1, .1])  # [0.1, 0.1]
    LENGTHSCALE_INIT = np.array([.1, .1])  # [0.1, 0.1]
    LEARNING_RATE = .1  #.01
    INIT_OBSNOISEVAR = 1e-6
    INIT_OBSNOISEVAR_ = 0.001  # 0.001
    XEDGES = 60
    YEDGES = 60  #60
    LOGDIR = "./log_dir_gp/gp_plot/"
    NUM_ITERS = 1000  # 1000
    PRED_FRACTION = 50  # 50
    LOGCHECKPT = "model.ckpt"
    NUM_SAMPLES = 20  # 50
    PLOTRASTERPOINTS = 20  # 60 this is SQUARED to to get smooth samples.
    LINSPACE_LINE = 400  # 200
    SIZEOFDATASET = 200

    # indoor = drs.load_csv('/Ubuntu_18/data_room/data_selections_csv/indoor_003.csv')
    # ds = drs.load_csv('roomselection_0001.600.csv')

    # def import_to_GP_fit(CSV):
    ds = load_csv('roomselection_1800.csv')
    # randomize index set.
    ds_idx_length = ds.shape[0]
    ds_idx_arr = np.linspace(0, ds_idx_length - 1, ds_idx_length).T
    np.random.shuffle(ds_idx_arr)
    ds_r = np.array(ds_idx_arr[:SIZEOFDATASET])

    ds = ds.iloc[ds_r, :]

    xyzt_idx = ds[['Points:0', 'Points:1', 'Points:2', 'Temperature']]
    xyz_idx = ds[['Points:0', 'Points:1', 'Points:2']]
    xy_idx = ds[['Points:0', 'Points:1']]
    xyt_idx = ds[['Points:0', 'Points:1', 'Temperature']]
    x_idx = ds[['Points:0']]
    y_idx = ds[['Points:1']]
    t_idx = ds[['Temperature']]

    ###############################
    # xyzt_idx = np.array(xyzt_idx[:1000,:])
    # xyz_idx = np.array(xyz_idx[:1000,:])
    # xy_idx = np.array(xy_idx[:1000,:])
    # xyt_idx = np.array(xyt_idx[:1000,:])
    # x_idx = np.array(x_idx[:1000,:])
    # y_idx = np.array(y_idx[:1000,:])
    # t_idx = np.array(t_idx[:1000,:])
    # ##############################
    x_min = np.min(x_idx)
    x_max = np.max(x_idx)
    y_min = np.min(y_idx)
    y_max = np.max(y_idx)

    y_max = 68.
    BEGIN = [x_min, y_min]
    END = [x_max, y_max]

    BEGIN = np.array(BEGIN).astype(dtype=np.float64).flatten()
    END = np.array(END).astype(dtype=np.float64).flatten()

    EPSILON = 0.2

    xyt_sel_idx = gpf.capture_close_3d_pts_with_2d_distance(
        xyt_idx, BEGIN, END, EPSILON)  # 2D distance, 3D pts

    if PLOTS:
        plts.plot_range_of_values_and_sel(12, 4, xyt_idx, xyt_sel_idx)
        plts.plot_2d_observations(12, 12, xyt_idx, xyt_sel_idx, BEGIN, END)
        plts.plot_capture_line(12, 12, xyt_sel_idx, BEGIN, END)
        plts.plot_val_histogram(10, 4, np.array(t_idx).T.flatten())

    ##############

    ##############
    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)

    # xy_idx, t_idx = dg.generate_noisy_2Dsin_data(NUM_TRAINING_POINTS, 0.001, COORDINATE_RANGE)
    print("--------------")
    w_linsp = np.linspace(BEGIN, END, LINSPACE_LINE)
    w_linsp = gpf.project_2d_to_line_coordinates(
        w_linsp, BEGIN, END)  #2D coord linspace between BEGIN and END
    w_linsp = gpf.create_1d_w_line_coords(w_linsp, BEGIN, END)  # 2D to 1D
    print(w_linsp.shape)  # is it really going through w?

    t_idx = np.array(t_idx).reshape(-1, 1)  # to column vector
    # xyt_idx = gpf.add_dimension(xy_idx, t_idx) # TODO use: append
    xyt_sel_idx = gpf.capture_close_3d_pts_with_2d_distance(
        xyt_idx, BEGIN, END,
        EPSILON)  # 3D # TODO use: capture_close_3d_pts_with_2d_distance

    t_sel_idx = np.zeros((0, 1))
    for i in xyt_sel_idx[:, 2]:  # 3rd D TODO replace with shape ...
        t_sel_idx = np.concatenate((t_sel_idx, i), axis=None)

    #TODO fix: d = np.dot((v - u), (x - u)) / np.linalg.norm(v - u)

    xytw_sel_idx = gpf.extend_pts_with_line_coord(xyt_sel_idx, BEGIN,
                                                  END)  # 4D

    w_sel_idx = gpf.project_2d_to_line_coordinates(xyt_sel_idx, BEGIN, END)
    w_sel_idx = gpf.create_1d_w_line_coords(w_sel_idx, BEGIN, END)  # 1D

    w_pred_idx_linsp = gpf.create_line_coord_linspace(BEGIN, END,
                                                      LINSPACE_LINE)  # 1D
    xyo_pred_idx_linsp = gpf.create_line_linspace(BEGIN, END,
                                                  LINSPACE_LINE)  # 2D
    t_pred_obs_line_linsp = gpf.create_Z_sin_along_line_linspace(
        xyo_pred_idx_linsp)  # 1D

    kernel, log_likelihood, lls, saver, amp, lensc,obs_noise_var = \
        gpf.gp_train_and_hyperparameter_optimize(
            amp, # (2,)
            amp_assign, # tensor (2,)
            amp_p, # placeholder (2,)
            lensc, lensc_assign, lensc_p, emb,
            xy_idx, # df (200,2)
            t_idx, # ndarray (200,1)
            obs_noise_var, # tensor ()
            LEARNING_RATE, NUM_ITERS, LOGDIR, LOGCHECKPT, sess)

    print("lensc", lensc)
    print("amp", amp)

    pred_x = np.linspace(-2, 2, PRED_FRACTION, dtype=np.float64)
    pred_y = np.linspace(-2, 2, PRED_FRACTION, dtype=np.float64)
    xy_pred_pts = gpf.create_meshgrid(pred_x, pred_y,
                                      PRED_FRACTION)  # posterior = predictions
    t_pred_pts = gpf.project_2d_to_line_coordinates(
        xyt_sel_idx, BEGIN, END)  # 1D observations along x.
    # print("w_pred_idx_linsp.shape : ", w_pred_idx_linsp.shape) #(200, 1)
    # print("obs_idx_pts_along_section_line.shape : ", w_sel_idx.shape) #(100, 1)
    # print("t_sel_idx.shape : ", t_sel_idx.shape) #(100, )
    # print(repr(w_sel_idx))
    # print(repr(t_sel_idx))

    # Gaussian process regression model
    # gprm = gpf.tf_gp_regression_model(kernel, xy_pred_pts, xy_idx, t_idx.T, obs_noise_var, 0.)
    gprm_section = gpf.tf_gp_regression_model(kernel, w_pred_idx_linsp,
                                              w_sel_idx, t_sel_idx,
                                              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)

    H = gpf.calc_H(XEDGES, YEDGES, lensc, lensc_assign, lensc_p, amp,
                   amp_assign, amp_p, log_likelihood, sess)
    # 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)

    print("obs_idx_pts = xy_idx", xy_idx.shape)
    print("obs = t_idx", t_idx.shape)
    print("obs_rowvec ", t_idx.T.shape)
    print("obs_proj_idx_pts = w_linsp", w_linsp.shape)
    print("pts = xyt_idx", xyt_idx.shape)
    print("sel_pts = xyt_sel_idx", xyt_sel_idx.shape)
    print("sel_obs = t_sel_idx", t_sel_idx.shape)
    # print("ext_sel_pts",ext_sel_pts.shape)
    print("train_idx_pts_along_section_line = w_sel_idx", w_sel_idx.shape)
    print("line_idx = w_pred_idx_linsp", w_pred_idx_linsp.shape)
    print("line_XY = xyo_pred_idx_linsp", xyo_pred_idx_linsp.shape)
    print("line_obs = t_pred_obs_line_linsp", t_pred_obs_line_linsp.shape)
    print("pred_idx_pts = xy_pred_pts", xy_pred_pts.shape)
    print("pred_sel_idx_pts = t_pred_pts", t_pred_pts.shape)
    print("----------------------")

    # PLOTS
    if PLOTS:
        pass

        # plts.plot_sin3D_rand_points(12, 12, COORDINATE_RANGE, obs_idx_pts, obs, PLOTRASTERPOINTS)
        #
        # # 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.plot2d_samples_section(12, 4, xytw_sel_idx, w_pred_idx_linsp,
                                    w_linsp, t_pred_obs_line_linsp,
                                    samples_section_, NUM_SAMPLES, BEGIN, END)

        # plts.plot_samples3D(12, 12, 0, xy_pred_pts, samples_section_, xy_idx, t_idx, PRED_FRACTION, BEGIN, END)

        # # 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
Example #5
0
def graph_GP(et_Var,
             t_norm_,
             w_pred_linsp_Var,
             e_xyztp_s,
             amplitude_init=np.array([0.1, 0.1]),
             length_scale_init=np.array([.001, .001]),
             obs_noise_var_init=1e-3,
             LEARNING_RATE=.1,
             NUM_SAMPLES=8):

    with tf.name_scope("GP"):
        # ===================================================================
        # AMP LENSC
        # ===================================================================
        with tf.name_scope("amplitude_lengthscale"):
            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, length_scale_init, obs_noise_var_init)

        # ===================================================================
        # KERNEL
        # ===================================================================
        with tf.name_scope("kernel"):
            kernel = tfkern.MaternOneHalf(
                amp, lensc)  # ExponentiatedQuadratic # MaternOneHalf

        # ===================================================================
        # GP_FIT
        # ===================================================================
        with tf.name_scope("GP_fit"):
            # gp = gpf.fit_gp(kernel, np.array(enc_df).reshape(-1, enc_df.shape[1]).astype(np.float64), obs_noise_var)  # GP fit to 3D XYZ, where Z is sinusoid of XY
            gp = gpf.fit_gp(
                kernel,
                # np.array(enc_df).reshape(-1, enc_df.shape[1]).astype(np.float64),
                e_xyztp_s,  # et_Var
                obs_noise_var)  # GP fit to 3D XYZ, where Z is sinusoid of XY
            log_likelihood = gp.log_prob(
                tf.transpose(tf.cast(t_norm_, dtype=tf.float64)))
            tf.summary.scalar("log_likelihood_0", log_likelihood[0])
            tf.summary.scalar("log_likelihood_1",
                              log_likelihood[1])  # 2D GP input case
            tf.summary.scalar("length_scale", lensc[0])
            tf.summary.scalar("amplitude", amp[0])
            train_op = gpf.tf_train_gp_adam(log_likelihood, LEARNING_RATE)

        # ===================================================================
        # GP REGRESSION MODEL
        # ===================================================================
        with tf.name_scope("GP_regression_model"):
            gprm = gpf.tf_gp_regression_model(
                kernel,
                w_pred_linsp_Var,  # pred_idx_pts 1D_emb:(400,1), 2D_emb:(200,2)
                e_xyztp_s,  # e_xyztp_s # obs_idx_pts(15,1) (15,2)
                t_norm_,  # obs (15,) (15,)
                obs_noise_var,
                0.)
            samples_1d = gprm.sample(NUM_SAMPLES)

    return amp, amp_assign, amp_p, lensc, lensc_assign, lensc_p, log_likelihood, samples_1d, train_op, obs_noise_var