Ejemplo n.º 1
0
    def test_linear_weight_init_in_sofm(self):
        sofm = algorithms.SOFM(
            n_inputs=4,
            features_grid=(3, 3),
            weight='init_pca',
        )

        input_data = np.random.random((100, 4))
        self.assertTrue(callable(sofm.weight))

        sofm.train(input_data, epochs=0)
        self.assertFalse(callable(sofm.weight))
        self.assertEqual(sofm.weight.shape, (4, 9))

        for row in (0, 3, 6):
            left = sofm.weight[:, row]
            center = sofm.weight[:, row + 1]
            right = sofm.weight[:, row + 2]

            self.assertLess(np.linalg.norm((left - center)**2),
                            np.linalg.norm((left - right)**2))

        for i in range(3):
            top = sofm.weight[:, i]
            center = sofm.weight[:, i + 3]
            bottom = sofm.weight[:, i + 6]

            self.assertLess(np.linalg.norm((top - center)**2),
                            np.linalg.norm((top - bottom)**2))
Ejemplo n.º 2
0
    def test_sofm_hexagon_grid(self):
        data = make_circle(max_samples=100)
        sofm = algorithms.SOFM(
            n_inputs=2,
            n_outputs=9,
            learning_radius=1,
            reduce_radius_after=4,
            features_grid=(3, 3),
            verbose=False,
            grid_type='hexagon',
        )
        sofm.train(data, epochs=10)
        grid = sofm.weight.reshape((2, 3, 3))

        center = grid[:, 1, 1]
        top_left = grid[:, 0, 0]
        top_right = grid[:, 0, 2]

        distance_top_left = np.linalg.norm(center - top_left)
        distance_top_right = np.linalg.norm(center - top_right)

        self.assertLess(distance_top_right, distance_top_left)

        bottom_left = grid[:, 2, 0]
        bottom_right = grid[:, 2, 2]

        distance_bottom_left = np.linalg.norm(center - bottom_left)
        distance_bottom_right = np.linalg.norm(center - bottom_right)

        self.assertLess(distance_bottom_right, distance_bottom_left)
Ejemplo n.º 3
0
    def test_invalid_attrs(self):
        with self.assertRaisesRegexp(ValueError, "Feature grid"):
            # Invalid feature grid shape
            algorithms.SOFM(n_inputs=2, n_outputs=4, features_grid=(2, 3))

        with self.assertRaisesRegexp(ValueError, "n_outputs, features_grid"):
            algorithms.SOFM(n_inputs=2)

        with self.assertRaisesRegexp(ValueError, "more than 2 dimensions"):
            sofm = algorithms.SOFM(n_inputs=2, n_outputs=3, weight=self.weight)
            sofm.train(np.zeros((10, 2, 1)))

        with self.assertRaisesRegexp(ValueError, "more than 2 dimensions"):
            sofm = algorithms.SOFM(n_inputs=2, n_outputs=3, weight=self.weight)
            sofm.predict(np.zeros((10, 2, 1)))

        with self.assertRaisesRegexp(ValueError, "Input data expected"):
            sofm = algorithms.SOFM(n_inputs=2, n_outputs=3, weight=self.weight)
            sofm.train(np.zeros((10, 10)))

        with self.assertRaisesRegexp(ValueError, "Input data expected"):
            sofm = algorithms.SOFM(n_inputs=2, n_outputs=3, weight=self.weight)
            sofm.predict(np.zeros((10, 10)))

        with self.assertRaisesRegexp(ValueError, "one or two dimensional"):
            algorithms.SOFM(n_inputs=2,
                            features_grid=(3, 1, 1),
                            grid_type='hexagon')
Ejemplo n.º 4
0
    def test_sofm_init_during_the_training(self):
        sofm = algorithms.SOFM(
            n_inputs=4,
            n_outputs=7,
            weight='sample_from_data',
        )

        X = np.random.random((10, 4))
        self.assertTrue(callable(sofm.weight))

        sofm.train(X, epochs=1)
        self.assertFalse(callable(sofm.weight))
Ejemplo n.º 5
0
    def test_sample_data_weight_init_in_sofm(self):
        sofm = algorithms.SOFM(
            n_inputs=4,
            n_outputs=7,
            weight='sample_from_data',
        )

        input_data = np.random.random((10, 4))
        self.assertTrue(callable(sofm.weight))

        sofm.train(input_data, epochs=0)
        self.assertFalse(callable(sofm.weight))
        self.assertEqual(sofm.weight.shape, (4, 7))
Ejemplo n.º 6
0
    def test_sofm_storage(self):
        input_data = np.random.random((100, 10))
        sofm = algorithms.SOFM(n_inputs=10,
                               features_grid=(10, 10),
                               std=1,
                               step=0.5,
                               learning_radius=2,
                               weight=np.random.random((10, 100)))

        sofm.train(input_data, epochs=10)
        self.assertPickledNetwork(sofm, input_data)

        parameters = sofm.get_params()
        self.assertIn('weight', parameters)
        self.assertIsInstance(parameters['weight'], np.ndarray)
Ejemplo n.º 7
0
    def test_sofm_training_with_4d_grid(self):
        sofm = algorithms.SOFM(
            n_inputs=4,
            n_outputs=8,
            features_grid=(2, 2, 2),
            verbose=False,
        )

        data = np.concatenate([input_data, input_data], axis=1)

        sofm.train(data, epochs=1)
        error_after_first_epoch = sofm.errors.last()

        sofm.train(data, epochs=9)
        self.assertLess(sofm.errors.last(), error_after_first_epoch)
Ejemplo n.º 8
0
    def test_sofm_weight_norm_after_training_with_custom_weights(self):
        sofm = algorithms.SOFM(n_inputs=2,
                               n_outputs=3,
                               distance='cos',
                               learning_radius=1,
                               features_grid=(3, 1),
                               weight=X[(0, 2, 4), :].T,
                               verbose=False)

        actual_norms = np.linalg.norm(sofm.weight, axis=0)
        expected_norms = np.array([1, 1, 1])
        np.testing.assert_array_almost_equal(expected_norms, actual_norms)

        sofm.train(X, epochs=6)

        actual_norms = np.linalg.norm(sofm.weight, axis=0)
        expected_norms = np.array([1, 1, 1])
        np.testing.assert_array_almost_equal(expected_norms, actual_norms)