def run(): gpf.config.set_default_float(getattr(np, FLAGS.dtype)) tf.random.set_seed(FLAGS.tf_seed) f_times = os.path.join("results", f"mcmc-times-{FLAGS.model}-{FLAGS.mcmc}") # TODO: we need a flag for this directory really. f_posterior = os.path.join("results", f"mcmc-posterior-{FLAGS.model}-{FLAGS.mcmc}") n_training_logspace = [3000, 300, 50] cov_fun = get_covariance_function() if FLAGS.run: times = np.empty(len(n_training_logspace), dtype=float) for i, n_training in tqdm.tqdm(enumerate(n_training_logspace), total=FLAGS.n_runs): t, y = get_data(n_training) gp_model = get_model(ModelEnum(FLAGS.model), (t, y), FLAGS.noise_variance, cov_fun, t.shape[0]) set_gp_priors(gp_model) run_time, params_res = run_one_mcmc(n_training, gp_model) times[i] = run_time np.savez(f_posterior + f"-{n_training}", **params_res) np.save(f_times, np.stack([n_training_logspace, times], axis=1)) if FLAGS.plot: T = 2000 for n_training in n_training_logspace: result = np.load(f_posterior + f"-{n_training}.npz") t, y = get_data(n_training) rng = np.random.RandomState(FLAGS.np_seed) interpolation_times = np.sort(rng.uniform(t[0], t[-1], 2000))[:, None] gp_model = get_model(ModelEnum(FLAGS.model), (t, y), FLAGS.noise_variance, cov_fun, T + n_training) ax = plt.subplot() ax.scatter(t[:, ], y[:, 0], s=1, marker="x", color="k") for param_name, sample in result.items(): param = eval(f"gp_model{param_name}") print(param, np.mean(sample)) for i in rng.choice(FLAGS.n_samples, 10, replace=True): for param, sample in zip(gp_model.trainable_variables, result.values()): # param = eval(f"gp_model{param_name}") param.assign(sample[i]) interpolated_points, interpolation_cov = gp_model.predict_f( interpolation_times) ax.plot(interpolation_times[:, 0], interpolated_points[:, 0], alpha=0.25, color="blue") plt.show()
def run(): gpf.config.set_default_float(getattr(np, FLAGS.dtype)) tf.random.set_seed(FLAGS.tf_seed) f_times = os.path.join("results", f"mcmc-times-{FLAGS.model}-{FLAGS.mcmc}") # TODO: we need a flag for this directory really. f_posterior = os.path.join("results", f"mcmc-posterior-{FLAGS.model}-{FLAGS.mcmc}") n_training_logspace = [3192] if FLAGS.run: cov_fun = get_covariance_function() times = np.empty(len(n_training_logspace), dtype=float) for i, n_training in tqdm.tqdm(enumerate(n_training_logspace), total=len(n_training_logspace)): t, y = get_data(n_training) gp_model = get_model(ModelEnum(FLAGS.model), (t, y), FLAGS.noise_variance, cov_fun, t.shape[0]) set_gp_priors(gp_model) run_time, params_res = run_one_mcmc(n_training, gp_model) times[i] = run_time np.savez(f_posterior + f"-{n_training}", **params_res) np.save(f_times, np.stack([n_training_logspace, times], axis=1))
def run_one(seed, covariance_function, gp_model, n_training, n_pred): t, ft, t_pred, ft_pred, y = get_data(seed, n_training, n_pred) gp_dtype = gpf.config.default_float() if gp_model is None: model_name = ModelEnum(FLAGS.model) gp_model = get_model(model_name, (t, y), FLAGS.noise_variance, covariance_function, t.shape[0] + t_pred.shape[0]) else: gp_model.data = data_input_to_tensor((t, y)) tensor_t_pred = tf.convert_to_tensor(t_pred, dtype=gp_dtype) y_pred, _ = gp_model.predict_f(tensor_t_pred) error = rmse(y_pred, ft_pred) return error, gp_model
def run(): gpf.config.set_default_float(getattr(np, FLAGS.dtype)) tf.random.set_seed(FLAGS.tf_seed) f_times = os.path.join("results", f"mcmc-times-{FLAGS.cov}-{FLAGS.model}-{FLAGS.mcmc}") f_posterior = os.path.join("results", f"mcmc-posterior-{FLAGS.cov}-{FLAGS.model}-{FLAGS.mcmc}") if FLAGS.cov == CovarianceEnum.QP.value: raise NotImplementedError("Quasiperiodic is not supported for this experiment") n_training_logspace = np.logspace(7, 14, FLAGS.n_runs, base=2, dtype=int) if FLAGS.run: times = np.empty(FLAGS.n_runs, dtype=float) cov_fun = get_simple_covariance_function(FLAGS.cov) for i, n_training in tqdm.tqdm(enumerate(n_training_logspace), total=FLAGS.n_runs): t, *_, y = get_data(FLAGS.np_seed, n_training, 1) gp_model = get_model(ModelEnum(FLAGS.model), (t, y), FLAGS.noise_variance, cov_fun, t.shape[0]) gp_model = set_priors(gp_model) run_time, params_res = run_one_mcmc(n_training, gp_model) times[i] = run_time np.savez(f_posterior + f"-{n_training}", **params_res) np.save(f_times, np.stack([n_training_logspace, times], axis=1))
def run(): gpf.config.set_default_float(getattr(np, FLAGS.dtype)) dtype = gpf.config.default_float() tf.random.set_seed(FLAGS.tf_seed) f_times = os.path.join("results", f"map-times-{FLAGS.model}") # TODO: we need a flag for this directory really. f_posterior = os.path.join("results", f"map-posterior-{FLAGS.model}") n_training_logspace = [ 1200, 2200, 3200 ] # np.logspace(2, np.log10(3200), num=4, dtype=int) cov_fun = get_covariance_function() if FLAGS.run: times = np.empty(len(n_training_logspace), dtype=dtype) for i, n_training in tqdm.tqdm(enumerate(n_training_logspace), total=len(n_training_logspace)): t, y = get_data(n_training) gp_model = get_model(ModelEnum(FLAGS.model), (t.astype(dtype), y.astype(dtype)), FLAGS.noise_variance, cov_fun, t.shape[0]) set_gp_priors(gp_model) opt = gpf.optimizers.Scipy() eval_func = opt.eval_func(gp_model.training_loss, gp_model.trainable_variables, compile=True) x0 = opt.initial_parameters(gp_model.trainable_variables).numpy() _ = eval_func(x0) tic = time.time() opt_log = minimize(eval_func, x0, jac=True, options=dict(maxiter=100, disp=1)) times[i] = time.time() - tic print(opt_log) params_res = gpf.utilities.parameter_dict(gp_model) for param in gp_model.trainable_parameters: print(param.name) np.savez(f_posterior + f"-{n_training}", **params_res) np.save(f_times, np.stack([n_training_logspace, times], axis=1)) if FLAGS.plot: T = 3200 * 30 for n_training in n_training_logspace[-1:]: result = np.load(f_posterior + f"-{n_training}.npz") t, y = get_data(n_training) # rng = np.random.RandomState(FLAGS.np_seed) interpolation_times = np.linspace(t[0, 0], t[-1, 0], T).astype( gpf.config.default_float())[:, None] print(interpolation_times.shape) gp_model = get_model(ModelEnum(FLAGS.model), (t, y), FLAGS.noise_variance, cov_fun, T + n_training) ax = plt.subplot() ax.scatter(t[:, 0], y[:, 0], s=1, marker="x", color="k") for param_name, param_val in result.items(): print(param_name, param_val) param = eval(f"gp_model{param_name}") param.assign(param_val) interpolated_points, interpolation_cov = gp_model.predict_f( interpolation_times) tic = time.time() interpolated_points, interpolation_cov = gp_model.predict_f( interpolation_times) print(time.time() - tic) print(interpolated_points.shape) ax.plot(interpolation_times[:, 0], interpolated_points[:, 0], alpha=0.25, color="blue") ax.fill_between(interpolation_times[:, 0], interpolated_points[:, 0] - 1.96 * np.sqrt(interpolation_cov[:, 0]), interpolated_points[:, 0] + 1.96 * np.sqrt(interpolation_cov[:, 0]), alpha=0.25, color="blue") plt.show()