如何修复整齐的recurrent.py文件,整洁的python库openAI健身房

问题描述 投票:-1回答:1

试图让python整洁的算法与openAI gym retro一起使用。我正在使用youtube的python3:https://www.youtube.com/watch?v=8dY3nQRcsac&list=PLTWFMbPFsvz3CeozHfeuJIXWAJMkPtAdS&index=8&t=410s试图在openAI env中使用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)


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

谢谢你的帮助 :))

python machine-learning openai-gym neat
1个回答
0
投票

你的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和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

祝好运!

© www.soinside.com 2019 - 2024. All rights reserved.