def test_gaussian_process_deep_copyable( gpr_interface_factory: ModelFactoryType) -> None: x = tf.constant(np.arange(5).reshape(-1, 1), dtype=gpflow.default_float()) model, _ = gpr_interface_factory(x, fnc_2sin_x_over_3(x)) model_copy = copy.deepcopy(model) x_predict = tf.constant([[50.5]], gpflow.default_float()) # check deepcopy predicts same values as original mean_f, variance_f = model.predict(x_predict) mean_f_copy, variance_f_copy = model_copy.predict(x_predict) npt.assert_equal(mean_f, mean_f_copy) npt.assert_equal(variance_f, variance_f_copy) # check that updating the original doesn't break or change the deepcopy x_new = tf.concat( [x, tf.constant([[10.0], [11.0]], dtype=gpflow.default_float())], 0) new_data = Dataset(x_new, fnc_2sin_x_over_3(x_new)) model.update(new_data) model.optimize(new_data) mean_f_updated, variance_f_updated = model.predict(x_predict) mean_f_copy_updated, variance_f_copy_updated = model_copy.predict( x_predict) npt.assert_equal(mean_f_copy_updated, mean_f_copy) npt.assert_equal(variance_f_copy_updated, variance_f_copy) npt.assert_array_compare(operator.__ne__, mean_f_updated, mean_f) npt.assert_array_compare(operator.__ne__, variance_f_updated, variance_f)
def test_model_stack_predict_y() -> None: x = tf.constant(np.arange(5).reshape(-1, 1), dtype=gpflow.default_float()) model1 = gpr_model(x, fnc_3x_plus_10(x)) model2 = sgpr_model(x, fnc_2sin_x_over_3(x)) stack = ModelStack((model1, 1), (model2, 1)) mean, variance = stack.predict_y(x) npt.assert_allclose(mean[:, 0:1], model1.predict_y(x)[0]) npt.assert_allclose(mean[:, 1:2], model2.predict_y(x)[0]) npt.assert_allclose(variance[:, 0:1], model1.predict_y(x)[1]) npt.assert_allclose(variance[:, 1:2], model2.predict_y(x)[1])
def test_dgp_optimize_with_defaults(two_layer_model: Callable[[TensorType], DeepGP], keras_float: None) -> None: x_observed = np.linspace(0, 100, 100).reshape((-1, 1)) y_observed = fnc_2sin_x_over_3(x_observed) data = x_observed, y_observed dataset = Dataset(*data) optimizer = tf.optimizers.Adam() model = DeepGaussianProcess(two_layer_model(x_observed), optimizer=optimizer) elbo = model.model_gpflux.elbo(data) model.optimize(dataset) assert model.model_gpflux.elbo(data) > elbo
def test_vgp_update_q_mu_sqrt_unchanged() -> None: x_observed = tf.constant(np.arange(10).reshape((-1, 1)), dtype=gpflow.default_float()) y_observed = fnc_2sin_x_over_3(x_observed) model = VariationalGaussianProcess(vgp_matern_model(x_observed, y_observed)) old_q_mu = model.model.q_mu.numpy() old_q_sqrt = model.model.q_sqrt.numpy() data = Dataset(x_observed, y_observed) model.update(data) new_q_mu = model.model.q_mu.numpy() new_q_sqrt = model.model.q_sqrt.numpy() npt.assert_allclose(old_q_mu, new_q_mu, atol=1e-5) npt.assert_allclose(old_q_sqrt, new_q_sqrt, atol=1e-5)
def test_dgp_optimize(two_layer_model: Callable[[TensorType], DeepGP], batch_size: int, keras_float: None) -> None: x_observed = np.linspace(0, 100, 100).reshape((-1, 1)) y_observed = fnc_2sin_x_over_3(x_observed) data = x_observed, y_observed dataset = Dataset(*data) optimizer = tf.optimizers.Adam() fit_args = {"batch_size": batch_size, "epochs": 10, "verbose": 0} model = DeepGaussianProcess(two_layer_model(x_observed), optimizer, fit_args) elbo = model.model_gpflux.elbo(data) model.optimize(dataset) assert model.model_gpflux.elbo(data) > elbo
def test_gaussian_process_regression_ref_optimize( gpflow_interface_factory: ModelFactoryType, ) -> None: x = tf.constant(np.arange(5).reshape(-1, 1), dtype=gpflow.default_float()) y = fnc_2sin_x_over_3(x) model, _reference_model = gpflow_interface_factory( x, y, optimizer=create_optimizer(gpflow.optimizers.Scipy(), {}) ) reference_model = _reference_model(x, y) model.optimize(Dataset(x, y)) internal_model = model.model if isinstance(internal_model, SVGP): args = {"data": (x, y)} else: args = {} reference_model.data = ( tf.Variable( reference_model.data[0], trainable=False, shape=[None, *reference_model.data[0].shape[1:]], ), tf.Variable( reference_model.data[1], trainable=False, shape=[None, *reference_model.data[1].shape[1:]], ), ) gpflow.optimizers.Scipy().minimize( reference_model.training_loss_closure(**args, compile=False), reference_model.trainable_variables, ) npt.assert_allclose( internal_model.training_loss(**args), reference_model.training_loss(**args), rtol=1e-6 )