python - Google Colaboratory 中的 openAI Gym NameError

标签 python google-colaboratory openai-gym

我刚刚在 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 <your_script.py>'"

最佳答案

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

关于python - Google Colaboratory 中的 openAI Gym NameError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49203023/

相关文章:

python - FrozenLake-v1 环境中的渲染问题

python - 人脸检测抛出错误:函数 cv::CascadeClassifier::detectMultiScale 中的 !empty()

python - 如何使用与换行符不同的分隔符读取子进程标准输出

pyspark - findspark.init() IndexError : list index out of range: PySpark on Google Colab

reinforcement-learning - 为什么我们总是需要为开放健身房ai设置env.seed(#)?

python - 如何在 Google Colab 上创建和使用自定义 OpenAI Gym 环境?

python - Pandas:向表示日期的长整数添加 0

python - 检查变量是字符串类型还是整数类型

jupyter-notebook - 谷歌 Colab : how to turn off suggestion window?

python - 无法加载在 Google Colaboratory 中创建的模型