问题描述
我想在笔记本中玩 OpenAI 健身房,并在线渲染健身房.
I want to play with the OpenAI gyms in a notebook, with the gym being rendered inline.
这是一个基本示例:
import matplotlib.pyplot as plt
import gym
from IPython import display
%matplotlib inline
env = gym.make('CartPole-v0')
env.reset()
for i in range(25):
plt.imshow(env.render(mode='rgb_array'))
display.display(plt.gcf())
display.clear_output(wait=True)
env.step(env.action_space.sample()) # take a random action
env.close()
这有效,我可以在笔记本中看到健身房:
This works, and I get see the gym in the notebook:
但是!它还会打开一个交互式窗口,显示完全相同的内容.我不想打开这个窗口:
But! it also opens an interactive window that shows precisely the same thing. I don't want this window to be open:
推荐答案
我在这里做了一个可以 fork 的工作示例:https://kyso.io/eoin/openai-gym-jupyter 有两个在 Jupyter 中渲染的示例 - 一个作为 mp4,另一个作为实时 gif.
I made a working example here that you can fork: https://kyso.io/eoin/openai-gym-jupyter with two examples of rendering in Jupyter - one as an mp4, and another as a realtime gif.
.mp4 示例非常简单.
The .mp4 example is quite simple.
import gym
from gym import wrappers
env = gym.make('SpaceInvaders-v0')
env = wrappers.Monitor(env, "./gym-results", force=True)
env.reset()
for _ in range(1000):
action = env.action_space.sample()
observation, reward, done, info = env.step(action)
if done: break
env.close()
然后在一个新的单元格中
Then in a new cell
import io
import base64
from IPython.display import HTML
video = io.open('./gym-results/openaigym.video.%s.video000000.mp4' % env.file_infix, 'r+b').read()
encoded = base64.b64encode(video)
HTML(data='''
<video width="360" height="auto" alt="test" controls><source src="data:video/mp4;base64,{0}" type="video/mp4" /></video>'''
.format(encoded.decode('ascii')))
这篇关于仅在 Jupyter 笔记本中显示 OpenAI 健身房的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!