Display OpenAI gym in Jupyter notebook only

后端 未结 2 559
[愿得一人]
[愿得一人] 2020-12-31 09:02

I want to play with the OpenAI gyms in a notebook, with the gym being rendered inline.

Here\'s a basic example:

import matplotlib.pyplot as plt
impor         


        
2条回答
  •  一向
    一向 (楼主)
    2020-12-31 09:25

    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.

    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='''
        '''
    .format(encoded.decode('ascii')))
    

提交回复
热议问题