在Jupyter笔记本中使用AI-Gym Python图形的困难

Reb*_*bin 6 python matplotlib jupyter openai-gym

我正试图在Jupyter笔记本中显示AI-Gym演示.我获得了Atari演示的良好结果Breakout-v0以及购物车极点演示的难度错误消息CartPole-v0.两者在笔记本电脑外都很好用.以下是最小的细节:

没有JUPYTER

在控制台:

$ pip install gym[atari] &> /dev/null
$ /anaconda3/bin/python3
Python 3.6.3 |Anaconda, Inc.| (default, Oct  6 2017, 12:04:38)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import gym
>>> env = gym.make('Breakout-v0')
>>> env.reset()
>>> env.render()
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述

现在,同样使用cart-pole,在一个新的Python会话中:

$ pip install gym &> /dev/null
$ /anaconda3/bin/python3
Python 3.6.3 |Anaconda, Inc.| (default, Oct  6 2017, 12:04:38)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> env = gym.make('CartPole-v0')
>>> env.reset()
>>> env.render()
Run Code Online (Sandbox Code Playgroud)

上面Python代码的唯一区别是demo的名称:CartPole-v0而不是Breakout-v0.

结果: 在此输入图像描述

与JUPYTER

从这个问题的一些提示:如何在服务器上运行OpenAI Gym .render(),我有这个Breakout演示:

在此输入图像描述

或者,在文本中:

import gym
from IPython import display
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
env = gym.make('Breakout-v0')
env.reset()
img = plt.imshow(env.render(mode='rgb_array'))
img.set_data(env.render(mode='rgb_array'))
display.display(plt.gcf())
display.clear_output(wait=True)
Run Code Online (Sandbox Code Playgroud)

现在,对于购物车杆,再次使用新的内核会话和笔记本,唯一的区别是演示的名称:

import gym
from IPython import display
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
env = gym.make('CartPole-v0')
env.reset()
img = plt.imshow(env.render(mode='rgb_array'))
img.set_data(env.render(mode='rgb_array'))
display.display(plt.gcf())
display.clear_output(wait=True)
Run Code Online (Sandbox Code Playgroud)

我得到一个很长的错误消息抱怨一些pyglet类是抽象的.堆栈跟踪的最后一个元素如下:

/anaconda3/lib/python3.6/site-packages/pyglet/canvas/base.py in get_screens(self)
     63         :rtype: list of :class:`Screen`
     64         '''
---> 65         raise NotImplementedError('abstract')
     66 
     67     def get_default_screen(self):

NotImplementedError: abstract
Run Code Online (Sandbox Code Playgroud)

整个堆栈轨迹位于底部以供检查.我仔细阅读但无法确定问题以及如何解决问题.我很感激你的建议.

总结一下,

NO JUPYTER:
  Breakout: OK
  CartPole: OK
JUPYTER:
  Breakout: OK
  CartPole: ERROR
Run Code Online (Sandbox Code Playgroud)

整个堆栈跟踪如下:

NotImplementedError                       Traceback (most recent call last)
<ipython-input-1-df4d39818fe3> in <module>()
      6 env = gym.make('CartPole-v0')
      7 env.reset()
----> 8 img = plt.imshow(env.render(mode='rgb_array'))
      9 img.set_data(env.render(mode='rgb_array'))
     10 display.display(plt.gcf())

/anaconda3/lib/python3.6/site-packages/gym/core.py in render(self, mode, close)
    148             elif mode not in modes:
    149                 raise error.UnsupportedMode('Unsupported rendering mode: {}. (Supported modes for {}: {})'.format(mode, self, modes))
--> 150         return self._render(mode=mode, close=close)
    151 
    152     def close(self):

/anaconda3/lib/python3.6/site-packages/gym/core.py in _render(self, mode, close)
    284 
    285     def _render(self, mode='human', close=False):
--> 286         return self.env.render(mode, close)
    287 
    288     def _close(self):

/anaconda3/lib/python3.6/site-packages/gym/core.py in render(self, mode, close)
    148             elif mode not in modes:
    149                 raise error.UnsupportedMode('Unsupported rendering mode: {}. (Supported modes for {}: {})'.format(mode, self, modes))
--> 150         return self._render(mode=mode, close=close)
    151 
    152     def close(self):

/anaconda3/lib/python3.6/site-packages/gym/envs/classic_control/cartpole.py in _render(self, mode, close)
    114         if self.viewer is None:
    115             from gym.envs.classic_control import rendering
--> 116             self.viewer = rendering.Viewer(screen_width, screen_height)
    117             l,r,t,b = -cartwidth/2, cartwidth/2, cartheight/2, -cartheight/2
    118             axleoffset =cartheight/4.0

/anaconda3/lib/python3.6/site-packages/gym/envs/classic_control/rendering.py in __init__(self, width, height, display)
     49         self.width = width
     50         self.height = height
---> 51         self.window = pyglet.window.Window(width=width, height=height, display=display)
     52         self.window.on_close = self.window_closed_by_user
     53         self.geoms = []

/anaconda3/lib/python3.6/site-packages/pyglet/window/__init__.py in __init__(self, width, height, caption, resizable, style, fullscreen, visible, vsync, display, screen, config, context, mode)
    502 
    503         if not screen:
--> 504             screen = display.get_default_screen()
    505 
    506         if not config:

/anaconda3/lib/python3.6/site-packages/pyglet/canvas/base.py in get_default_screen(self)
     71         :rtype: :class:`Screen`
     72         '''
---> 73         return self.get_screens()[0]
     74 
     75     def get_windows(self):

/anaconda3/lib/python3.6/site-packages/pyglet/canvas/base.py in get_screens(self)
     63         :rtype: list of :class:`Screen`
     64         '''
---> 65         raise NotImplementedError('abstract')
     66 
     67     def get_default_screen(self):

NotImplementedError: abstract
Run Code Online (Sandbox Code Playgroud)

Pip*_*ing 0

只需编写一个.py脚本,渲染就会在新窗口中完成。为我在 mac 上工作!