def test_match_neat():
    with open("tests/test-genome.pkl", "rb") as f:
        genome = pickle.load(f)

    # use tanh since neat sets output nodes with no inputs to 0
    # (sigmoid would output 0.5 for us)
    def neat_tanh_activation(z):
        return float(torch.tanh(2.5 * torch.tensor(z, dtype=torch.float64)))

    for node in genome.nodes.values():
        node.response = 0.5

    config = neat.Config(
        neat.DefaultGenome,
        neat.DefaultReproduction,
        neat.DefaultSpeciesSet,
        neat.DefaultStagnation,
        "tests/test-config.cfg",
    )

    for _ in range(500):
        genome.mutate(config.genome_config)
        # print(genome)

        neat_net = neat.nn.RecurrentNetwork.create(genome, config)
        for i, (node, _activation, aggregation, bias, response,
                links) in enumerate(neat_net.node_evals):
            neat_net.node_evals[i] = (
                node,
                neat_tanh_activation,
                aggregation,
                bias,
                response,
                links,
            )

        torch_net = RecurrentNet.create(genome,
                                        config,
                                        activation=tanh_activation,
                                        prune_empty=True)

        for _ in range(5):
            inputs = np.random.randn(12)
            # print(inputs)
            neat_result = neat_net.activate(inputs)
            torch_result = torch_net.activate([inputs])[0].numpy()
            assert np.allclose(neat_result, torch_result, atol=1e-8)
Beispiel #2
0
def make_net(genome, config, bs):
    # TODO: get number of agents directly instead of magic number
    return RecurrentNet.create(genome, config, bs * 2)
Beispiel #3
0
def make_net(genome, config, bs):
    return RecurrentNet.create(genome, config, bs)
 def __init__(self, genome, config, batch_size=64):
     self._batch_size = batch_size
     self._net = RecurrentNet.create(genome, config, batch_size)