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()
instructions_linux_weai.tex
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.
Making GIFs
GIFs are fun and easy to make and look at. Here is a way to make them after every episode.
import gymnasium as gym
import PIL.Image
import os
# env = gym.make("Pong-v4", render_mode="rgb_array")
# env.metadata['render_fps'] = 30
env = gym.make("CartPole-v1", render_mode="rgb_array_list")
def save_gif(frames, filename='gym.gif'):
directory = os.path.dirname(filename)
if not os.path.exists(directory):
os.makedirs(directory)
images = [PIL.Image.fromarray(frame) for frame in frames]
images[0].save(filename, format='GIF', append_images=images[1:],
save_all=True, duration=30, loop=0)
observation, info = env.reset()
gif_count = 0
for _ in range(1000):
action = env.action_space.sample()
observation, reward, terminated, truncated, info = env.step(action)
if terminated or truncated:
# save gif for every episode
save_gif(env.render(), f'gifs/gym-{gif_count:04}.gif')
gif_count += 1
observation, info = env.reset()
env.close()
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.
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
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.')