示例#1
0
    def test_set_activation(self):
        g = Genome(3, 3)
        node_ids = range(len(g.get_nodes()))
        act = [activations.relu, activations.gaussian, activations.square]

        # Test to make sure the activation is set correctly
        msg = 'Activation set incorrectly!'
        for nid in node_ids:
            for a in act:
                g.set_activation(nid, a)
                self.assertEqual(g.get_node(nid).activation, a)
示例#2
0
    def test_forward(self):
        error_margin = 0.000000000001

        g = Genome(2, 2)
        x = np.array([0.5, 0.5])

        msg = 'Invalid genome output!'

        # No connections
        y = g(x)
        self.assertEqual(y[0], 0.0, msg=msg)
        self.assertEqual(y[1], 0.0, msg=msg)

        # No hidden nodes, modified sigmoid activation
        g.add_connection(0, 2, weight=-0.7)
        g.add_connection(0, 3, weight=-0.1)
        g.add_connection(1, 2, weight=0.5)
        g.add_connection(1, 3, weight=0.9)
        y = g(x)
        self.assertAlmostEqual(y[0],
                               0.3798935676569099,
                               msg=msg,
                               delta=error_margin)
        self.assertAlmostEqual(y[1],
                               0.8765329524347759,
                               msg=msg,
                               delta=error_margin)

        # Different activation
        for n in g.get_nodes():
            if n.type != 'input':
                g.set_activation(n.id, activations.absolute)
        y = g(x)
        self.assertAlmostEqual(y[0], 0.1, msg=msg, delta=error_margin)
        self.assertAlmostEqual(y[1], 0.4, msg=msg, delta=error_margin)

        # With hidden nodes and different activations (sigmoid for the new ones)
        g.add_node(0, activation=activations.sigmoid)
        g.add_node(2, activation=activations.sigmoid)
        g.add_connection(4, 5, 0.5)
        y = g(x)
        self.assertAlmostEqual(y[0],
                               0.08953579350695234,
                               msg=msg,
                               delta=error_margin)
        self.assertAlmostEqual(y[1], 0.4, msg=msg, delta=error_margin)

        # Test with many hidden nodes in a line
        g2 = Genome(2, 2)
        g2.add_connection(0, 2, -0.3)
        g2.add_node(0)
        g2.add_node(2)
        g2.add_node(4)
        g2.add_node(6)
        g2.add_connection(1, 7, 0.7)
        g2.add_connection(4, 3, -0.2)
        y = g2(x)
        self.assertAlmostEqual(y[0],
                               0.18866305913528142,
                               msg=msg,
                               delta=error_margin)
        self.assertAlmostEqual(y[1],
                               0.2743863603871294,
                               msg=msg,
                               delta=error_margin)

        # Test with recursive loop
        g3 = Genome(1, 1)
        x = np.array([0.5])
        g3.add_connection(0, 1, 0.5)
        g3.add_node(0)
        g3.add_node(0)
        g3.add_node(1)
        g3.add_connection(3, 2, 0.5)
        g3.add_connection(4, 3, 0.5)
        y = g3(x)
        self.assertAlmostEqual(y[0],
                               0.9907948306148218,
                               msg=msg,
                               delta=error_margin)