Skip to content
Snippets Groups Projects
Commit 6a76078f authored by Christof Kaufmann's avatar Christof Kaufmann
Browse files

Add tutorial for graphical output of gym environments

parents
No related branches found
No related tags found
No related merge requests found
# Jupyter Tutorials
This is a collection of tutorials for Jupyter Notebooks.
%% Cell type:markdown id:cb5980cf-bb11-477c-975d-c555dbd61e22 tags:
# Graphic Output of Gymnasium Environments
Although values are probably better to monitor, when you got some experience, in the beginning, for debugging and for presentations you like to watch your agent playing games. Depending on the IDE there are partially different ways to do it.
## Recording Videos
The best solution is probably to make periodic videos. In this way, you can look at the capabilities at different learning states and you can rewatch it later on. In JupyterLab right-click on a video and select *Open in New Browser Tab*.
%% Cell type:code id:b425cecc-4212-434f-b57c-a9292c5a08d1 tags:
``` python
import gymnasium as gym
# env = gym.make("Pong-v4", render_mode="rgb_array")
# env.metadata['render_fps'] = 30
env = gym.make("LunarLander-v2", render_mode="rgb_array")
env = gym.wrappers.RecordVideo(env, './video/', disable_logger=True,
episode_trigger=lambda episode_id: episode_id % 5 == 0) # every 5th episode
observation, info = env.reset()
for _ in range(1000):
action = env.action_space.sample()
observation, reward, terminated, truncated, info = env.step(action)
if terminated or truncated:
observation, info = env.reset()
env.close()
```
%% Cell type:markdown id:9f7a4b36-5204-4783-9b6e-55eb3568b591 tags:
## Live View using Matplotlib
You can also watch the game live during training, maybe every few episodes or so. For that, choose `render_mode="rgb_array"` and make a first `imshow` plot. *Very important: The cell has to end after making the plot, because the plot will only be visible when the cell finishes.* In the second cell the plot gets updated.
%% Cell type:code id:17be9d89-1246-4cd7-9258-49cff9ee6da9 tags:
``` python
import gymnasium as gym
import matplotlib.pyplot as plt
# you maybe have to restart your kernel, if you changed the matplotlib backend before
%matplotlib widget
env = gym.make("LunarLander-v2", render_mode="rgb_array")
observation, info = env.reset() # is required once before calling env.render()
img = plt.imshow(env.render()) # only call this once to bring up the plot
plt.show()
# IMPORTANT: Split your code at this point into two cells, so the plot can be drawn
```
%% Cell type:code id:fe77c845-f5b0-4462-a4e6-c2b5c7fd71cf tags:
``` python
observation, info = env.reset()
for _ in range(150):
action = env.action_space.sample()
observation, reward, terminated, truncated, info = env.step(action)
if terminated or truncated:
observation, info = env.reset()
# update plot
img.set_data(env.render())
plt.gcf().canvas.draw()
env.close()
print('done.')
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment