【问题标题】:How to fix neat recurrent.py file, neat python library openAI gym如何修复整洁的recurrent.py文件,整洁的python库openAI健身房
【发布时间】:2019-09-07 19:14:45
【问题描述】:

试图让 python 简洁的算法与 openAI gym retro一起使用。 我在 youtube 上使用 python3:https://www.youtube.com/watch?v=8dY3nQRcsac&list=PLTWFMbPFsvz3CeozHfeuJIXWAJMkPtAdS&index=8&t=410s 试图在 openAI 环境中与 sonic 一起工作。 recrrent.py 文件似乎有问题。

在这里找到代码:https://gitlab.com/lucasrthompson/Sonic-Bot-In-OpenAI-and-NEAT/blob/master/tut2.py

这是错误信息


    File "tut3.py", line 53, in <module>
        winner = p.run(eval_genomes)
      File "/home/gym/OPAI/lib/python3.6/site-packages/neat/population.py", line 89, in run
        fitness_function(list(iteritems(self.population)), self.config)
      File "tut3.py", line 41, in eval_genomes
        imgarray.append(y)
    AttributeError: 'numpy.ndarray' object has no attribute 'append'

population.py 文件中的第 89 行


    self.reporters.start_generation(self.generation)

                # Evaluate all genomes using the user-provided function.
                fitness_function(list(iteritems(self.population)), self.config)



我从@lucas 那里得到的 tut3 代码 只需计划整洁的网络即可。


import retro
import numpy as np
import pickle
import neat
import cv2

env = retro.make('SonicTheHedgehog-Genesis', 'GreenHillZone.Act1')

imgarray = []

def eval_genomes(genomes, config):

    for genome_id, genome in genomes:
        ob = env.reset()
        ac = env.action_space.sample()

    inx, iny, inc = env.observation_space.shape

    inx = int(inx/8)
    iny = int(iny/8)

    net = neat.nn.RecurrentNetwork.create(genome, config)
    current_max_fitness = 0
    fitness_current = 0
    frame = 0
    counter = 0
    xpos = 0
    xpos_max = 0

    done = False

    while not done:
            env.render()
            frame +=1
            ob = cv2.resize(ob, (inx,iny))
            ob = cv2.cvtColor(ob, cv2.COLOR_BGR2GRAY)
            ob = np.reshape(ob, (inx,iny))
            imgarray = np.ndarray.flatten(ob)
            for x in ob:
                for y in x:
                    imgarray.append(y)

                nnOutput = net.activate(imgarray)
                print(nnOutput)

                ob, rew,done, info = env.step(nnOutput)
                imgarray.clear()

config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
                     neat.DefaultSpeciesSet, neat.DefaultStagnation,
                     'config-feedforward')
p = neat.Population(config)
winner = p.run(eval_genomes)


如果你们能帮忙就太好了。 我想完全理解,因为这是一个学校项目。

感谢您的帮助 :))

【问题讨论】:

  • @lucas 你能看看吗:)

标签: python machine-learning openai-gym neat


【解决方案1】:

您的 while 循环中有一些错误。使您的 eval_genomes 函数如下所示:

def eval_genomes(genomes, config):

    for genome_id, genome in genomes:
        ob = env.reset()
        ac = env.action_space.sample()

    inx, iny, inc = env.observation_space.shape

    inx = int(inx/8)
    iny = int(iny/8)

    net = neat.nn.RecurrentNetwork.create(genome, config)
    current_max_fitness = 0
    fitness_current = 0
    frame = 0
    counter = 0
    xpos = 0
    xpos_max = 0

    done = False

    while not done:
            env.render()
            frame +=1
            ob = cv2.resize(ob, (inx, iny))
            ob = cv2.cvtColor(ob, cv2.COLOR_BGR2GRAY)                             
            ob = np.reshape(ob, (inx,iny))
            imgarray = np.ndarray.flatten(ob)
            nnOutput = net.activate(imgarray)
            print(nnOutput)
            ob, rew,done, info = env.step(nnOutput)

ndarray.flatten 与 for x 和 for y 循环执行相同的操作,因此您只需要两种解决方案之一,并且 flatten 更易于阅读。此外,python 是一种缩进非常重要的语言。始终确保您的制表符/空格排列正确!

希望有效。如果没有,请继续使用:

https://gitlab.com/lucasrthompson/Sonic-Bot-In-OpenAI-and-NEAT/blob/master/tut2.py

或者这个:

https://gitlab.com/lucasrthompson/Sonic-Bot-In-OpenAI-and-NEAT/blob/master/neat-paralle-sonic.py

祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-07
    • 2019-02-05
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多