Exemple #1
0
    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))
Exemple #2
0
    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)
Exemple #3
0
    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)
Exemple #4
0
    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')
Exemple #5
0
    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
Exemple #6
0
    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))
            output_layer = OutputLayer(input_dim=1,
                                       output_dim=1,
                                       num_inducing=self.M,
                                       kernel=RBF(1) + White(1))

            seq = Sequential([input_layer, output_layer])

            model = DSDGP(X=self.X,
                          Y=self.Y,
                          Z=self.Z,
                          layers=seq,
                          likelihood=Gaussian())
        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)
Exemple #7
0
    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)
Exemple #8
0
    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)
Exemple #9
0
 def test_initialization_with_empty(self):
     seq = Sequential()
     self.assertIsInstance(seq.layers, ParamList)