.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/demo_agents/video_plot_dqn.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_demo_agents_video_plot_dqn.py: =============================================== A demo of DQN algorithm in CartPole environment =============================================== Illustration of how to set up a DQN algorithm in rlberry. The environment chosen here is gym's cartpole environment. As DQN can be computationally intensive and hard to tune, one can use tensorboard to visualize the training of the DQN using the following command: .. code-block:: bash tensorboard --logdir {Path(agent.writer.log_dir).parent} .. video:: ../../video_plot_dqn.mp4 :width: 600 .. GENERATED FROM PYTHON SOURCE LINES 20-56 .. code-block:: python3 from rlberry.envs import gym_make from torch.utils.tensorboard import SummaryWriter from rlberry_research.agents.torch.dqn import DQNAgent from rlberry.utils.logging import configure_logging from gymnasium.wrappers.rendering import RecordVideo import shutil import os configure_logging(level="INFO") env = gym_make("CartPole-v1", render_mode="rgb_array") agent = DQNAgent(env, epsilon_decay_interval=1000) agent.set_writer(SummaryWriter()) print(f"Running DQN on {env}") agent.fit(budget=50) env = RecordVideo(env, "_video/temp") for episode in range(3): done = False observation, info = env.reset() while not done: action = agent.policy(observation) observation, reward, terminated, truncated, info = env.step(action) done = terminated or truncated env.close() # need to move the final result inside the folder used for documentation os.rename("_video/temp/rl-video-episode-0.mp4", "_video/video_plot_dqn.mp4") shutil.rmtree("_video/temp/") .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.000 seconds) .. _sphx_glr_download_auto_examples_demo_agents_video_plot_dqn.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: video_plot_dqn.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: video_plot_dqn.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_