How to render OpenAI gym in google Colab?

题意:如何在Google Colab中渲染OpenAI Gym

问题背景:

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.

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

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.

我发现了一些针对Jupyter notebook的解决方案,然而,这些解决方案不适用于Colab,因为我无法访问远程服务器。

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

我想知道是否有人知道一个适用于Google Colab的解决方案或变通方法?

问题解决:

Korakot's answer is not correct.        Korakot 的回答是不正确的。

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

你确实可以在Google Colab中渲染OpenAI Gym环境,尽管使用matplotlib可能会有点慢。

Heres how:        方法如下:

Install xvfb & other dependencies

安装 xvfb 及其他依赖项

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

As well as pyvirtual display:

以及 pyvirtualdisplay:

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

then import all your libraries, including matplotlib & ipythondisplay:

然后导入所有库,包括 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... :

然后你需要从 pyvirtualdisplay 导入 Display 并初始化你的屏幕尺寸,在这个例子中为 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)

最后但同样重要的是,使用 Gym 的 "rgb_array" 渲染函数将图像渲染到一个 "Screen" 变量中,然后使用 Matplotlib 绘制这个屏幕变量!(通过 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:

链接到我的工作 Colaboratory 笔记本,它演示了 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.

注意:并非所有 Gym 环境都支持 "rgb_array" 渲染模式,但大多数基础环境都支持。

  • 23
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在现如今这个互联网以及信息技术飞速发展的时代,信息技术被广泛地运用于人们的日常生活与生产中,并有效地提高了办事、办公效率。教育信息化的高速发展,使得学校在线考试系统,作为一种新的考试管理工具,在各大院校及相关教育机构的运用日趋普遍。绝大多数的考试机构需要不断地推进自身信息化的建设,而在考务管理方面,以往手工记账、登记信息的方式,已经逐渐不能满足现如今大数据量、高效率完成的要求,使用学校在线考试系统,对学校及相关教育机构的考试事宜管理已成当下教育信息化发展的必要手段。现代化的考试机构都应该进行学校在线考试系统的替换工作,学校在线考试系统一经推出,便在考试机构范围内引起了极大地反响。 本次学校在线考试系统的实现过程,它的开发使用B/S结构即浏览器和服务器结构框架,采用SSM框架技术,数据库使用了mysql数据库,页面设计采用了MVC框架,后端采用了SSM框架技术scrip等其他一些脚本语言,使用到在大学里面学的软件工程导论课程、mysql数据库、数据库原理、SSM框架技术高级程序设计等方面的知识完成本系统。本文结合全流程网站系统开发过程,详细介绍了此次学校在线考试系统的设计理念、模型结构,将所学知识融入到本网站的开发实践中,简略介绍了研究的背景和系统存在的现实意义,开发使用的技术背景,并对系统概要设计、系统实现与系统测试等进行了详细的介绍。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

营赢盈英

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值