def __call__(self, X): # try tf.squeeze instead of tf.shape. just use tf.shape and take the first number(s) from brackets tile_shape = tf.concat( [tf.shape(X)[:-1], [1]], axis=0, ) reshape_shape = tf.concat( [tf.ones(shape=1, dtype=default_int()), [-1]], axis=0, ) return tf.tile(tf.reshape(self.c, reshape_shape), (902, 1))
def test_switched_mean_function(N, D): """ Test for the SwitchedMeanFunction. """ X = np.hstack([rng.randn(N, D), 1.0 * rng.randint(0, 2, N).reshape(-1, 1)]) zeros, ones = Constant(np.zeros(1)), Constant(np.ones(1)) switched_mean = SwitchedMeanFunction([zeros, ones]) np_list = np.array([0., 1.]) result_ref = (np_list[X[:, D].astype(default_int())]).reshape(-1, 1) result = switched_mean(X) assert_allclose(result, result_ref)
def test_softmax_y_shape_assert(num, dimF, dimY): """ SoftMax assumes the class is given as a label (not, e.g., one-hot encoded), and hence just uses the first column of Y. To prevent silent errors, there is a tf assertion that ensures Y only has one dimension. This test checks that this assert works as intended. """ F = tf.random.normal((num, dimF)) dY = np.vstack((np.random.randn(num - 3, dimY), np.ones((3, dimY)))) > 0 Y = tf.convert_to_tensor(dY, dtype=default_int()) likelihood = Softmax(dimF) try: likelihood.log_prob(F, Y) except tf.errors.InvalidArgumentError as e: assert "Condition x == y did not hold." in e.message
def test_robust_max_multiclass_predict_density(num_classes, num_points, mock_prob, expected_prediction, tol, epsilon): class MockRobustMax(gpflow.likelihoods.RobustMax): def prob_is_largest(self, Y, Fmu, Fvar, gh_x, gh_w): return tf.ones((num_points, 1), dtype=default_float()) * mock_prob likelihood = MultiClass(num_classes, invlink=MockRobustMax(num_classes, epsilon)) F = tf.ones((num_points, num_classes)) rng = np.random.RandomState(1) Y = tf.cast(rng.randint(num_classes, size=(num_points, 1)), dtype=default_int()) prediction = likelihood.predict_density(F, F, Y) assert_allclose(prediction, expected_prediction, tol, tol)
def test_softmax_bernoulli_equivalence(num, dimF, dimY): dF = np.vstack( (np.random.randn(num - 3, dimF), np.array([[-3., 0.], [3, 0.], [0., 0.]]))) dY = np.vstack((np.random.randn(num - 3, dimY), np.ones((3, dimY)))) > 0 F = tf.cast(dF, default_float()) Fvar = tf.exp( tf.stack([F[:, 1], -10.0 + tf.zeros(F.shape[0], dtype=F.dtype)], axis=1)) F = tf.stack([F[:, 0], tf.zeros(F.shape[0], dtype=F.dtype)], axis=1) Y = tf.cast(dY, default_int()) Ylabel = 1 - Y softmax_likelihood = Softmax(dimF) bernoulli_likelihood = Bernoulli(invlink=tf.sigmoid) softmax_likelihood.num_monte_carlo_points = int( 0.3e7) # Minimum number of points to pass the test on CircleCI bernoulli_likelihood.num_gauss_hermite_points = 40 assert_allclose( softmax_likelihood.conditional_mean(F)[:, :1], bernoulli_likelihood.conditional_mean(F[:, :1])) assert_allclose( softmax_likelihood.conditional_variance(F)[:, :1], bernoulli_likelihood.conditional_variance(F[:, :1])) assert_allclose(softmax_likelihood.log_prob(F, Ylabel), bernoulli_likelihood.log_prob(F[:, :1], Y.numpy())) mean1, var1 = softmax_likelihood.predict_mean_and_var(F, Fvar) mean2, var2 = bernoulli_likelihood.predict_mean_and_var( F[:, :1], Fvar[:, :1]) assert_allclose(mean1[:, 0, None], mean2, rtol=2e-3) assert_allclose(var1[:, 0, None], var2, rtol=2e-3) ls_ve = softmax_likelihood.variational_expectations(F, Fvar, Ylabel) lb_ve = bernoulli_likelihood.variational_expectations( F[:, :1], Fvar[:, :1], Y.numpy()) assert_allclose(ls_ve[:, 0, None], lb_ve, rtol=5e-3)
import tensorflow as tf import gym from gpflow import config from gpflow import set_trainable from pilco.models import PILCO from pilco.controllers import RbfController, LinearController from pilco.rewards import ExponentialReward, LinearReward from safe_pilco_extension.rewards_safe import RiskOfCollision, ObjectiveFunction from safe_pilco_extension.safe_pilco import SafePILCO from safe_pilco_experiments.utils import rollout, policy from safe_pilco_experiments.utils import Normalised_Env from linear_cars_env import LinearCars int_type = config.default_int() float_type = config.default_float() def safe_cars(name='', seed=0, logging=False): T = 25 th = 0.05 np.random.seed(seed) name = name J = 5 N = 8 eval_runs = 5 env = LinearCars() # Initial random rollouts to generate a dataset X1, Y1, _, _ = rollout(env, pilco=None, timesteps=T, verbose=True, random=True, render=False) for i in range(1,5):
def __repr__(self): name = self.likelihood.__class__.__name__ return f"{name}-rtol={self.rtol}-atol={self.atol}" likelihood_setups = [ LikelihoodSetup(Gaussian()), LikelihoodSetup(StudentT()), LikelihoodSetup(Beta(), Y=tf.random.uniform(Datum.Yshape, dtype=default_float())), LikelihoodSetup(Ordinal(np.array([-1, 1])), Y=tf.random.uniform(Datum.Yshape, 0, 3, dtype=default_int())), LikelihoodSetup(Poisson(invlink=tf.square), Y=tf.random.poisson(Datum.Yshape, 1.0, dtype=default_float())), LikelihoodSetup(Exponential(invlink=tf.square), Y=tf.random.uniform(Datum.Yshape, dtype=default_float())), LikelihoodSetup(Gamma(invlink=tf.square), Y=tf.random.uniform(Datum.Yshape, dtype=default_float())), LikelihoodSetup(Bernoulli(invlink=tf.sigmoid), Y=tf.random.uniform(Datum.Yshape, dtype=default_float())), pytest.param(LikelihoodSetup(MultiClass(2), Y=tf.argmax(Datum.Y, 1).numpy().reshape(-1, 1), rtol=1e-3, atol=1e-3),
self.Y = Y self.rtol = rtol self.atol = atol def __repr__(self): name = self.likelihood.__class__.__name__ return f"{name}-rtol={self.rtol}-atol={self.atol}" scalar_likelihood_setups = [ LikelihoodSetup(Gaussian()), LikelihoodSetup(StudentT()), LikelihoodSetup(Beta(), Y=tf.random.uniform(Datum.Yshape, dtype=default_float())), LikelihoodSetup( Ordinal(np.array([-1, 1])), Y=tf.random.uniform(Datum.Yshape, 0, 3, dtype=default_int()), ), LikelihoodSetup( Poisson(invlink=tf.square), Y=tf.random.poisson(Datum.Yshape, 1.0, dtype=default_float()), ), LikelihoodSetup( Exponential(invlink=tf.square), Y=tf.random.uniform(Datum.Yshape, dtype=default_float()), ), LikelihoodSetup( Gamma(invlink=tf.square), Y=tf.random.uniform(Datum.Yshape, dtype=default_float()), ), LikelihoodSetup( Bernoulli(invlink=tf.sigmoid),