Beispiel #1
0
    def test_reproducibility(self):
        ortho = init.Orthogonal(seed=0)

        weight1 = ortho.sample((10, 20), return_array=True)
        weight2 = ortho.sample((10, 20), return_array=True)

        np.testing.assert_array_almost_equal(weight1, weight2)
Beispiel #2
0
    def test_orthogonal_matrix_initializer(self):
        # Note: Matrix can't be orthogonal for row and column space
        # at the same time in case if matrix rectangular

        ortho = init.Orthogonal(scale=1)
        # Matrix that have more rows than columns
        weight = self.eval(ortho.sample((30, 10)))
        np.testing.assert_array_almost_equal(np.eye(10),
                                             weight.T.dot(weight),
                                             decimal=5)

        ortho = init.Orthogonal(scale=1)
        # Matrix that have more columns than rows
        weight = self.eval(ortho.sample((10, 30)))
        np.testing.assert_array_almost_equal(np.eye(10),
                                             weight.dot(weight.T),
                                             decimal=5)
    def test_bfgs(self):
        x_train, x_test, y_train, y_test = simple_classification()

        qnnet = algorithms.QuasiNewton(
            connection=[
                layers.Input(10),
                layers.Dropout(0.1),
                layers.Sigmoid(30, weight=init.Orthogonal()),
                layers.Sigmoid(1, weight=init.Orthogonal()),
            ],
            shuffle_data=True,
            show_epoch='20 times',
            verbose=False,
        )
        qnnet.train(x_train, y_train, x_test, y_test, epochs=20)
        result = qnnet.predict(x_test).round().astype(int)

        roc_curve_score = metrics.roc_auc_score(result, y_test)
        self.assertAlmostEqual(0.92, roc_curve_score, places=2)
    def test_quasi_newton_psb(self):
        x_train, x_test, y_train, y_test = simple_classification()

        qnnet = algorithms.QuasiNewton(
            connection=[
                layers.Input(10),
                layers.Sigmoid(30, weight=init.Orthogonal()),
                layers.Sigmoid(1, weight=init.Orthogonal()),
            ],
            shuffle_data=True,
            verbose=False,
            update_function='psb',
            h0_scale=2,
        )
        qnnet.train(x_train, y_train, x_test, y_test, epochs=3)
        result = qnnet.predict(x_test).round()

        roc_curve_score = metrics.roc_auc_score(result, y_test)
        self.assertAlmostEqual(0.92, roc_curve_score, places=2)
Beispiel #5
0
    def test_quasi_newton_bfgs(self):
        x_train, x_test, y_train, y_test = simple_classification()

        qnnet = algorithms.QuasiNewton(
            network=[
                layers.Input(10),
                layers.Sigmoid(30, weight=init.Orthogonal()),
                layers.Sigmoid(1, weight=init.Orthogonal()),
            ],
            shuffle_data=True,
            show_epoch=10,
            update_function='bfgs',
        )

        qnnet.train(x_train, y_train, x_test, y_test, epochs=50)
        result = qnnet.predict(x_test).round().astype(int)

        roc_curve_score = metrics.roc_auc_score(result, y_test)
        self.assertAlmostEqual(0.92, roc_curve_score, places=2)
Beispiel #6
0
 def test_orthogonal_1d_shape(self):
     ortho = init.Orthogonal(scale=1)
     sampled_data = self.eval(ortho.sample(shape=(1, )))
     self.assertEqual((1, ), sampled_data.shape)
Beispiel #7
0
 def test_orthogonal_init_repr(self):
     ortho_initializer = init.Orthogonal(scale=1)
     self.assertEqual("Orthogonal(scale=1)", str(ortho_initializer))
Beispiel #8
0
 def test_orthogonal_matrix_initializer_errors(self):
     with self.assertRaises(ValueError):
         ortho = init.Orthogonal()
         # More than 2 dimensions
         ortho.sample((5, 5, 5))