def setUp(self): self.rng = np.random.RandomState(42) kernel = RBF(2) input_dim = 2 output_dim = 2 mean_function = None self.Z = self.rng.randn(5, 2) num_inducing = 5 self.layer = OutputLayer(input_dim=input_dim, output_dim=output_dim, num_inducing=num_inducing, kernel=kernel, mean_function=mean_function) self.X = self.rng.randn(10,2)
def test_optimize(self): with defer_build(): input_layer = InputLayer(input_dim=1, output_dim=1, num_inducing=self.M, kernel=RBF(1) + White(1), multitask=True) output_layer = OutputLayer(input_dim=1, output_dim=1, num_inducing=self.M, kernel=RBF(1) + White(1), multitask=True) seq = MultitaskSequential([input_layer, output_layer]) model = MultitaskDSDGP(X=self.X, Y=self.Y, Z=self.Z, layers=seq, likelihood=SwitchedLikelihood( [Gaussian(), Gaussian()]), num_latent=1) model.compile() before = model.compute_log_likelihood() opt = gpflow.train.AdamOptimizer(0.01) opt.minimize(model, maxiter=100) after = model.compute_log_likelihood() self.assertGreaterEqual(after, before)
def test_add_to_full(self): input_layer = InputLayer(2, 2, 10, RBF(2)) hidden_layer_1 = HiddenLayer(2, 2, 10, RBF(2)) hidden_layer_2 = HiddenLayer(2, 2, 10, RBF(2)) hidden_layer_3 = HiddenLayer(3, 2, 10, RBF(3)) output_layer = OutputLayer(2, 2, 10, RBF(2)) # Add hidden layer with correct dimensions with self.subTest(): layer_list = [input_layer, hidden_layer_1] seq = Sequential(layer_list) seq.add(hidden_layer_2) self.assertIs(seq.layers[-1], hidden_layer_2) # Add hidden layer with incorrect dimensions with self.subTest(): layer_list = [input_layer, hidden_layer_1] seq = Sequential(layer_list) with self.assertRaises(AssertionError): seq.add(hidden_layer_3) # Add output layer with correct dimensions with self.subTest(): layer_list = [input_layer, hidden_layer_1] seq = Sequential(layer_list) seq.add(output_layer) self.assertIs(seq.layers[-1], output_layer) # Add hidden layer after output layer with self.subTest(): layer_list = [input_layer, output_layer] seq = Sequential(layer_list) with self.assertRaises(ValueError): seq.add(hidden_layer_1)
def test_initialize_params(self): input_layer = InputLayer(2, 2, 10, RBF(2)) hidden_layer_1 = HiddenLayer(2, 2, 10, RBF(2)) output_layer = OutputLayer(2, 1, 10, RBF(2)) Z = np.ones((10, 2)) X = np.ones((100, 2)) seq = Sequential([input_layer, hidden_layer_1, output_layer]) seq.initialize_params(X, Z) self.assertTrue(np.allclose(Z, seq.layers[0].feature.Z.value))
def test_dims(self): input_layer = InputLayer(2, 3, 10, RBF(2)) hidden_layer_1 = HiddenLayer(3, 2, 10, RBF(2)) hidden_layer_2 = HiddenLayer(2, 1, 10, RBF(2)) hidden_layer_3 = HiddenLayer(1, 2, 10, RBF(3)) output_layer = OutputLayer(2, 1, 10, RBF(2)) layer_list = [ input_layer, hidden_layer_1, hidden_layer_2, hidden_layer_3, output_layer ] seq = Sequential(layer_list) dims = seq.get_dims() reference = [(2, 3), (3, 2), (2, 1), (1, 2), (2, 1)] self.assertEqual(dims, reference)
def prepare(self): N = 100 M = 10 rng = np.random.RandomState(42) X = rng.randn(N, 2) Y = rng.randn(N, 1) Z = rng.randn(M, 2) X_ind = rng.randint(0, 2, (N, 1)) Z_ind = rng.randint(0, 2, (M, 1)) X = np.hstack([X, X_ind]) Y = np.hstack([Y, X_ind]) Z = np.hstack([Z, Z_ind]) Xs = rng.randn(M, 2) Xs_ind = rng.randint(0, 2, (M, 1)) Xs = np.hstack([Xs, Xs_ind]) with defer_build(): lik = SwitchedLikelihood([Gaussian(), Gaussian()]) input_layer = InputLayer(input_dim=2, output_dim=1, num_inducing=M, kernel=RBF(2) + White(2), mean_function=Linear(A=np.ones((3, 1))), multitask=True) output_layer = OutputLayer(input_dim=1, output_dim=1, num_inducing=M, kernel=RBF(1) + White(1), multitask=True) seq = MultitaskSequential([input_layer, output_layer]) model = MultitaskDSDGP(X=X, Y=Y, Z=Z, layers=seq, likelihood=lik, num_latent=1) model.compile() return model, Xs
def test_contructor(self): input_layer = InputLayer(input_dim=1, output_dim=1, num_inducing=self.M, kernel=RBF(1) + White(1)) output_layer = OutputLayer(input_dim=1, output_dim=1, num_inducing=self.M, kernel=RBF(1) + White(1)) seq = Sequential([input_layer, output_layer]) try: model = DSDGP(X=self.X, Y=self.Y, Z=self.Z, layers=seq, likelihood=Gaussian()) except Exception as e: print(e) self.fail('DSDGP contructor fails')
def test_initialization_with_list(self): input_layer = InputLayer(2, 2, 10, RBF(2)) hidden_layer_1 = HiddenLayer(2, 2, 10, RBF(2)) hidden_layer_2 = HiddenLayer(2, 2, 10, RBF(2)) output_layer = OutputLayer(2, 1, 10, RBF(2)) with self.subTest(): layer_list = [ input_layer, hidden_layer_1, hidden_layer_2, output_layer ] try: seq = Sequential(layer_list) except Exception as e: print(e) self.fail("Initialisation with list of layers fails") # Test initilisation with incorrect layer structure with self.subTest(): layer_list = [hidden_layer_1, hidden_layer_2, output_layer] with self.assertRaises(AssertionError): seq = Sequential(layer_list)
def test_initialize_params(self): rng = np.random.RandomState(42) input_layer = InputLayer(2, 2, 10, RBF(2), multitask=True) hidden_layer_1 = HiddenLayer(2, 2, 10, RBF(2), multitask=True) output_layer = OutputLayer(2, 1, 10, RBF(2), multitask=True) Z = np.ones((10, 2)) X = np.ones((100, 2)) Z_ind = rng.randint(0, 2, (10, 1)) X_ind = rng.randint(0, 2, (100, 1)) Z = np.hstack([Z, Z_ind]) X = np.hstack([X, X_ind]) seq = MultitaskSequential([input_layer, hidden_layer_1, output_layer]) seq.initialize_params(X, Z) for l in seq.layers: self.assertTrue(np.allclose(Z_ind, l.feature.Z.value[:, -1:]))
def test_add_to_empty(self): input_layer = InputLayer(2, 2, 10, RBF(2)) hidden_layer_1 = HiddenLayer(2, 2, 10, RBF(2)) output_layer = OutputLayer(2, 1, 10, RBF(2)) # Add input layer only with self.subTest(): seq = Sequential() seq.add(input_layer) self.assertIs(seq.layers[-1], input_layer) # Add input layer and hidden layer with self.subTest(): seq = Sequential() seq.add(input_layer) seq.add(hidden_layer_1) self.assertIs(seq.layers[0], input_layer) self.assertIs(seq.layers[1], hidden_layer_1) # Add input layer, hidden layer and output layer with self.subTest(): seq = Sequential() seq.add(input_layer) seq.add(hidden_layer_1) seq.add(output_layer) self.assertIs(seq.layers[0], input_layer) self.assertIs(seq.layers[1], hidden_layer_1) self.assertIs(seq.layers[2], output_layer) # Add hidden layer as first layer with self.subTest(): seq = Sequential() with self.assertRaises(AssertionError): seq.add(hidden_layer_1) # Add output layer as first layer with self.subTest(): seq = Sequential() with self.assertRaises(AssertionError): seq.add(output_layer)
class OutputLayerTest(unittest.TestCase): @defer_build() def setUp(self): self.rng = np.random.RandomState(42) kernel = RBF(2) input_dim = 2 output_dim = 2 mean_function = None self.Z = self.rng.randn(5, 2) num_inducing = 5 self.layer = OutputLayer(input_dim=input_dim, output_dim=output_dim, num_inducing=num_inducing, kernel=kernel, mean_function=mean_function) self.X = self.rng.randn(10,2) def test_initialize_forward(self): _ = self.layer.initialize_forward(self.X, self.Z) with self.subTest(): self.assertTrue(np.allclose(self.layer.feature.Z.value, self.Z))
def prepare(self): N = 100 M = 10 rng = np.random.RandomState(42) X = rng.randn(N, 2) Y = rng.randn(N, 1) Z = rng.randn(M, 2) Xs = rng.randn(M, 2) lik = Gaussian() input_layer = InputLayer(input_dim=2, output_dim=1, num_inducing=M, kernel=RBF(2) + White(2), mean_function=Linear(A=np.ones((2, 1)))) output_layer = OutputLayer(input_dim=1, output_dim=1, num_inducing=M, kernel=RBF(1) + White(1)) seq = Sequential([input_layer, output_layer]) model = DSDGP(X=X, Y=Y, Z=Z, layers=seq, likelihood=lik) model.compile() return model, Xs