def onepred(i): f = compute_f(alpha[i], rho[i], beta[i], eta[i]) gprm = tfd.GaussianProcessRegressionModel( kernel=SqExpKernel(alpha[i], rho[i]), index_points=index_points.astype(dtype), observation_index_points=X, jitter=1e-6, observations=f) return gprm.sample().numpy()
def get_model_infer(self, X_test): return tfd.GaussianProcessRegressionModel( kernel=self.model_train. kernel, # Reuse the same kernel instance, with the same params index_points=X_test, observation_index_points=self.model_train.index_points, observations=self.y_train, observation_noise_variance=self.model_train. observation_noise_variance, predictive_noise_variance=tf.constant(0., dtype=np.float64))
def body(i, ys): y = tf.gather(observed_dz_dxt, i, axis=1) posterior2 = tfd.GaussianProcessRegressionModel( kernel=kernel2, mean_fn=lambda x: tf.reduce_mean(y, keep_dims=True), index_points=new_Xt, observation_index_points=observed_Xt, observations=y, observation_noise_variance=0., predictive_noise_variance=0.) ys = tf.concat([ys, posterior2.mean()], axis=0) return i + 1, ys
def tf_gp_regression_model(kernel, pred_idx_pts, obs_idx_pts, obs, obs_noise_var, pred_noise_var): pred_idx_pts_Var = tf.cast(pred_idx_pts, dtype=tf.float64, name="pred_idx_pts_Var") obs_idx_pts_Var = tf.cast(obs_idx_pts, dtype=tf.float64, name="obs_idx_pts_Var") obs_Var = tf.cast(obs, dtype=tf.float64, name="obs_Var") obs_Var = tf.reshape(obs_Var, shape=[-1], name="obs_Var") obs_noise_Var = tf.Variable(obs_noise_var, dtype=tf.float64, name="obs_noise_var") gprm = tfd.GaussianProcessRegressionModel( kernel=kernel, index_points=pred_idx_pts_Var, observation_index_points=obs_idx_pts_Var, observations=obs_Var, observation_noise_variance=obs_noise_Var, predictive_noise_variance=pred_noise_var) return gprm
# Having trained the model, we'd like to sample from the posterior conditioned # on observations. We'd like the samples to be at points other than the training inputs. predictive_index_points_ = np.linspace(-1.2, 1.2, 200, dtype=np.float64) # Reshape to [200, 1] -- 1 is the dimensionality of the feature space. predictive_index_points_ = predictive_index_points_[..., np.newaxis] print("predictive_index_points_.shape", predictive_index_points_.shape) print("observation_index_points_.shape", observation_index_points_.shape) print("observations_.shape", observations_.shape) gprm = tfd.GaussianProcessRegressionModel( kernel=kernel, # Reuse the same kernel instance, with the same params index_points=predictive_index_points_, observation_index_points=observation_index_points_, observations=observations_, observation_noise_variance=observation_noise_variance, predictive_noise_variance=0.) ##################################### # Create op to draw 50 independent samples, each of which is a *joint* draw # from the posterior at the predictive_index_points_. Since we have 200 input # locations as defined above, this posterior distribution over corresponding # function values is a 200-dimensional multivariate Gaussian distribution! num_samples = 50 samples = gprm.sample(num_samples) # Draw samples and visualize. samples_ = sess.run(samples)
FIRSTELEM_=False else: observations = np.vstack((observations, z)) observations = np.reshape(observations, [-1]) observation_noise_variance=0.01 # print(kernel) print(pred_pts) # print(xy_pts) # print(observations) gprm = tfd.GaussianProcessRegressionModel( kernel=kernel, # Reuse the same kernel instance, with the same params index_points=pred_pts, # 1D_emb:(400,1), 2D_emb:(200,2) observation_index_points=xy_pts, # (15,1) (15,2) observations=observations, # (15,) observation_noise_variance=observation_noise_variance, validate_args=True) samples = gprm.sample(NUM_GP_SAMPLES) gpsamples = sess.run(samples) #TODO # fix gprm index_..... # condition somehow def plot2D_samples(X, Y): # Plot the true function, observations, and posterior samples. plt.figure(figsize=(X, Y)) # plt.plot(pred_pts, sinusoid(predictive_index_points_),
with tf.name_scope('PCA'): pca = PCA(config.n_components) observed_Xt = pca.fit_transform(observed_X) observed_dz_dxt = pca.transform_gradient(observed_dz_dx) new_Xt = pca.transform(new_X) with tf.name_scope('prediction'): with tf.name_scope('function_process'): with tf.name_scope('kernel'): kernel1 = tfk.ExponentiatedQuadratic(amplitude, length_scale) with tf.name_scope('posterior'): posterior1 = tfd.GaussianProcessRegressionModel( kernel=kernel1, mean_fn=lambda x: tf.reduce_mean(observed_y, keep_dims=True), index_points=new_Xt, observation_index_points=observed_Xt, observations=observed_y, observation_noise_variance=0., predictive_noise_variance=0.) pred_y = posterior1.mean() y_variance = posterior1.variance() with tf.name_scope('derivative_process'): _init_index = tf.constant(0) _pred_dzt = tf.constant([], dtype=tf.float64) with tf.name_scope('kernel'): kernel2 = ApproximateExponentialQuadraticHessian( amplitude, length_scale) with tf.name_scope('posterior'): def cond(i, ys):