微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 Neat 算法打开 AI Gym 不适用于 Jupyter

如何解决使用 Neat 算法打开 AI Gym 不适用于 Jupyter

import multiprocessing
import os
import pickle
import numpy as np
import neat
import gym

runs_per_net = 2

# Use the NN network phenotype and the discrete actuator force function.
def eval_genome(genome,config):
    net = neat.nn.FeedForwardNetwork.create(genome,config)

    fitnesses = []

    for runs in range(runs_per_net):
        env = gym.make("CartPole-v1")
        observation = env.reset()
        # Run the given simulation for up to num_steps time steps.
        fitness = 0.0
        done = False
        while not done:
            
            action = np.argmax(net.activate(observation))
            observation,reward,done,info = env.step(action)    
            fitness += reward

        fitnesses.append(fitness)

    # The genome's fitness is its worst performance across all runs.
    return np.mean(fitnesses)

def eval_genomes(genomes,config):
    for genome_id,genome in genomes:
        genome.fitness = eval_genome(genome,config)


def run():
    # Load the config file,which is assumed to live in
    # the same directory as this script.
    config = neat.Config(neat.DefaultGenome,neat.DefaultReproduction,neat.DefaultSpeciesSet,neat.DefaultStagnation,"config")

    pop = neat.Population(config)
    stats = neat.StatisticsReporter()
    pop.add_reporter(stats)
    pop.add_reporter(neat.StdOutReporter(True))

    pe = neat.ParallelEvaluator(multiprocessing.cpu_count(),eval_genome)
    winner = pop.run(pe.evaluate)

    # Save the winner.
    with open('winner','wb') as f:
        pickle.dump(winner,f)

    print(winner)

if __name__ == '__main__':
    run()

尝试在 jupyter notebook 上使用 Neat Algorithm 运行 CartPole v1,但输出卡住

**** Running generation 0 **** 

随着 GPU 使用量的增加,而不是 cpu

但是当尝试在 CMD 上运行它时,一切正常(GPU 使用正常)。有什么办法可以在 Jupyter Notebook 上做到这一点。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。