【问题标题】:openAI Gym NameError in Google ColaboratoryGoogle Colaboratory 中的 openAI Gym NameError
【发布时间】:2018-03-09 22:18:13
【问题描述】:

我刚刚在 Google Colab 上安装了 openAI gym,但是当我尝试以 explained here 运行“CartPole-v0”环境时。

代码:

import gym
env = gym.make('CartPole-v0')
for i_episode in range(20):
    observation = env.reset()
    for t in range(100):
        env.render()
        print(observation)
        action = env.action_space.sample()
        observation, reward, done, info = env.step(action)
        if done:
            print("Episode finished after {} timesteps".format(t+1))
            break

我明白了:

WARN: gym.spaces.Box autodetected dtype as <class 'numpy.float32'>. Please provide explicit dtype.
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-19-a81cbed23ce4> in <module>()
      4     observation = env.reset()
      5     for t in range(100):
----> 6         env.render()
      7         print(observation)
      8         action = env.action_space.sample()

/content/gym/gym/core.py in render(self, mode)
    282 
    283     def render(self, mode='human'):
--> 284         return self.env.render(mode)
    285 
    286     def close(self):

/content/gym/gym/envs/classic_control/cartpole.py in render(self, mode)
    104 
    105         if self.viewer is None:
--> 106             from gym.envs.classic_control import rendering
    107             self.viewer = rendering.Viewer(screen_width, screen_height)
    108             l,r,t,b = -cartwidth/2, cartwidth/2, cartheight/2, -cartheight/2

/content/gym/gym/envs/classic_control/rendering.py in <module>()
     21 
     22 try:
---> 23     from pyglet.gl import *
     24 except ImportError as e:
     25     reraise(prefix="Error occured while running `from pyglet.gl import *`",suffix="HINT: make sure you have OpenGL install. On Ubuntu, you can run 'apt-get install python-opengl'. If you're running on a server, you may need a virtual frame buffer; something like this should work: 'xvfb-run -s \"-screen 0 1400x900x24\" python <your_script.py>'")

/usr/local/lib/python3.6/dist-packages/pyglet/gl/__init__.py in <module>()
    225     else:
    226         from .carbon import CarbonConfig as Config
--> 227 del base
    228 
    229 # XXX remove

NameError: name 'base' is not defined

this question about NameError in openAI gym中的问题是一样的

什么都没有被渲染。我不知道如何在 google colab 中使用它:'xvfb-run -s \"-screen 0 1400x900x24\" python &lt;your_script.py&gt;'"

【问题讨论】:

    标签: python google-colaboratory openai-gym


    【解决方案1】:

    在 google colab 中渲染健身房环境的一种方法是在运行环境时使用 pyvirtualdisplay 并存储 rgb 帧数组。可以使用 matplotlib 的动画功能和用于 Ipython 显示模块的 HTML 函数对环境帧进行动画处理。 你可以找到实现here。 确保安装所需的库,您可以在 colab 的第一个单元格中找到这些库。如果 google colab 的第一个链接不起作用,您可以查看 this one

    【讨论】:

    • 超级有帮助。在我们获得 WebGL 渲染之前,这绝对是任何在这里绊脚的人的方式。
    • @Yograj 它工作正常。但是你能不能添加几行代码来安装 Box2D。 CartPole 在它上面运行良好,但是当我使用 CarRacing 时它会抛出错误。我尝试从各种来源安装 Box2D,但一直失败。
    • @सत्यमेवजयते 你有没有为 box2 环境找到解决方案?
    • 我不记得了,我是怎么做到的。祝你好运。
    • 对不起,您请求的文件不存在。
    【解决方案2】:

    Gym 通常会使用 GL 在您的屏幕上呈现显示。

    但是 Colab 在网络上作为笔记本运行,它不能直接显示在您的屏幕上。它只能通过 HTML 显示结果。

    如果有人修改 Gym 以操纵 WebGL,也许有一天。但不是现在。

    【讨论】:

    • 谢谢 Korakot。我们还要等!
    【解决方案3】:

    我看到了一个合适的答案here

    pip install pyglet==1.5.11
    

    【讨论】:

      【解决方案4】:

      Javier,您能找到解决此问题的方法吗?我正在尝试使用 OenAIs 新环境“gym retro”并在调用 make 时遇到同样的错误。但是正如您所说,我认为使用 xvfb 应该可以解决问题并让程序运行,但是我们当然无法以图形方式查看环境。但问题是不允许xvfb在后台运行! xvfb :99 & 引发 OSError: 不支持后台进程。

      【讨论】:

      • 嗨!现在不行。我认为 Korakot 是对的,应该修改 Gym 以操纵 webGL,然后我们将能够看到我们的算法在运行。我一直在考虑在不调用 env.render() 的情况下训练算法,并尝试使用监视器生成体验的视频文件。你怎么看?
      • 嗨。好吧,我只是在没有调用 render 的情况下尝试了它,它似乎正在工作。但是由于我对健身房环境非常陌生,所以在录制和播放时,您是在谈论此页面中的播放部分:github.com/openai/retro
      • 我不确定播放部分的解释是否相同。你试过吗?
      • 这不是答案,而是评论,或者更确切地说是一个问题
      【解决方案5】:

      复制粘贴即可

      %%bash
      
      # install required system dependencies
      apt-get install -y xvfb x11-utils
      
      # install required python dependencies (might need to install additional gym extras depending)
      pip install gym[box2d]==0.17.* pyvirtualdisplay==0.2.* PyOpenGL==3.1.* PyOpenGL-accelerate==3.1.*
      
      #import
      import pyvirtualdisplay
      _display = pyvirtualdisplay.Display(visible=0,  # remember to use visible=0 and not False
                                          size=(1400, 900))
      _ = _display.start()
      
      #check
      !echo $DISPLAY
      

      【讨论】:

        猜你喜欢
        • 2019-04-27
        • 2022-10-08
        • 2019-07-14
        • 1970-01-01
        • 2020-10-18
        • 2022-08-24
        • 2017-10-24
        • 1970-01-01
        • 2017-01-13
        相关资源
        最近更新 更多