Google Colaboratory中的openAI Gym NameError

Jav*_*ias 7 python openai-gym google-colaboratory

我刚刚在Google Colab上安装了openAI gym,但是当我尝试按照此处的说明运行'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
Run Code Online (Sandbox Code Playgroud)

我明白了:

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
Run Code Online (Sandbox Code Playgroud)

关于openAI gym中NameError的问题,问题是一样的

什么都没有呈现.我不知道如何在谷歌colab中使用它:'xvfb-run -s \"-screen 0 1400x900x24\" python <your_script.py>'"

Yog*_*loi 9

在谷歌colab中渲染健身房环境的一种方法是在运行环境时使用pyvirtualdisplay并存储rgb帧阵列.可以使用matplotlib的动画功能和用于Ipython显示模块的HTML功能来动画环境帧.你可以在这里找到实现.确保安装所需的库,您可以在colab的第一个单元格中找到它们.如果谷歌colab的第一个链接不起作用,你可以看到这一个.


Kor*_*ich 6

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

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

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