简体   繁体   English

Google Colaboratory 中的 openAI Gym NameError

[英]openAI Gym NameError in Google Colaboratory

I've just installed openAI gym on Google Colab, but when I try to run 'CartPole-v0' environment as explained here .我刚刚在 Google Colab 上安装了 openAI 健身房,但是当我尝试运行“CartPole-v0”环境时,如此处所述

Code:代码:

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

I get this:我明白了:

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

The problem is the same in this question about NameError in openAI gym这个问题与openAI Gym中关于NameError的问题相同

Nothing is being rendered.没有任何东西被渲染。 I don't know how I could use this in google colab: 'xvfb-run -s \\"-screen 0 1400x900x24\\" python <your_script.py>'"我不知道如何在 google colab 中使用它: 'xvfb-run -s \\"-screen 0 1400x900x24\\" python <your_script.py>'"

One way to render gym environment in google colab is to use pyvirtualdisplay and store rgb frame array while running environment.在 google colab 中渲染健身房环境的一种方法是在运行环境时使用 pyvirtualdisplay 并存储 rgb 帧数组。 Environment frames can be animated using animation feature of matplotlib and HTML function used for Ipython display module.环境帧可以使用 matplotlib 的动画特性和用于 Ipython 显示模块的 HTML 函数进行动画处理。 You can find the implementation here .您可以在此处找到实现。 Make sure you install required libraries which you can find in the first cell of the colab.确保安装所需的库,您可以在 colab 的第一个单元格中找到这些库。 In case the first link for google colab doesn't work you can see this one .如果 google colab 的第一个链接不起作用,您可以查看此链接

The Gym will normally render the display using GL, on your screen. Gym 通常会在您的屏幕上使用 GL 渲染显示。

But Colab is run on the web as a notebook, it can't display directly to your screen.但是 Colab 作为笔记本在网络上运行,它无法直接显示在您的屏幕上。 It can only show the result through HTML.它只能通过 HTML 显示结果。

If someone modifies the Gym to maniplulate WebGL, maybe someday.如果有人修改 Gym 来操纵 WebGL,也许有一天。 But not now.但不是现在。

Javier, Could you find any solution to this issue?哈维尔,你能找到解决这个问题的方法吗? I am trying to use the OenAIs new environment "gym retro" and getting same kind of error when calling make.我正在尝试使用 OenAI 的新环境“gym retro”并在调用 make 时遇到相同类型的错误。 But as you said I think using xvfb should resolve the issue and let the program run, but of course we wouldn't be able to see the environment graphically.但是正如您所说,我认为使用 xvfb 应该可以解决问题并让程序运行,但是我们当然无法以图形方式查看环境。 But the issue is that does not allow xvfb to run in the background!但问题是不允许 xvfb 在后台运行! xvfb :99 & raises OSError: Background processes not supported. xvfb :99 & 引发 OSError:不支持后台进程。

我在这里看到了一个合适的答案。

pip install pyglet==1.5.11

Just copy paste this只需复制粘贴这个

%%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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM