本文介绍了如何在Google Colab中渲染OpenAI Gym?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Google colab中使用OpenAI Gym.由于Notebook在远程服务器上运行,因此我无法渲染Gym的环境.

我找到了一些适用于Jupyter笔记本的解决方案,但是,由于我无权访问远程服务器,因此这些解决方案无法与colab配合使用.

我想知道是否有人知道可以与Google Colab一起使用的解决方法吗?

解决方案

Korakot的答案不正确.

您确实可以使用matplotlib轻松地以协作,缓慢的方式渲染OpenAi Gym.

在这里:

安装 xvfb &其他依赖性

!apt-get install -y xvfb python-opengl > /dev/null 2>&1

&安装虚拟显示器:

!pip install gym pyvirtualdisplay > /dev/null 2>&1

然后导入所有库,包括 matplotlib & ipythondisplay :

import gym
import numpy as np
import matplotlib.pyplot as plt
from IPython import display as ipythondisplay

然后您要从虚拟显示&初始化屏幕尺寸,在此示例中为400x300 ...:

from pyvirtualdisplay import Display
display = Display(visible=0, size=(400, 300))
display.start()

最后但并非最不重要的一点,是使用功能强大的Gym的"rgb_array"渲染,渲染到"Screen"变量,然后使用Matplotlib绘制屏幕变量! (使用Ipython显示间接渲染)

env = gym.make("CartPole-v0")
env.reset()
prev_screen = env.render(mode='rgb_array')
plt.imshow(prev_screen)

for i in range(50):
  action = env.action_space.sample()
  obs, reward, done, info = env.step(action)
  screen = env.render(mode='rgb_array')

  plt.imshow(screen)
  ipythondisplay.clear_output(wait=True)
  ipythondisplay.display(plt.gcf())

  if done:
    break

ipythondisplay.clear_output(wait=True)
env.close()

链接到我正在工作的协作笔记本演示小插曲:

https://colab.research.google.com/drive/16gZuQlwxmxR5ZWYLZvBeq3bTd /p>

注意:并非所有的Gym Environments都支持"rgb_array"渲染模式,但是大多数基本的环境都支持.

I'm trying to use OpenAI gym in google colab. As the Notebook is running on a remote server I can not render gym's environment.

I found some solution for Jupyter notebook, however, these solutions do not work with colab as I don't have access to the remote server.

I wonder if someone knows a workaround for this that works with google Colab?

Korakot's answer is not correct.

You can indeed render OpenAi Gym in colaboratory, albiet kind of slowly using none other than matplotlib.

Heres how:

install xvfb & other dependancies

!apt-get install -y xvfb python-opengl > /dev/null 2>&1

& install pyvirtual display:

!pip install gym pyvirtualdisplay > /dev/null 2>&1

then import all your libraries, including matplotlib & ipythondisplay:

import gym
import numpy as np
import matplotlib.pyplot as plt
from IPython import display as ipythondisplay

then you want to import Display from pyvirtual display & initialise your screen size, in this example 400x300... :

from pyvirtualdisplay import Display
display = Display(visible=0, size=(400, 300))
display.start()

last but not least, using gym's "rgb_array" render functionally, render to a "Screen" variable, then plot the screen variable using Matplotlib! (rendered indirectly using Ipython display)

env = gym.make("CartPole-v0")
env.reset()
prev_screen = env.render(mode='rgb_array')
plt.imshow(prev_screen)

for i in range(50):
  action = env.action_space.sample()
  obs, reward, done, info = env.step(action)
  screen = env.render(mode='rgb_array')

  plt.imshow(screen)
  ipythondisplay.clear_output(wait=True)
  ipythondisplay.display(plt.gcf())

  if done:
    break

ipythondisplay.clear_output(wait=True)
env.close()

Link to my working Colaboratory notebook demoing cartpole:

https://colab.research.google.com/drive/16gZuQlwxmxR5ZWYLZvBeq3bTdFfb1r_6

Note: not all Gym Environments support "rgb_array" render mode, but most of the basic ones do.

这篇关于如何在Google Colab中渲染OpenAI Gym?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-07 18:28