Display OpenAI gym in Jupyter notebook only

后端 未结 2 557
[愿得一人]
[愿得一人] 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:29

    This worked for me in Ubuntu 18.04 LTS, to render gym locally. But, I believe it will work even in remote Jupyter Notebook servers.

    First, run the following installations in Terminal:

    pip install gym
    python -m pip install pyvirtualdisplay
    pip3 install box2d
    sudo apt-get install xvfb
    

    That's just it. Use the following snippet to configure how your matplotlib should render :

    import matplotlib.pyplot as plt
    from pyvirtualdisplay import Display
    display = Display(visible=0, size=(1400, 900))
    display.start()
    
    is_ipython = 'inline' in plt.get_backend()
    if is_ipython:
        from IPython import display
    
    plt.ion()
    
    # Load the gym environment
    
    import gym
    import matplotlib.pyplot as plt
    %matplotlib inline
    
    env = gym.make('LunarLander-v2')
    env.seed(23)
    
    # Let's watch how an untrained agent moves around
    
    state = env.reset()
    img = plt.imshow(env.render(mode='rgb_array'))
    for j in range(200):
    #     action = agent.act(state)
        action = random.choice(range(4))
        img.set_data(env.render(mode='rgb_array')) 
        plt.axis('off')
        display.display(plt.gcf())
        display.clear_output(wait=True)
        state, reward, done, _ = env.step(action)
        if done:
            break 
            
    env.close()
    

提交回复
热议问题