Example #1
0
    def test_sofm_euclide_norm_distance(self):
        weight = np.array([
            [1.41700099, 0.52680476],
            [-0.60938464, 1.56545643],
            [-0.30243644, 0.13994967],
            [-0.07456091, 0.54797268],
            [-1.12894803, 0.32702141],
            [0.92084690, 0.02683249],
        ]).T
        input_layer = EuclideDistanceLayer(2, weight=weight)
        output_layer = CompetitiveOutputLayer(6)

        sn = SOFM(
            input_layer > output_layer,
            learning_radius=1,
            features_grid=(3, 2)
        )

        sn.train(input_data, epochs=10)

        answers = np.array([
            [0., 0., 0., 1., 0., 0.],
            [0., 0., 0., 1., 0., 0.],
            [1., 0., 0., 0., 0., 0.],
            [1., 0., 0., 0., 0., 0.],
            [0., 0., 0., 0., 1., 0.],
            [0., 0., 0., 0., 1., 0.],
        ])

        for data, answer in zip(input_data, answers):
            network_output = sn.predict(np.reshape(data, (2, 1)).T)
            correct_result = np.reshape(answer, (6, 1)).T
            self.assertTrue(np.all(network_output == correct_result))
Example #2
0
    def test_sofm(self):
        input_layer = LinearLayer(2, weight=self.weight)
        output_layer = CompetitiveOutputLayer(3)

        sn = SOFM(
            input_layer > output_layer,
            learning_radius=0,
            features_grid=(3, 1)
        )

        sn.train(input_data, epochs=100)

        answers = np.array([
            [0., 1., 0.],
            [0., 1., 0.],
            [1., 0., 0.],
            [1., 0., 0.],
            [0., 0., 1.],
            [0., 0., 1.],
        ])

        for data, answer in zip(input_data, answers):
            network_output = sn.predict(np.reshape(data, (2, 1)).T)
            correct_result = np.reshape(answer, (3, 1)).T
            self.assertTrue(np.all(network_output == correct_result))