{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "njb_ProuHiOe" }, "source": [ "# Unit 1: Train your first Deep Reinforcement Learning Agent ๐Ÿค–\n", "\n", "![Cover](https://huggingface.co/datasets/huggingface-deep-rl-course/course-images/resolve/main/en/unit1/thumbnail.jpg)\n", "\n", "In this notebook, you'll train your **first Deep Reinforcement Learning agent** a Lunar Lander agent that will learn to **land correctly on the Moon ๐ŸŒ•**. Using [Stable-Baselines3](https://stable-baselines3.readthedocs.io/en/master/) a Deep Reinforcement Learning library, share them with the community, and experiment with different configurations\n", "\n", "โฌ‡๏ธ Here is an example of what **you will achieve in just a couple of minutes.** โฌ‡๏ธ\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "id": "PF46MwbZD00b" }, "outputs": [ { "data": { "text/html": [ "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%html\n", "" ] }, { "cell_type": "markdown", "metadata": { "id": "x7oR6R-ZIbeS" }, "source": [ "### The environment ๐ŸŽฎ\n", "\n", "- [LunarLander-v2](https://gymnasium.farama.org/environments/box2d/lunar_lander/)\n", "\n", "### The library used ๐Ÿ“š\n", "\n", "- [Stable-Baselines3](https://stable-baselines3.readthedocs.io/en/master/)" ] }, { "cell_type": "markdown", "metadata": { "id": "OwEcFHe9RRZW" }, "source": [ "We're constantly trying to improve our tutorials, so **if you find some issues in this notebook**, please [open an issue on the Github Repo](https://github.com/huggingface/deep-rl-class/issues)." ] }, { "cell_type": "markdown", "metadata": { "id": "4i6tjI2tHQ8j" }, "source": [ "## Objectives of this notebook ๐Ÿ†\n", "\n", "At the end of the notebook, you will:\n", "\n", "- Be able to use **Gymnasium**, the environment library.\n", "- Be able to use **Stable-Baselines3**, the deep reinforcement learning library.\n", "- Be able to **push your trained agent to the Hub** with a nice video replay and an evaluation score ๐Ÿ”ฅ.\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "Ff-nyJdzJPND" }, "source": [ "## This notebook is from Deep Reinforcement Learning Course\n", "\n", "\"Deep" ] }, { "cell_type": "markdown", "metadata": { "id": "6p5HnEefISCB" }, "source": [ "In this free course, you will:\n", "\n", "- ๐Ÿ“– Study Deep Reinforcement Learning in **theory and practice**.\n", "- ๐Ÿง‘โ€๐Ÿ’ป Learn to **use famous Deep RL libraries** such as Stable Baselines3, RL Baselines3 Zoo, CleanRL and Sample Factory 2.0.\n", "- ๐Ÿค– Train **agents in unique environments**\n", "- ๐ŸŽ“ **Earn a certificate of completion** by completing 80% of the assignments.\n", "\n", "And more!\n", "\n", "Check ๐Ÿ“š the syllabus ๐Ÿ‘‰ https://simoninithomas.github.io/deep-rl-course\n", "\n", "Donโ€™t forget to **sign up to the course** (we are collecting your email to be able toย **send you the links when each Unit is published and give you information about the challenges and updates).**\n", "\n", "The best way to keep in touch and ask questions is **to join our discord server** to exchange with the community and with us ๐Ÿ‘‰๐Ÿป https://discord.gg/ydHrjt3WP5" ] }, { "cell_type": "markdown", "metadata": { "id": "Y-mo_6rXIjRi" }, "source": [ "## Prerequisites ๐Ÿ—๏ธ\n", "\n", "Before diving into the notebook, you need to:\n", "\n", "๐Ÿ”ฒ ๐Ÿ“ **[Read Unit 0](https://huggingface.co/deep-rl-course/unit0/introduction)** that gives you all the **information about the course and helps you to onboard** ๐Ÿค—\n", "\n", "๐Ÿ”ฒ ๐Ÿ“š **Develop an understanding of the foundations of Reinforcement learning** (RL process, Rewards hypothesis...) by [reading Unit 1](https://huggingface.co/deep-rl-course/unit1/introduction)." ] }, { "cell_type": "markdown", "metadata": { "id": "HoeqMnr5LuYE" }, "source": [ "## A small recap of Deep Reinforcement Learning ๐Ÿ“š\n", "\n", "\"The" ] }, { "cell_type": "markdown", "metadata": { "id": "xcQYx9ynaFMD" }, "source": [ "Let's do a small recap on what we learned in the first Unit:\n", "\n", "- Reinforcement Learning is a **computational approach to learning from actions**. We build an agent that learns from the environment by **interacting with it through trial and error** and receiving rewards (negative or positive) as feedback.\n", "\n", "- The goal of any RL agent is to **maximize its expected cumulative reward** (also called expected return) because RL is based on the _reward hypothesis_, which is that all goals can be described as the maximization of an expected cumulative reward.\n", "\n", "- The RL process is a **loop that outputs a sequence of state, action, reward, and next state**.\n", "\n", "- To calculate the expected cumulative reward (expected return), **we discount the rewards**: the rewards that come sooner (at the beginning of the game) are more probable to happen since they are more predictable than the long-term future reward.\n", "\n", "- To solve an RL problem, you want to **find an optimal policy**; the policy is the \"brain\" of your AI that will tell us what action to take given a state. The optimal one is the one that gives you the actions that max the expected return.\n", "\n", "There are **two** ways to find your optimal policy:\n", "\n", "- By **training your policy directly**: policy-based methods.\n", "- By **training a value function** that tells us the expected return the agent will get at each state and use this function to define our policy: value-based methods.\n", "\n", "- Finally, we spoke about Deep RL because **we introduce deep neural networks to estimate the action to take (policy-based) or to estimate the value of a state (value-based) hence the name \"deep.\"**" ] }, { "cell_type": "markdown", "metadata": { "id": "qDploC3jSH99" }, "source": [ "# Let's train our first Deep Reinforcement Learning agent and upload it to the Hub ๐Ÿš€\n", "\n", "## Get a certificate ๐ŸŽ“\n", "\n", "To validate this hands-on for the [certification process](https://huggingface.co/deep-rl-course/en/unit0/introduction#certification-process), you need to push your trained model to the Hub and **get a result of >= 200**.\n", "\n", "To find your result, go to the [leaderboard](https://huggingface.co/spaces/huggingface-projects/Deep-Reinforcement-Learning-Leaderboard) and find your model, **the result = mean_reward - std of reward**\n", "\n", "For more information about the certification process, check this section ๐Ÿ‘‰ https://huggingface.co/deep-rl-course/en/unit0/introduction#certification-process" ] }, { "cell_type": "markdown", "metadata": { "id": "HqzznTzhNfAC", "jp-MarkdownHeadingCollapsed": true }, "source": [ "## Set the GPU ๐Ÿ’ช\n", "\n", "- To **accelerate the agent's training, we'll use a GPU**. To do that, go to `Runtime > Change Runtime type`\n", "\n", "\"GPU" ] }, { "cell_type": "markdown", "metadata": { "id": "38HBd3t1SHJ8" }, "source": [ "- `Hardware Accelerator > GPU`\n", "\n", "\"GPU" ] }, { "cell_type": "markdown", "metadata": { "id": "jeDAH0h0EBiG" }, "source": [ "## Install dependencies and create a virtual screen ๐Ÿ”ฝ\n", "\n", "The first step is to install the dependencies, weโ€™ll install multiple ones.\n", "\n", "- `gymnasium[box2d]`: Contains the LunarLander-v2 environment ๐ŸŒ›\n", "- `stable-baselines3[extra]`: The deep reinforcement learning library.\n", "- `huggingface_sb3`: Additional code for Stable-baselines3 to load and upload models from the Hugging Face ๐Ÿค— Hub.\n", "\n", "To make things easier, we created a script to install all these dependencies." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "id": "yQIGLPDkGhgG" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[sudo] password for anish: \n" ] }, { "ename": "KeyboardInterrupt", "evalue": "", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", "File \u001b[0;32m~/mambaforge/lib/python3.10/site-packages/IPython/utils/_process_posix.py:153\u001b[0m, in \u001b[0;36mProcessHandler.system\u001b[0;34m(self, cmd)\u001b[0m\n\u001b[1;32m 150\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[1;32m 151\u001b[0m \u001b[38;5;66;03m# res is the index of the pattern that caused the match, so we\u001b[39;00m\n\u001b[1;32m 152\u001b[0m \u001b[38;5;66;03m# know whether we've finished (if we matched EOF) or not\u001b[39;00m\n\u001b[0;32m--> 153\u001b[0m res_idx \u001b[38;5;241m=\u001b[39m \u001b[43mchild\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexpect_list\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpatterns\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mread_timeout\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 154\u001b[0m \u001b[38;5;28mprint\u001b[39m(child\u001b[38;5;241m.\u001b[39mbefore[out_size:]\u001b[38;5;241m.\u001b[39mdecode(enc, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mreplace\u001b[39m\u001b[38;5;124m'\u001b[39m), end\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m'\u001b[39m)\n", "File \u001b[0;32m~/mambaforge/lib/python3.10/site-packages/pexpect/spawnbase.py:372\u001b[0m, in \u001b[0;36mSpawnBase.expect_list\u001b[0;34m(self, pattern_list, timeout, searchwindowsize, async_, **kw)\u001b[0m\n\u001b[1;32m 371\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 372\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mexp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexpect_loop\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtimeout\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m~/mambaforge/lib/python3.10/site-packages/pexpect/expect.py:169\u001b[0m, in \u001b[0;36mExpecter.expect_loop\u001b[0;34m(self, timeout)\u001b[0m\n\u001b[1;32m 168\u001b[0m \u001b[38;5;66;03m# Still have time left, so read more data\u001b[39;00m\n\u001b[0;32m--> 169\u001b[0m incoming \u001b[38;5;241m=\u001b[39m \u001b[43mspawn\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mread_nonblocking\u001b[49m\u001b[43m(\u001b[49m\u001b[43mspawn\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmaxread\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 170\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mspawn\u001b[38;5;241m.\u001b[39mdelayafterread \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n", "File \u001b[0;32m~/mambaforge/lib/python3.10/site-packages/pexpect/pty_spawn.py:500\u001b[0m, in \u001b[0;36mspawn.read_nonblocking\u001b[0;34m(self, size, timeout)\u001b[0m\n\u001b[1;32m 497\u001b[0m \u001b[38;5;66;03m# Because of the select(0) check above, we know that no data\u001b[39;00m\n\u001b[1;32m 498\u001b[0m \u001b[38;5;66;03m# is available right now. But if a non-zero timeout is given\u001b[39;00m\n\u001b[1;32m 499\u001b[0m \u001b[38;5;66;03m# (possibly timeout=None), we call select() with a timeout.\u001b[39;00m\n\u001b[0;32m--> 500\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m (timeout \u001b[38;5;241m!=\u001b[39m \u001b[38;5;241m0\u001b[39m) \u001b[38;5;129;01mand\u001b[39;00m \u001b[43mselect\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtimeout\u001b[49m\u001b[43m)\u001b[49m:\n\u001b[1;32m 501\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28msuper\u001b[39m(spawn, \u001b[38;5;28mself\u001b[39m)\u001b[38;5;241m.\u001b[39mread_nonblocking(size)\n", "File \u001b[0;32m~/mambaforge/lib/python3.10/site-packages/pexpect/pty_spawn.py:450\u001b[0m, in \u001b[0;36mspawn.read_nonblocking..select\u001b[0;34m(timeout)\u001b[0m\n\u001b[1;32m 449\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mselect\u001b[39m(timeout):\n\u001b[0;32m--> 450\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mselect_ignore_interrupts\u001b[49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mchild_fd\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[43m)\u001b[49m[\u001b[38;5;241m0\u001b[39m]\n", "File \u001b[0;32m~/mambaforge/lib/python3.10/site-packages/pexpect/utils.py:143\u001b[0m, in \u001b[0;36mselect_ignore_interrupts\u001b[0;34m(iwtd, owtd, ewtd, timeout)\u001b[0m\n\u001b[1;32m 142\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 143\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mselect\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mselect\u001b[49m\u001b[43m(\u001b[49m\u001b[43miwtd\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mowtd\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mewtd\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 144\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mInterruptedError\u001b[39;00m:\n", "\u001b[0;31mKeyboardInterrupt\u001b[0m: ", "\nDuring handling of the above exception, another exception occurred:\n", "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[5], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mget_ipython\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msystem\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43msudo apt install swig cmake\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m~/mambaforge/lib/python3.10/site-packages/ipykernel/zmqshell.py:657\u001b[0m, in \u001b[0;36mZMQInteractiveShell.system_piped\u001b[0;34m(self, cmd)\u001b[0m\n\u001b[1;32m 655\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39muser_ns[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_exit_code\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m system(cmd)\n\u001b[1;32m 656\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 657\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39muser_ns[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_exit_code\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[43msystem\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvar_expand\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcmd\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdepth\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m~/mambaforge/lib/python3.10/site-packages/IPython/utils/_process_posix.py:177\u001b[0m, in \u001b[0;36mProcessHandler.system\u001b[0;34m(self, cmd)\u001b[0m\n\u001b[1;32m 174\u001b[0m \u001b[38;5;28;01mpass\u001b[39;00m\n\u001b[1;32m 175\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[1;32m 176\u001b[0m \u001b[38;5;66;03m# Ensure the subprocess really is terminated\u001b[39;00m\n\u001b[0;32m--> 177\u001b[0m \u001b[43mchild\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mterminate\u001b[49m\u001b[43m(\u001b[49m\u001b[43mforce\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m\n\u001b[1;32m 178\u001b[0m \u001b[38;5;66;03m# add isalive check, to ensure exitstatus is set:\u001b[39;00m\n\u001b[1;32m 179\u001b[0m child\u001b[38;5;241m.\u001b[39misalive()\n", "File \u001b[0;32m~/mambaforge/lib/python3.10/site-packages/pexpect/pty_spawn.py:650\u001b[0m, in \u001b[0;36mspawn.terminate\u001b[0;34m(self, force)\u001b[0m\n\u001b[1;32m 648\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[1;32m 649\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mkill(signal\u001b[38;5;241m.\u001b[39mSIGINT)\n\u001b[0;32m--> 650\u001b[0m \u001b[43mtime\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msleep\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdelayafterterminate\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 651\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39misalive():\n\u001b[1;32m 652\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m\n", "\u001b[0;31mKeyboardInterrupt\u001b[0m: " ] } ], "source": [ "!sudo apt install swig cmake" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "id": "9XaULfDZDvrC" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Collecting stable-baselines3==2.0.0a5 (from -r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1))\n", " Downloading stable_baselines3-2.0.0a5-py3-none-any.whl.metadata (5.3 kB)\n", "Collecting swig (from -r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 2))\n", " Downloading swig-4.3.0-py2.py3-none-manylinux_2_5_x86_64.manylinux1_x86_64.whl.metadata (3.5 kB)\n", "Collecting huggingface_sb3 (from -r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 4))\n", " Downloading huggingface_sb3-3.0-py3-none-any.whl.metadata (6.3 kB)\n", "Collecting gymnasium[box2d] (from -r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 3))\n", " Downloading gymnasium-1.0.0-py3-none-any.whl.metadata (9.5 kB)\n", "Collecting gymnasium==0.28.1 (from stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1))\n", " Downloading gymnasium-0.28.1-py3-none-any.whl.metadata (9.2 kB)\n", "Requirement already satisfied: numpy in /home/anish/mambaforge/lib/python3.10/site-packages (from stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1)) (1.23.5)\n", "Requirement already satisfied: torch>=1.11 in /home/anish/mambaforge/lib/python3.10/site-packages (from stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1)) (2.1.1)\n", "Requirement already satisfied: cloudpickle in /home/anish/mambaforge/lib/python3.10/site-packages (from stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1)) (2.2.1)\n", "Requirement already satisfied: pandas in /home/anish/mambaforge/lib/python3.10/site-packages (from stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1)) (1.5.2)\n", "Requirement already satisfied: matplotlib in /home/anish/mambaforge/lib/python3.10/site-packages (from stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1)) (3.5.3)\n", "Collecting jax-jumpy>=1.0.0 (from gymnasium==0.28.1->stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1))\n", " Downloading jax_jumpy-1.0.0-py3-none-any.whl.metadata (15 kB)\n", "Requirement already satisfied: typing-extensions>=4.3.0 in /home/anish/mambaforge/lib/python3.10/site-packages (from gymnasium==0.28.1->stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1)) (4.9.0)\n", "Collecting farama-notifications>=0.0.1 (from gymnasium==0.28.1->stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1))\n", " Downloading Farama_Notifications-0.0.4-py3-none-any.whl.metadata (558 bytes)\n", "INFO: pip is looking at multiple versions of gymnasium[box2d] to determine which version is compatible with other requirements. This could take a while.\n", "Collecting gymnasium[box2d] (from -r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 3))\n", " Downloading gymnasium-0.29.1-py3-none-any.whl.metadata (10 kB)\n", " Downloading gymnasium-0.29.0-py3-none-any.whl.metadata (10 kB)\n", "Collecting box2d-py==2.3.5 (from gymnasium[box2d]->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 3))\n", " Downloading box2d-py-2.3.5.tar.gz (374 kB)\n", " Preparing metadata (setup.py) ... \u001b[?25ldone\n", "\u001b[?25hCollecting pygame==2.1.3 (from gymnasium[box2d]->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 3))\n", " Downloading pygame-2.1.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (9.3 kB)\n", "Requirement already satisfied: huggingface-hub~=0.8 in /home/anish/mambaforge/lib/python3.10/site-packages (from huggingface_sb3->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 4)) (0.20.3)\n", "Requirement already satisfied: pyyaml~=6.0 in /home/anish/mambaforge/lib/python3.10/site-packages (from huggingface_sb3->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 4)) (6.0.1)\n", "Requirement already satisfied: wasabi in /home/anish/mambaforge/lib/python3.10/site-packages (from huggingface_sb3->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 4)) (0.10.1)\n", "Requirement already satisfied: filelock in /home/anish/mambaforge/lib/python3.10/site-packages (from huggingface-hub~=0.8->huggingface_sb3->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 4)) (3.13.1)\n", "Collecting fsspec>=2023.5.0 (from huggingface-hub~=0.8->huggingface_sb3->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 4))\n", " Downloading fsspec-2025.2.0-py3-none-any.whl.metadata (11 kB)\n", "Requirement already satisfied: requests in /home/anish/mambaforge/lib/python3.10/site-packages (from huggingface-hub~=0.8->huggingface_sb3->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 4)) (2.31.0)\n", "Requirement already satisfied: tqdm>=4.42.1 in /home/anish/mambaforge/lib/python3.10/site-packages (from huggingface-hub~=0.8->huggingface_sb3->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 4)) (4.65.0)\n", "Requirement already satisfied: packaging>=20.9 in /home/anish/mambaforge/lib/python3.10/site-packages (from huggingface-hub~=0.8->huggingface_sb3->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 4)) (24.0)\n", "Requirement already satisfied: sympy in /home/anish/mambaforge/lib/python3.10/site-packages (from torch>=1.11->stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1)) (1.12)\n", "Requirement already satisfied: networkx in /home/anish/mambaforge/lib/python3.10/site-packages (from torch>=1.11->stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1)) (2.6.3)\n", "Requirement already satisfied: jinja2 in /home/anish/mambaforge/lib/python3.10/site-packages (from torch>=1.11->stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1)) (3.1.3)\n", "Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.1.105 in /home/anish/mambaforge/lib/python3.10/site-packages (from torch>=1.11->stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1)) (12.1.105)\n", "Requirement already satisfied: nvidia-cuda-runtime-cu12==12.1.105 in /home/anish/mambaforge/lib/python3.10/site-packages (from torch>=1.11->stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1)) (12.1.105)\n", "Requirement already satisfied: nvidia-cuda-cupti-cu12==12.1.105 in /home/anish/mambaforge/lib/python3.10/site-packages (from torch>=1.11->stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1)) (12.1.105)\n", "Requirement already satisfied: nvidia-cudnn-cu12==8.9.2.26 in /home/anish/mambaforge/lib/python3.10/site-packages (from torch>=1.11->stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1)) (8.9.2.26)\n", "Requirement already satisfied: nvidia-cublas-cu12==12.1.3.1 in /home/anish/mambaforge/lib/python3.10/site-packages (from torch>=1.11->stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1)) (12.1.3.1)\n", "Requirement already satisfied: nvidia-cufft-cu12==11.0.2.54 in /home/anish/mambaforge/lib/python3.10/site-packages (from torch>=1.11->stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1)) (11.0.2.54)\n", "Requirement already satisfied: nvidia-curand-cu12==10.3.2.106 in /home/anish/mambaforge/lib/python3.10/site-packages (from torch>=1.11->stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1)) (10.3.2.106)\n", "Requirement already satisfied: nvidia-cusolver-cu12==11.4.5.107 in /home/anish/mambaforge/lib/python3.10/site-packages (from torch>=1.11->stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1)) (11.4.5.107)\n", "Requirement already satisfied: nvidia-cusparse-cu12==12.1.0.106 in /home/anish/mambaforge/lib/python3.10/site-packages (from torch>=1.11->stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1)) (12.1.0.106)\n", "Requirement already satisfied: nvidia-nccl-cu12==2.18.1 in /home/anish/mambaforge/lib/python3.10/site-packages (from torch>=1.11->stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1)) (2.18.1)\n", "Requirement already satisfied: nvidia-nvtx-cu12==12.1.105 in /home/anish/mambaforge/lib/python3.10/site-packages (from torch>=1.11->stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1)) (12.1.105)\n", "Requirement already satisfied: triton==2.1.0 in /home/anish/mambaforge/lib/python3.10/site-packages (from torch>=1.11->stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1)) (2.1.0)\n", "Requirement already satisfied: nvidia-nvjitlink-cu12 in /home/anish/mambaforge/lib/python3.10/site-packages (from nvidia-cusolver-cu12==11.4.5.107->torch>=1.11->stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1)) (12.3.101)\n", "Requirement already satisfied: cycler>=0.10 in /home/anish/mambaforge/lib/python3.10/site-packages (from matplotlib->stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1)) (0.11.0)\n", "Requirement already satisfied: fonttools>=4.22.0 in /home/anish/mambaforge/lib/python3.10/site-packages (from matplotlib->stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1)) (4.25.0)\n", "Requirement already satisfied: kiwisolver>=1.0.1 in /home/anish/mambaforge/lib/python3.10/site-packages (from matplotlib->stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1)) (1.4.4)\n", "Requirement already satisfied: pillow>=6.2.0 in /home/anish/mambaforge/lib/python3.10/site-packages (from matplotlib->stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1)) (10.0.1)\n", "Requirement already satisfied: pyparsing>=2.2.1 in /home/anish/mambaforge/lib/python3.10/site-packages (from matplotlib->stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1)) (3.0.9)\n", "Requirement already satisfied: python-dateutil>=2.7 in /home/anish/mambaforge/lib/python3.10/site-packages (from matplotlib->stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1)) (2.8.2)\n", "Requirement already satisfied: pytz>=2020.1 in /home/anish/mambaforge/lib/python3.10/site-packages (from pandas->stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1)) (2022.6)\n", "Requirement already satisfied: six>=1.5 in /home/anish/mambaforge/lib/python3.10/site-packages (from python-dateutil>=2.7->matplotlib->stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1)) (1.16.0)\n", "Requirement already satisfied: MarkupSafe>=2.0 in /home/anish/mambaforge/lib/python3.10/site-packages (from jinja2->torch>=1.11->stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1)) (2.1.3)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /home/anish/mambaforge/lib/python3.10/site-packages (from requests->huggingface-hub~=0.8->huggingface_sb3->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 4)) (2.0.4)\n", "Requirement already satisfied: idna<4,>=2.5 in /home/anish/mambaforge/lib/python3.10/site-packages (from requests->huggingface-hub~=0.8->huggingface_sb3->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 4)) (3.4)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in /home/anish/mambaforge/lib/python3.10/site-packages (from requests->huggingface-hub~=0.8->huggingface_sb3->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 4)) (1.26.18)\n", "Requirement already satisfied: certifi>=2017.4.17 in /home/anish/mambaforge/lib/python3.10/site-packages (from requests->huggingface-hub~=0.8->huggingface_sb3->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 4)) (2024.12.14)\n", "Requirement already satisfied: mpmath>=0.19 in /home/anish/mambaforge/lib/python3.10/site-packages (from sympy->torch>=1.11->stable-baselines3==2.0.0a5->-r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt (line 1)) (1.3.0)\n", "Downloading stable_baselines3-2.0.0a5-py3-none-any.whl (177 kB)\n", "Downloading gymnasium-0.28.1-py3-none-any.whl (925 kB)\n", "\u001b[2K \u001b[90mโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”\u001b[0m \u001b[32m925.5/925.5 kB\u001b[0m \u001b[31m6.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "Downloading swig-4.3.0-py2.py3-none-manylinux_2_5_x86_64.manylinux1_x86_64.whl (1.9 MB)\n", "\u001b[2K \u001b[90mโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”\u001b[0m \u001b[32m1.9/1.9 MB\u001b[0m \u001b[31m10.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "Downloading pygame-2.1.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (13.7 MB)\n", "\u001b[2K \u001b[90mโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”\u001b[0m \u001b[32m13.7/13.7 MB\u001b[0m \u001b[31m10.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n", "Downloading huggingface_sb3-3.0-py3-none-any.whl (9.7 kB)\n", "Downloading Farama_Notifications-0.0.4-py3-none-any.whl (2.5 kB)\n", "Downloading jax_jumpy-1.0.0-py3-none-any.whl (20 kB)\n", "Downloading fsspec-2025.2.0-py3-none-any.whl (184 kB)\n", "Building wheels for collected packages: box2d-py\n", "done\n", "\u001b[?25h Created wheel for box2d-py: filename=box2d_py-2.3.5-cp310-cp310-linux_x86_64.whl size=494418 sha256=9330fe9cfaa51efd57e0f94d0f069a07fe0bb002222968c11173f0166bce1f70\n", " Stored in directory: /home/anish/.cache/pip/wheels/db/8f/6a/eaaadf056fba10a98d986f6dce954e6201ba3126926fc5ad9e\n", "Successfully built box2d-py\n", "Installing collected packages: swig, farama-notifications, box2d-py, pygame, jax-jumpy, fsspec, gymnasium, huggingface_sb3, stable-baselines3\n", " Attempting uninstall: fsspec\n", " Found existing installation: fsspec 2022.11.0\n", " Uninstalling fsspec-2022.11.0:\n", " Successfully uninstalled fsspec-2022.11.0\n", "\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n", "datasets 2.17.0 requires fsspec[http]<=2023.10.0,>=2023.1.0, but you have fsspec 2025.2.0 which is incompatible.\n", "rasa 3.6.18 requires attrs<22.2,>=19.3, but you have attrs 23.2.0 which is incompatible.\n", "rasa 3.6.18 requires jsonschema<4.18,>=3.2, but you have jsonschema 4.20.0 which is incompatible.\n", "rasa 3.6.18 requires packaging<21.0,>=20.0, but you have packaging 24.0 which is incompatible.\u001b[0m\u001b[31m\n", "\u001b[0mSuccessfully installed box2d-py-2.3.5 farama-notifications-0.0.4 fsspec-2023.10.0 gymnasium-0.28.1 huggingface_sb3-3.0 jax-jumpy-1.0.0 pygame-2.1.3 stable-baselines3-2.0.0a5 swig-4.3.0\n", "\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m25.0\u001b[0m\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n" ] } ], "source": [ "!pip install -r https://raw.githubusercontent.com/huggingface/deep-rl-class/main/notebooks/unit1/requirements-unit1.txt" ] }, { "cell_type": "markdown", "metadata": { "id": "BEKeXQJsQCYm" }, "source": [ "During the notebook, we'll need to generate a replay video. To do so, with colab, **we need to have a virtual screen to be able to render the environment** (and thus record the frames).\n", "\n", "Hence the following cell will install virtual screen libraries and create and run a virtual screen ๐Ÿ–ฅ" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "id": "j5f2cGkdP-mb" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[sudo] password for anish: \n", "[sudo] password for anish: \n", "\u001b[1;31mE: \u001b[0mCould not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)\u001b[0m\n", "\u001b[1;31mE: \u001b[0mUnable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?\u001b[0m\n", "\u001b[1;31mE: \u001b[0mCould not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)\u001b[0m\n", "\u001b[1;31mE: \u001b[0mUnable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?\u001b[0m\n", "Collecting pyvirtualdisplay\n", " Downloading PyVirtualDisplay-3.0-py3-none-any.whl.metadata (943 bytes)\n", "Downloading PyVirtualDisplay-3.0-py3-none-any.whl (15 kB)\n", "Installing collected packages: pyvirtualdisplay\n", "Successfully installed pyvirtualdisplay-3.0\n", "\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m25.0\u001b[0m\n", "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n" ] } ], "source": [ "!sudo apt-get update\n", "!sudo apt-get install -y python3-opengl\n", "!apt install ffmpeg\n", "!apt install xvfb\n", "!pip3 install pyvirtualdisplay" ] }, { "cell_type": "markdown", "metadata": { "id": "TCwBTAwAW9JJ" }, "source": [ "To make sure the new installed libraries are used, **sometimes it's required to restart the notebook runtime**. The next cell will force the **runtime to crash, so you'll need to connect again and run the code starting from here**. Thanks to this trick, **we will be able to run our virtual screen.**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "cYvkbef7XEMi" }, "outputs": [], "source": [ "import os\n", "os.kill(os.getpid(), 9)\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "id": "BE5JWP5rQIKf" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Virtual display should be active on DISPLAY = :100\n" ] } ], "source": [ "# # Virtual display\n", "# import os\n", "# from pyvirtualdisplay import Display\n", "\n", "# os.environ[\"DISPLAY\"] = \":100\"\n", "# virtual_display = Display(visible=0, size=(1400, 900))\n", "# virtual_display.start()\n", "# print(\"Virtual display started with display number:\", virtual_display.display)\n", "\n", "import os\n", "from pyvirtualdisplay import Display\n", "\n", "os.environ[\"DISPLAY\"] = \":100\" # Ensure DISPLAY is set\n", "virtual_display = Display(visible=0, size=(1400, 900), backend=\"xvfb\", use_xauth=False)\n", "print(\"Virtual display should be active on DISPLAY =\", os.environ[\"DISPLAY\"])\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "wrgpVFqyENVf" }, "source": [ "## Import the packages ๐Ÿ“ฆ\n", "\n", "One additional library we import is huggingface_hub **to be able to upload and download trained models from the hub**.\n", "\n", "\n", "The Hugging Face Hub ๐Ÿค— works as a central place where anyone can share and explore models and datasets. It has versioning, metrics, visualizations and other features that will allow you to easily collaborate with others.\n", "\n", "You can see here all the Deep reinforcement Learning models available here๐Ÿ‘‰ https://huggingface.co/models?pipeline_tag=reinforcement-learning&sort=downloads\n", "\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Num GPUs Available: 1\n" ] } ], "source": [ "import tensorflow as tf\n", "print(\"Num GPUs Available:\", len(tf.config.list_physical_devices('GPU')))\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "id": "cygWLPGsEQ0m" }, "outputs": [], "source": [ "import gymnasium\n", "\n", "from huggingface_sb3 import load_from_hub, package_to_hub\n", "from huggingface_hub import notebook_login # To log to our Hugging Face account to be able to upload models to the Hub.\n", "\n", "from stable_baselines3 import PPO\n", "from stable_baselines3.common.env_util import make_vec_env\n", "from stable_baselines3.common.evaluation import evaluate_policy\n", "from stable_baselines3.common.monitor import Monitor" ] }, { "cell_type": "markdown", "metadata": { "id": "MRqRuRUl8CsB" }, "source": [ "## Understand Gymnasium and how it works ๐Ÿค–\n", "\n", "๐Ÿ‹ The library containing our environment is called Gymnasium.\n", "**You'll use Gymnasium a lot in Deep Reinforcement Learning.**\n", "\n", "Gymnasium is the **new version of Gym library** [maintained by the Farama Foundation](https://farama.org/).\n", "\n", "The Gymnasium library provides two things:\n", "\n", "- An interface that allows you to **create RL environments**.\n", "- A **collection of environments** (gym-control, atari, box2D...).\n", "\n", "Let's look at an example, but first let's recall the RL loop.\n", "\n", "\"The" ] }, { "cell_type": "markdown", "metadata": { "id": "-TzNN0bQ_j-3" }, "source": [ "At each step:\n", "- Our Agent receivesย a **state (S0)**ย from theย **Environment**ย โ€” we receive the first frame of our game (Environment).\n", "- Based on thatย **state (S0),**ย the Agent takes anย **action (A0)**ย โ€” our Agent will move to the right.\n", "- The environment transitions to aย **new**ย **state (S1)**ย โ€” new frame.\n", "- The environment gives someย **reward (R1)**ย to the Agent โ€” weโ€™re not deadย *(Positive Reward +1)*.\n", "\n", "\n", "With Gymnasium:\n", "\n", "1๏ธโƒฃ We create our environment using `gymnasium.make()`\n", "\n", "2๏ธโƒฃ We reset the environment to its initial state with `observation = env.reset()`\n", "\n", "At each step:\n", "\n", "3๏ธโƒฃ Get an action using our model (in our example we take a random action)\n", "\n", "4๏ธโƒฃ Using `env.step(action)`, we perform this action in the environment and get\n", "- `observation`: The new state (st+1)\n", "- `reward`: The reward we get after executing the action\n", "- `terminated`: Indicates if the episode terminated (agent reach the terminal state)\n", "- `truncated`: Introduced with this new version, it indicates a timelimit or if an agent go out of bounds of the environment for instance.\n", "- `info`: A dictionary that provides additional information (depends on the environment).\n", "\n", "For more explanations check this ๐Ÿ‘‰ https://gymnasium.farama.org/api/env/#gymnasium.Env.step\n", "\n", "If the episode is terminated:\n", "- We reset the environment to its initial state with `observation = env.reset()`\n", "\n", "**Let's look at an example!** Make sure to read the code\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "id": "w7vOFlpA_ONz" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Action taken: 1\n", "Action taken: 2\n", "Action taken: 2\n", "Action taken: 2\n", "Action taken: 3\n", "Action taken: 2\n", "Action taken: 3\n", "Action taken: 0\n", "Action taken: 1\n", "Action taken: 2\n", "Action taken: 2\n", "Action taken: 2\n", "Action taken: 2\n", "Action taken: 1\n", "Action taken: 3\n", "Action taken: 0\n", "Action taken: 3\n", "Action taken: 3\n", "Action taken: 3\n", "Action taken: 3\n" ] } ], "source": [ "import gymnasium as gym\n", "\n", "# First, we create our environment called LunarLander-v2\n", "env = gym.make(\"LunarLander-v2\")\n", "\n", "# Then we reset this environment\n", "observation, info = env.reset()\n", "\n", "for _ in range(20):\n", " # Take a random action\n", " action = env.action_space.sample()\n", " print(\"Action taken:\", action)\n", "\n", " # Do this action in the environment and get\n", " # next_state, reward, terminated, truncated and info\n", " observation, reward, terminated, truncated, info = env.step(action)\n", "\n", " # If the game is terminated (in our case we land, crashed) or truncated (timeout)\n", " if terminated or truncated:\n", " # Reset the environment\n", " print(\"Environment is reset\")\n", " observation, info = env.reset()\n", "\n", "env.close()" ] }, { "cell_type": "markdown", "metadata": { "id": "XIrKGGSlENZB" }, "source": [ "## Create the LunarLander environment ๐ŸŒ› and understand how it works\n", "\n", "### [The environment ๐ŸŽฎ](https://gymnasium.farama.org/environments/box2d/lunar_lander/)\n", "\n", "In this first tutorial, weโ€™re going to train our agent, a [Lunar Lander](https://gymnasium.farama.org/environments/box2d/lunar_lander/), **to land correctly on the moon**. To do that, the agent needs to learn **to adapt its speed and position (horizontal, vertical, and angular) to land correctly.**\n", "\n", "---\n", "\n", "\n", "๐Ÿ’ก A good habit when you start to use an environment is to check its documentation\n", "\n", "๐Ÿ‘‰ https://gymnasium.farama.org/environments/box2d/lunar_lander/\n", "\n", "---\n" ] }, { "cell_type": "markdown", "metadata": { "id": "poLBgRocF9aT" }, "source": [ "Let's see what the Environment looks like:\n" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "id": "ZNPG0g_UGCfh" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "_____OBSERVATION SPACE_____ \n", "\n", "Observation Space Shape (8,)\n", "Sample observation [-49.60593 -16.963318 -2.1011531 -1.9610301 3.0569177\n", " -1.3220655 0.42197073 0.931666 ]\n" ] } ], "source": [ "# We create our environment with gym.make(\"\")\n", "env = gym.make(\"LunarLander-v2\")\n", "env.reset()\n", "print(\"_____OBSERVATION SPACE_____ \\n\")\n", "print(\"Observation Space Shape\", env.observation_space.shape)\n", "print(\"Sample observation\", env.observation_space.sample()) # Get a random observation" ] }, { "cell_type": "markdown", "metadata": { "id": "2MXc15qFE0M9" }, "source": [ "We see with `Observation Space Shape (8,)` that the observation is a vector of size 8, where each value contains different information about the lander:\n", "- Horizontal pad coordinate (x)\n", "- Vertical pad coordinate (y)\n", "- Horizontal speed (x)\n", "- Vertical speed (y)\n", "- Angle\n", "- Angular speed\n", "- If the left leg contact point has touched the land (boolean)\n", "- If the right leg contact point has touched the land (boolean)\n" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "id": "We5WqOBGLoSm" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", " _____ACTION SPACE_____ \n", "\n", "Action Space Shape 4\n", "Action Space Sample 3\n" ] } ], "source": [ "print(\"\\n _____ACTION SPACE_____ \\n\")\n", "print(\"Action Space Shape\", env.action_space.n)\n", "print(\"Action Space Sample\", env.action_space.sample()) # Take a random action" ] }, { "cell_type": "markdown", "metadata": { "id": "MyxXwkI2Magx" }, "source": [ "The action space (the set of possible actions the agent can take) is discrete with 4 actions available ๐ŸŽฎ:\n", "\n", "- Action 0: Do nothing,\n", "- Action 1: Fire left orientation engine,\n", "- Action 2: Fire the main engine,\n", "- Action 3: Fire right orientation engine.\n", "\n", "Reward function (the function that will give a reward at each timestep) ๐Ÿ’ฐ:\n", "\n", "After every step a reward is granted. The total reward of an episode is the **sum of the rewards for all the steps within that episode**.\n", "\n", "For each step, the reward:\n", "\n", "- Is increased/decreased the closer/further the lander is to the landing pad.\n", "- Is increased/decreased the slower/faster the lander is moving.\n", "- Is decreased the more the lander is tilted (angle not horizontal).\n", "- Is increased by 10 points for each leg that is in contact with the ground.\n", "- Is decreased by 0.03 points each frame a side engine is firing.\n", "- Is decreased by 0.3 points each frame the main engine is firing.\n", "\n", "The episode receive an **additional reward of -100 or +100 points for crashing or landing safely respectively.**\n", "\n", "An episode is **considered a solution if it scores at least 200 points.**" ] }, { "cell_type": "markdown", "metadata": { "id": "dFD9RAFjG8aq" }, "source": [ "#### Vectorized Environment\n", "\n", "- We create a vectorized environment (a method for stacking multiple independent environments into a single environment) of 16 environments, this way, **we'll have more diverse experiences during the training.**" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "id": "99hqQ_etEy1N" }, "outputs": [], "source": [ "# Create the environment\n", "env = make_vec_env('LunarLander-v2', n_envs=16)" ] }, { "cell_type": "markdown", "metadata": { "id": "VgrE86r5E5IK" }, "source": [ "## Create the Model ๐Ÿค–\n", "- We have studied our environment and we understood the problem: **being able to land the Lunar Lander to the Landing Pad correctly by controlling left, right and main orientation engine**. Now let's build the algorithm we're going to use to solve this Problem ๐Ÿš€.\n", "\n", "- To do so, we're going to use our first Deep RL library, [Stable Baselines3 (SB3)](https://stable-baselines3.readthedocs.io/en/master/).\n", "\n", "- SB3 is a set of **reliable implementations of reinforcement learning algorithms in PyTorch**.\n", "\n", "---\n", "\n", "๐Ÿ’ก A good habit when using a new library is to dive first on the documentation: https://stable-baselines3.readthedocs.io/en/master/ and then try some tutorials.\n", "\n", "----" ] }, { "cell_type": "markdown", "metadata": { "id": "HLlClRW37Q7e" }, "source": [ "\"Stable" ] }, { "cell_type": "markdown", "metadata": { "id": "HV4yiUM_9_Ka" }, "source": [ "To solve this problem, we're going to use SB3 **PPO**. [PPO (aka Proximal Policy Optimization) is one of the SOTA (state of the art) Deep Reinforcement Learning algorithms that you'll study during this course](https://stable-baselines3.readthedocs.io/en/master/modules/ppo.html#example%5D).\n", "\n", "PPO is a combination of:\n", "- *Value-based reinforcement learning method*: learning an action-value function that will tell us the **most valuable action to take given a state and action**.\n", "- *Policy-based reinforcement learning method*: learning a policy that will **give us a probability distribution over actions**." ] }, { "cell_type": "markdown", "metadata": { "id": "5qL_4HeIOrEJ" }, "source": [ "Stable-Baselines3 is easy to set up:\n", "\n", "1๏ธโƒฃ You **create your environment** (in our case it was done above)\n", "\n", "2๏ธโƒฃ You define the **model you want to use and instantiate this model** `model = PPO(\"MlpPolicy\")`\n", "\n", "3๏ธโƒฃ You **train the agent** with `model.learn` and define the number of training timesteps\n", "\n", "```\n", "# Create environment\n", "env = gym.make('LunarLander-v2')\n", "\n", "# Instantiate the agent\n", "model = PPO('MlpPolicy', env, verbose=1)\n", "# Train the agent\n", "model.learn(total_timesteps=int(2e5))\n", "```\n", "\n" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "id": "nxI6hT1GE4-A" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Using cuda device\n" ] } ], "source": [ "# TODO: Define a PPO MlpPolicy architecture\n", "# We use MultiLayerPerceptron (MLPPolicy) because the input is a vector,\n", "# if we had frames as input we would use CnnPolicy\n", "from stable_baselines3 import PPO\n", "model = PPO(\n", " policy = 'MlpPolicy',\n", " env = env,\n", " n_steps = 1024,\n", " batch_size = 64,\n", " n_epochs = 4,\n", " gamma = 0.999,\n", " gae_lambda = 0.98,\n", " ent_coef = 0.01,\n", " verbose=1)" ] }, { "cell_type": "markdown", "metadata": { "id": "ClJJk88yoBUi" }, "source": [ "## Train the PPO agent ๐Ÿƒ\n", "- Let's train our agent for 1,000,000 timesteps, don't forget to use GPU on Colab. It will take approximately ~20min, but you can use fewer timesteps if you just want to try it out.\n", "- During the training, take a โ˜• break you deserved it ๐Ÿค—" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "id": "qKnYkNiVp89p" }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "895ff2a2530c4733a93a515753b43e60", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Output()" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "---------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 90.9 |\n", "| ep_rew_mean | -171 |\n", "| time/ | |\n", "| fps | 2274 |\n", "| iterations | 1 |\n", "| time_elapsed | 7 |\n", "| total_timesteps | 16384 |\n", "---------------------------------\n", "-----------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 97 |\n", "| ep_rew_mean | -161 |\n", "| time/ | |\n", "| fps | 1516 |\n", "| iterations | 2 |\n", "| time_elapsed | 21 |\n", "| total_timesteps | 32768 |\n", "| train/ | |\n", "| approx_kl | 0.007830694 |\n", "| clip_fraction | 0.055 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -1.38 |\n", "| explained_variance | 0.00203 |\n", "| learning_rate | 0.0003 |\n", "| loss | 979 |\n", "| n_updates | 4 |\n", "| policy_gradient_loss | -0.00488 |\n", "| value_loss | 4.76e+03 |\n", "-----------------------------------------\n", "-----------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 92.3 |\n", "| ep_rew_mean | -129 |\n", "| time/ | |\n", "| fps | 1430 |\n", "| iterations | 3 |\n", "| time_elapsed | 34 |\n", "| total_timesteps | 49152 |\n", "| train/ | |\n", "| approx_kl | 0.006937025 |\n", "| clip_fraction | 0.0372 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -1.37 |\n", "| explained_variance | 0.00521 |\n", "| learning_rate | 0.0003 |\n", "| loss | 731 |\n", "| n_updates | 8 |\n", "| policy_gradient_loss | -0.00521 |\n", "| value_loss | 2.53e+03 |\n", "-----------------------------------------\n", "-----------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 97.8 |\n", "| ep_rew_mean | -117 |\n", "| time/ | |\n", "| fps | 1387 |\n", "| iterations | 4 |\n", "| time_elapsed | 47 |\n", "| total_timesteps | 65536 |\n", "| train/ | |\n", "| approx_kl | 0.006378214 |\n", "| clip_fraction | 0.0363 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -1.35 |\n", "| explained_variance | -0.00173 |\n", "| learning_rate | 0.0003 |\n", "| loss | 442 |\n", "| n_updates | 12 |\n", "| policy_gradient_loss | -0.00376 |\n", "| value_loss | 1.4e+03 |\n", "-----------------------------------------\n", "-----------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 100 |\n", "| ep_rew_mean | -110 |\n", "| time/ | |\n", "| fps | 1345 |\n", "| iterations | 5 |\n", "| time_elapsed | 60 |\n", "| total_timesteps | 81920 |\n", "| train/ | |\n", "| approx_kl | 0.009244712 |\n", "| clip_fraction | 0.112 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -1.33 |\n", "| explained_variance | 5e-05 |\n", "| learning_rate | 0.0003 |\n", "| loss | 370 |\n", "| n_updates | 16 |\n", "| policy_gradient_loss | -0.00714 |\n", "| value_loss | 799 |\n", "-----------------------------------------\n", "-----------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 117 |\n", "| ep_rew_mean | -105 |\n", "| time/ | |\n", "| fps | 1304 |\n", "| iterations | 6 |\n", "| time_elapsed | 75 |\n", "| total_timesteps | 98304 |\n", "| train/ | |\n", "| approx_kl | 0.010476859 |\n", "| clip_fraction | 0.0797 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -1.3 |\n", "| explained_variance | -0.0287 |\n", "| learning_rate | 0.0003 |\n", "| loss | 396 |\n", "| n_updates | 20 |\n", "| policy_gradient_loss | -0.00315 |\n", "| value_loss | 852 |\n", "-----------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 124 |\n", "| ep_rew_mean | -101 |\n", "| time/ | |\n", "| fps | 1285 |\n", "| iterations | 7 |\n", "| time_elapsed | 89 |\n", "| total_timesteps | 114688 |\n", "| train/ | |\n", "| approx_kl | 0.0065559074 |\n", "| clip_fraction | 0.0269 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -1.29 |\n", "| explained_variance | 0.0153 |\n", "| learning_rate | 0.0003 |\n", "| loss | 465 |\n", "| n_updates | 24 |\n", "| policy_gradient_loss | -0.00461 |\n", "| value_loss | 1e+03 |\n", "------------------------------------------\n", "-----------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 111 |\n", "| ep_rew_mean | -74.6 |\n", "| time/ | |\n", "| fps | 1277 |\n", "| iterations | 8 |\n", "| time_elapsed | 102 |\n", "| total_timesteps | 131072 |\n", "| train/ | |\n", "| approx_kl | 0.009337346 |\n", "| clip_fraction | 0.0635 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -1.26 |\n", "| explained_variance | 0.0381 |\n", "| learning_rate | 0.0003 |\n", "| loss | 392 |\n", "| n_updates | 28 |\n", "| policy_gradient_loss | -0.00487 |\n", "| value_loss | 901 |\n", "-----------------------------------------\n", "----------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 123 |\n", "| ep_rew_mean | -67.6 |\n", "| time/ | |\n", "| fps | 1267 |\n", "| iterations | 9 |\n", "| time_elapsed | 116 |\n", "| total_timesteps | 147456 |\n", "| train/ | |\n", "| approx_kl | 0.01065027 |\n", "| clip_fraction | 0.0845 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -1.23 |\n", "| explained_variance | -0.0372 |\n", "| learning_rate | 0.0003 |\n", "| loss | 286 |\n", "| n_updates | 32 |\n", "| policy_gradient_loss | -0.00288 |\n", "| value_loss | 564 |\n", "----------------------------------------\n", "-----------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 124 |\n", "| ep_rew_mean | -45.7 |\n", "| time/ | |\n", "| fps | 1254 |\n", "| iterations | 10 |\n", "| time_elapsed | 130 |\n", "| total_timesteps | 163840 |\n", "| train/ | |\n", "| approx_kl | 0.006434204 |\n", "| clip_fraction | 0.0828 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -1.2 |\n", "| explained_variance | 0.000335 |\n", "| learning_rate | 0.0003 |\n", "| loss | 264 |\n", "| n_updates | 36 |\n", "| policy_gradient_loss | -0.00519 |\n", "| value_loss | 573 |\n", "-----------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 149 |\n", "| ep_rew_mean | -29.3 |\n", "| time/ | |\n", "| fps | 1232 |\n", "| iterations | 11 |\n", "| time_elapsed | 146 |\n", "| total_timesteps | 180224 |\n", "| train/ | |\n", "| approx_kl | 0.0085010715 |\n", "| clip_fraction | 0.0377 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -1.19 |\n", "| explained_variance | -0.0956 |\n", "| learning_rate | 0.0003 |\n", "| loss | 430 |\n", "| n_updates | 40 |\n", "| policy_gradient_loss | -0.00147 |\n", "| value_loss | 549 |\n", "------------------------------------------\n", "-----------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 177 |\n", "| ep_rew_mean | -29.8 |\n", "| time/ | |\n", "| fps | 1197 |\n", "| iterations | 12 |\n", "| time_elapsed | 164 |\n", "| total_timesteps | 196608 |\n", "| train/ | |\n", "| approx_kl | 0.007712485 |\n", "| clip_fraction | 0.0596 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -1.16 |\n", "| explained_variance | -0.00129 |\n", "| learning_rate | 0.0003 |\n", "| loss | 517 |\n", "| n_updates | 44 |\n", "| policy_gradient_loss | -0.00302 |\n", "| value_loss | 571 |\n", "-----------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 159 |\n", "| ep_rew_mean | 1.27 |\n", "| time/ | |\n", "| fps | 1178 |\n", "| iterations | 13 |\n", "| time_elapsed | 180 |\n", "| total_timesteps | 212992 |\n", "| train/ | |\n", "| approx_kl | 0.0051239775 |\n", "| clip_fraction | 0.0405 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -1.17 |\n", "| explained_variance | 3.46e-05 |\n", "| learning_rate | 0.0003 |\n", "| loss | 222 |\n", "| n_updates | 48 |\n", "| policy_gradient_loss | -0.00148 |\n", "| value_loss | 586 |\n", "------------------------------------------\n", "-----------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 192 |\n", "| ep_rew_mean | 1.6 |\n", "| time/ | |\n", "| fps | 1148 |\n", "| iterations | 14 |\n", "| time_elapsed | 199 |\n", "| total_timesteps | 229376 |\n", "| train/ | |\n", "| approx_kl | 0.005512827 |\n", "| clip_fraction | 0.0323 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -1.13 |\n", "| explained_variance | -0.000203 |\n", "| learning_rate | 0.0003 |\n", "| loss | 224 |\n", "| n_updates | 52 |\n", "| policy_gradient_loss | -0.00172 |\n", "| value_loss | 596 |\n", "-----------------------------------------\n", "-----------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 200 |\n", "| ep_rew_mean | 7.8 |\n", "| time/ | |\n", "| fps | 1118 |\n", "| iterations | 15 |\n", "| time_elapsed | 219 |\n", "| total_timesteps | 245760 |\n", "| train/ | |\n", "| approx_kl | 0.008812543 |\n", "| clip_fraction | 0.0672 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -1.14 |\n", "| explained_variance | 8.73e-05 |\n", "| learning_rate | 0.0003 |\n", "| loss | 410 |\n", "| n_updates | 56 |\n", "| policy_gradient_loss | -0.00222 |\n", "| value_loss | 592 |\n", "-----------------------------------------\n", "----------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 256 |\n", "| ep_rew_mean | 9.12 |\n", "| time/ | |\n", "| fps | 1073 |\n", "| iterations | 16 |\n", "| time_elapsed | 244 |\n", "| total_timesteps | 262144 |\n", "| train/ | |\n", "| approx_kl | 0.00626533 |\n", "| clip_fraction | 0.0394 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -1.16 |\n", "| explained_variance | -0.000533 |\n", "| learning_rate | 0.0003 |\n", "| loss | 393 |\n", "| n_updates | 60 |\n", "| policy_gradient_loss | -0.00155 |\n", "| value_loss | 741 |\n", "----------------------------------------\n", "-----------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 360 |\n", "| ep_rew_mean | 15.4 |\n", "| time/ | |\n", "| fps | 1043 |\n", "| iterations | 17 |\n", "| time_elapsed | 266 |\n", "| total_timesteps | 278528 |\n", "| train/ | |\n", "| approx_kl | 0.007680665 |\n", "| clip_fraction | 0.0641 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -1.2 |\n", "| explained_variance | -0.00375 |\n", "| learning_rate | 0.0003 |\n", "| loss | 154 |\n", "| n_updates | 64 |\n", "| policy_gradient_loss | -0.00189 |\n", "| value_loss | 508 |\n", "-----------------------------------------\n", "-----------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 470 |\n", "| ep_rew_mean | 25.4 |\n", "| time/ | |\n", "| fps | 1011 |\n", "| iterations | 18 |\n", "| time_elapsed | 291 |\n", "| total_timesteps | 294912 |\n", "| train/ | |\n", "| approx_kl | 0.004360679 |\n", "| clip_fraction | 0.0112 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -1.2 |\n", "| explained_variance | 0.361 |\n", "| learning_rate | 0.0003 |\n", "| loss | 165 |\n", "| n_updates | 68 |\n", "| policy_gradient_loss | -0.00186 |\n", "| value_loss | 270 |\n", "-----------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 525 |\n", "| ep_rew_mean | 31.5 |\n", "| time/ | |\n", "| fps | 988 |\n", "| iterations | 19 |\n", "| time_elapsed | 314 |\n", "| total_timesteps | 311296 |\n", "| train/ | |\n", "| approx_kl | 0.0046085017 |\n", "| clip_fraction | 0.0248 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -1.14 |\n", "| explained_variance | 0.494 |\n", "| learning_rate | 0.0003 |\n", "| loss | 73.8 |\n", "| n_updates | 72 |\n", "| policy_gradient_loss | -0.00267 |\n", "| value_loss | 315 |\n", "------------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 558 |\n", "| ep_rew_mean | 32.8 |\n", "| time/ | |\n", "| fps | 969 |\n", "| iterations | 20 |\n", "| time_elapsed | 337 |\n", "| total_timesteps | 327680 |\n", "| train/ | |\n", "| approx_kl | 0.0048538707 |\n", "| clip_fraction | 0.0248 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -1.12 |\n", "| explained_variance | 0.568 |\n", "| learning_rate | 0.0003 |\n", "| loss | 222 |\n", "| n_updates | 76 |\n", "| policy_gradient_loss | -0.00171 |\n", "| value_loss | 359 |\n", "------------------------------------------\n", "-----------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 586 |\n", "| ep_rew_mean | 43.2 |\n", "| time/ | |\n", "| fps | 947 |\n", "| iterations | 21 |\n", "| time_elapsed | 363 |\n", "| total_timesteps | 344064 |\n", "| train/ | |\n", "| approx_kl | 0.004662386 |\n", "| clip_fraction | 0.023 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -1.15 |\n", "| explained_variance | 0.613 |\n", "| learning_rate | 0.0003 |\n", "| loss | 172 |\n", "| n_updates | 80 |\n", "| policy_gradient_loss | -0.00132 |\n", "| value_loss | 404 |\n", "-----------------------------------------\n", "----------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 647 |\n", "| ep_rew_mean | 52.8 |\n", "| time/ | |\n", "| fps | 931 |\n", "| iterations | 22 |\n", "| time_elapsed | 386 |\n", "| total_timesteps | 360448 |\n", "| train/ | |\n", "| approx_kl | 0.00829421 |\n", "| clip_fraction | 0.054 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -1.17 |\n", "| explained_variance | 0.824 |\n", "| learning_rate | 0.0003 |\n", "| loss | 97 |\n", "| n_updates | 84 |\n", "| policy_gradient_loss | -0.00189 |\n", "| value_loss | 148 |\n", "----------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 703 |\n", "| ep_rew_mean | 61.1 |\n", "| time/ | |\n", "| fps | 913 |\n", "| iterations | 23 |\n", "| time_elapsed | 412 |\n", "| total_timesteps | 376832 |\n", "| train/ | |\n", "| approx_kl | 0.0056812884 |\n", "| clip_fraction | 0.0351 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -1.16 |\n", "| explained_variance | 0.835 |\n", "| learning_rate | 0.0003 |\n", "| loss | 35.4 |\n", "| n_updates | 88 |\n", "| policy_gradient_loss | -0.00193 |\n", "| value_loss | 160 |\n", "------------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 739 |\n", "| ep_rew_mean | 71 |\n", "| time/ | |\n", "| fps | 898 |\n", "| iterations | 24 |\n", "| time_elapsed | 437 |\n", "| total_timesteps | 393216 |\n", "| train/ | |\n", "| approx_kl | 0.0036020793 |\n", "| clip_fraction | 0.0341 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -1.12 |\n", "| explained_variance | 0.872 |\n", "| learning_rate | 0.0003 |\n", "| loss | 24.5 |\n", "| n_updates | 92 |\n", "| policy_gradient_loss | -0.00145 |\n", "| value_loss | 143 |\n", "------------------------------------------\n", "-----------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 809 |\n", "| ep_rew_mean | 83.2 |\n", "| time/ | |\n", "| fps | 885 |\n", "| iterations | 25 |\n", "| time_elapsed | 462 |\n", "| total_timesteps | 409600 |\n", "| train/ | |\n", "| approx_kl | 0.005959125 |\n", "| clip_fraction | 0.0517 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -1.08 |\n", "| explained_variance | 0.921 |\n", "| learning_rate | 0.0003 |\n", "| loss | 13.8 |\n", "| n_updates | 96 |\n", "| policy_gradient_loss | -0.002 |\n", "| value_loss | 83.6 |\n", "-----------------------------------------\n", "-----------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 789 |\n", "| ep_rew_mean | 83.4 |\n", "| time/ | |\n", "| fps | 874 |\n", "| iterations | 26 |\n", "| time_elapsed | 486 |\n", "| total_timesteps | 425984 |\n", "| train/ | |\n", "| approx_kl | 0.004236764 |\n", "| clip_fraction | 0.0333 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -1.05 |\n", "| explained_variance | 0.929 |\n", "| learning_rate | 0.0003 |\n", "| loss | 95 |\n", "| n_updates | 100 |\n", "| policy_gradient_loss | -0.00171 |\n", "| value_loss | 73.2 |\n", "-----------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 807 |\n", "| ep_rew_mean | 88.1 |\n", "| time/ | |\n", "| fps | 865 |\n", "| iterations | 27 |\n", "| time_elapsed | 511 |\n", "| total_timesteps | 442368 |\n", "| train/ | |\n", "| approx_kl | 0.0058022556 |\n", "| clip_fraction | 0.0313 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -1.03 |\n", "| explained_variance | 0.84 |\n", "| learning_rate | 0.0003 |\n", "| loss | 111 |\n", "| n_updates | 104 |\n", "| policy_gradient_loss | -0.000718 |\n", "| value_loss | 193 |\n", "------------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 779 |\n", "| ep_rew_mean | 88.8 |\n", "| time/ | |\n", "| fps | 857 |\n", "| iterations | 28 |\n", "| time_elapsed | 534 |\n", "| total_timesteps | 458752 |\n", "| train/ | |\n", "| approx_kl | 0.0050881235 |\n", "| clip_fraction | 0.0381 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -1.1 |\n", "| explained_variance | 0.923 |\n", "| learning_rate | 0.0003 |\n", "| loss | 41.1 |\n", "| n_updates | 108 |\n", "| policy_gradient_loss | -0.000728 |\n", "| value_loss | 81.8 |\n", "------------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 778 |\n", "| ep_rew_mean | 89.3 |\n", "| time/ | |\n", "| fps | 849 |\n", "| iterations | 29 |\n", "| time_elapsed | 559 |\n", "| total_timesteps | 475136 |\n", "| train/ | |\n", "| approx_kl | 0.0027631787 |\n", "| clip_fraction | 0.0179 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -1.04 |\n", "| explained_variance | 0.885 |\n", "| learning_rate | 0.0003 |\n", "| loss | 75.8 |\n", "| n_updates | 112 |\n", "| policy_gradient_loss | -0.000397 |\n", "| value_loss | 143 |\n", "------------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 765 |\n", "| ep_rew_mean | 88.8 |\n", "| time/ | |\n", "| fps | 841 |\n", "| iterations | 30 |\n", "| time_elapsed | 584 |\n", "| total_timesteps | 491520 |\n", "| train/ | |\n", "| approx_kl | 0.0065794177 |\n", "| clip_fraction | 0.0367 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -1.01 |\n", "| explained_variance | 0.931 |\n", "| learning_rate | 0.0003 |\n", "| loss | 22.4 |\n", "| n_updates | 116 |\n", "| policy_gradient_loss | -0.000972 |\n", "| value_loss | 83.6 |\n", "------------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 802 |\n", "| ep_rew_mean | 96.7 |\n", "| time/ | |\n", "| fps | 835 |\n", "| iterations | 31 |\n", "| time_elapsed | 607 |\n", "| total_timesteps | 507904 |\n", "| train/ | |\n", "| approx_kl | 0.0058060028 |\n", "| clip_fraction | 0.0397 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -1.01 |\n", "| explained_variance | 0.94 |\n", "| learning_rate | 0.0003 |\n", "| loss | 6.68 |\n", "| n_updates | 120 |\n", "| policy_gradient_loss | -0.00186 |\n", "| value_loss | 54.2 |\n", "------------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 812 |\n", "| ep_rew_mean | 100 |\n", "| time/ | |\n", "| fps | 831 |\n", "| iterations | 32 |\n", "| time_elapsed | 630 |\n", "| total_timesteps | 524288 |\n", "| train/ | |\n", "| approx_kl | 0.0030073621 |\n", "| clip_fraction | 0.0269 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -0.998 |\n", "| explained_variance | 0.922 |\n", "| learning_rate | 0.0003 |\n", "| loss | 11.5 |\n", "| n_updates | 124 |\n", "| policy_gradient_loss | -0.000871 |\n", "| value_loss | 73 |\n", "------------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 855 |\n", "| ep_rew_mean | 106 |\n", "| time/ | |\n", "| fps | 825 |\n", "| iterations | 33 |\n", "| time_elapsed | 654 |\n", "| total_timesteps | 540672 |\n", "| train/ | |\n", "| approx_kl | 0.0035768435 |\n", "| clip_fraction | 0.0278 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -1.01 |\n", "| explained_variance | 0.933 |\n", "| learning_rate | 0.0003 |\n", "| loss | 9.32 |\n", "| n_updates | 128 |\n", "| policy_gradient_loss | -0.000894 |\n", "| value_loss | 71.7 |\n", "------------------------------------------\n", "----------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 885 |\n", "| ep_rew_mean | 117 |\n", "| time/ | |\n", "| fps | 820 |\n", "| iterations | 34 |\n", "| time_elapsed | 678 |\n", "| total_timesteps | 557056 |\n", "| train/ | |\n", "| approx_kl | 0.00547278 |\n", "| clip_fraction | 0.0505 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -0.954 |\n", "| explained_variance | 0.975 |\n", "| learning_rate | 0.0003 |\n", "| loss | 26.8 |\n", "| n_updates | 132 |\n", "| policy_gradient_loss | -0.00171 |\n", "| value_loss | 24.2 |\n", "----------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 914 |\n", "| ep_rew_mean | 123 |\n", "| time/ | |\n", "| fps | 816 |\n", "| iterations | 35 |\n", "| time_elapsed | 702 |\n", "| total_timesteps | 573440 |\n", "| train/ | |\n", "| approx_kl | 0.0034308662 |\n", "| clip_fraction | 0.0292 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -0.962 |\n", "| explained_variance | 0.939 |\n", "| learning_rate | 0.0003 |\n", "| loss | 14 |\n", "| n_updates | 136 |\n", "| policy_gradient_loss | -0.000466 |\n", "| value_loss | 63.5 |\n", "------------------------------------------\n", "----------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 930 |\n", "| ep_rew_mean | 128 |\n", "| time/ | |\n", "| fps | 812 |\n", "| iterations | 36 |\n", "| time_elapsed | 725 |\n", "| total_timesteps | 589824 |\n", "| train/ | |\n", "| approx_kl | 0.00409806 |\n", "| clip_fraction | 0.0437 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -0.929 |\n", "| explained_variance | 0.986 |\n", "| learning_rate | 0.0003 |\n", "| loss | 4.8 |\n", "| n_updates | 140 |\n", "| policy_gradient_loss | -0.00149 |\n", "| value_loss | 12.7 |\n", "----------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 911 |\n", "| ep_rew_mean | 130 |\n", "| time/ | |\n", "| fps | 810 |\n", "| iterations | 37 |\n", "| time_elapsed | 748 |\n", "| total_timesteps | 606208 |\n", "| train/ | |\n", "| approx_kl | 0.0041647265 |\n", "| clip_fraction | 0.0292 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -0.892 |\n", "| explained_variance | 0.984 |\n", "| learning_rate | 0.0003 |\n", "| loss | 2.15 |\n", "| n_updates | 144 |\n", "| policy_gradient_loss | -0.000187 |\n", "| value_loss | 17.9 |\n", "------------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 932 |\n", "| ep_rew_mean | 134 |\n", "| time/ | |\n", "| fps | 806 |\n", "| iterations | 38 |\n", "| time_elapsed | 772 |\n", "| total_timesteps | 622592 |\n", "| train/ | |\n", "| approx_kl | 0.0027398663 |\n", "| clip_fraction | 0.0234 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -0.927 |\n", "| explained_variance | 0.942 |\n", "| learning_rate | 0.0003 |\n", "| loss | 9.92 |\n", "| n_updates | 148 |\n", "| policy_gradient_loss | -0.000993 |\n", "| value_loss | 63.5 |\n", "------------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 915 |\n", "| ep_rew_mean | 134 |\n", "| time/ | |\n", "| fps | 802 |\n", "| iterations | 39 |\n", "| time_elapsed | 795 |\n", "| total_timesteps | 638976 |\n", "| train/ | |\n", "| approx_kl | 0.0041300924 |\n", "| clip_fraction | 0.0303 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -0.917 |\n", "| explained_variance | 0.965 |\n", "| learning_rate | 0.0003 |\n", "| loss | 7.98 |\n", "| n_updates | 152 |\n", "| policy_gradient_loss | -0.00166 |\n", "| value_loss | 44 |\n", "------------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 918 |\n", "| ep_rew_mean | 133 |\n", "| time/ | |\n", "| fps | 799 |\n", "| iterations | 40 |\n", "| time_elapsed | 819 |\n", "| total_timesteps | 655360 |\n", "| train/ | |\n", "| approx_kl | 0.0045867967 |\n", "| clip_fraction | 0.0252 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -0.94 |\n", "| explained_variance | 0.959 |\n", "| learning_rate | 0.0003 |\n", "| loss | 6.63 |\n", "| n_updates | 156 |\n", "| policy_gradient_loss | -0.000998 |\n", "| value_loss | 52 |\n", "------------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 918 |\n", "| ep_rew_mean | 135 |\n", "| time/ | |\n", "| fps | 796 |\n", "| iterations | 41 |\n", "| time_elapsed | 842 |\n", "| total_timesteps | 671744 |\n", "| train/ | |\n", "| approx_kl | 0.0073864306 |\n", "| clip_fraction | 0.0469 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -0.934 |\n", "| explained_variance | 0.98 |\n", "| learning_rate | 0.0003 |\n", "| loss | 39.5 |\n", "| n_updates | 160 |\n", "| policy_gradient_loss | -0.0013 |\n", "| value_loss | 25 |\n", "------------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 925 |\n", "| ep_rew_mean | 137 |\n", "| time/ | |\n", "| fps | 794 |\n", "| iterations | 42 |\n", "| time_elapsed | 866 |\n", "| total_timesteps | 688128 |\n", "| train/ | |\n", "| approx_kl | 0.0047421404 |\n", "| clip_fraction | 0.0533 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -0.938 |\n", "| explained_variance | 0.982 |\n", "| learning_rate | 0.0003 |\n", "| loss | 7.4 |\n", "| n_updates | 164 |\n", "| policy_gradient_loss | -0.00101 |\n", "| value_loss | 21.9 |\n", "------------------------------------------\n", "-----------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 922 |\n", "| ep_rew_mean | 136 |\n", "| time/ | |\n", "| fps | 791 |\n", "| iterations | 43 |\n", "| time_elapsed | 889 |\n", "| total_timesteps | 704512 |\n", "| train/ | |\n", "| approx_kl | 0.004400916 |\n", "| clip_fraction | 0.0448 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -0.923 |\n", "| explained_variance | 0.977 |\n", "| learning_rate | 0.0003 |\n", "| loss | 6.88 |\n", "| n_updates | 168 |\n", "| policy_gradient_loss | -0.000343 |\n", "| value_loss | 29 |\n", "-----------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 937 |\n", "| ep_rew_mean | 140 |\n", "| time/ | |\n", "| fps | 788 |\n", "| iterations | 44 |\n", "| time_elapsed | 914 |\n", "| total_timesteps | 720896 |\n", "| train/ | |\n", "| approx_kl | 0.0040651676 |\n", "| clip_fraction | 0.0306 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -0.918 |\n", "| explained_variance | 0.978 |\n", "| learning_rate | 0.0003 |\n", "| loss | 1.92 |\n", "| n_updates | 172 |\n", "| policy_gradient_loss | 0.000224 |\n", "| value_loss | 26.8 |\n", "------------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 954 |\n", "| ep_rew_mean | 145 |\n", "| time/ | |\n", "| fps | 786 |\n", "| iterations | 45 |\n", "| time_elapsed | 937 |\n", "| total_timesteps | 737280 |\n", "| train/ | |\n", "| approx_kl | 0.0040733675 |\n", "| clip_fraction | 0.0371 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -0.912 |\n", "| explained_variance | 0.994 |\n", "| learning_rate | 0.0003 |\n", "| loss | 2.51 |\n", "| n_updates | 176 |\n", "| policy_gradient_loss | -9.7e-05 |\n", "| value_loss | 6.48 |\n", "------------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 946 |\n", "| ep_rew_mean | 146 |\n", "| time/ | |\n", "| fps | 784 |\n", "| iterations | 46 |\n", "| time_elapsed | 960 |\n", "| total_timesteps | 753664 |\n", "| train/ | |\n", "| approx_kl | 0.0029262973 |\n", "| clip_fraction | 0.027 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -0.894 |\n", "| explained_variance | 0.988 |\n", "| learning_rate | 0.0003 |\n", "| loss | 4.87 |\n", "| n_updates | 180 |\n", "| policy_gradient_loss | -0.000259 |\n", "| value_loss | 15.2 |\n", "------------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 946 |\n", "| ep_rew_mean | 145 |\n", "| time/ | |\n", "| fps | 782 |\n", "| iterations | 47 |\n", "| time_elapsed | 983 |\n", "| total_timesteps | 770048 |\n", "| train/ | |\n", "| approx_kl | 0.0033976224 |\n", "| clip_fraction | 0.0199 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -0.913 |\n", "| explained_variance | 0.971 |\n", "| learning_rate | 0.0003 |\n", "| loss | 6.53 |\n", "| n_updates | 184 |\n", "| policy_gradient_loss | -6.21e-05 |\n", "| value_loss | 40.7 |\n", "------------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 962 |\n", "| ep_rew_mean | 149 |\n", "| time/ | |\n", "| fps | 780 |\n", "| iterations | 48 |\n", "| time_elapsed | 1007 |\n", "| total_timesteps | 786432 |\n", "| train/ | |\n", "| approx_kl | 0.0032964016 |\n", "| clip_fraction | 0.0263 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -0.892 |\n", "| explained_variance | 0.993 |\n", "| learning_rate | 0.0003 |\n", "| loss | 2.02 |\n", "| n_updates | 188 |\n", "| policy_gradient_loss | -0.00045 |\n", "| value_loss | 6.71 |\n", "------------------------------------------\n", "-----------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 970 |\n", "| ep_rew_mean | 147 |\n", "| time/ | |\n", "| fps | 778 |\n", "| iterations | 49 |\n", "| time_elapsed | 1030 |\n", "| total_timesteps | 802816 |\n", "| train/ | |\n", "| approx_kl | 0.004295407 |\n", "| clip_fraction | 0.0402 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -0.92 |\n", "| explained_variance | 0.997 |\n", "| learning_rate | 0.0003 |\n", "| loss | 1.02 |\n", "| n_updates | 192 |\n", "| policy_gradient_loss | -0.000568 |\n", "| value_loss | 3.34 |\n", "-----------------------------------------\n", "-----------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 960 |\n", "| ep_rew_mean | 149 |\n", "| time/ | |\n", "| fps | 777 |\n", "| iterations | 50 |\n", "| time_elapsed | 1053 |\n", "| total_timesteps | 819200 |\n", "| train/ | |\n", "| approx_kl | 0.004258155 |\n", "| clip_fraction | 0.0344 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -0.929 |\n", "| explained_variance | 0.996 |\n", "| learning_rate | 0.0003 |\n", "| loss | 0.942 |\n", "| n_updates | 196 |\n", "| policy_gradient_loss | -0.000289 |\n", "| value_loss | 4.16 |\n", "-----------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 952 |\n", "| ep_rew_mean | 147 |\n", "| time/ | |\n", "| fps | 774 |\n", "| iterations | 51 |\n", "| time_elapsed | 1078 |\n", "| total_timesteps | 835584 |\n", "| train/ | |\n", "| approx_kl | 0.0036211896 |\n", "| clip_fraction | 0.0177 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -0.864 |\n", "| explained_variance | 0.965 |\n", "| learning_rate | 0.0003 |\n", "| loss | 3.37 |\n", "| n_updates | 200 |\n", "| policy_gradient_loss | -0.00104 |\n", "| value_loss | 51 |\n", "------------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 974 |\n", "| ep_rew_mean | 151 |\n", "| time/ | |\n", "| fps | 772 |\n", "| iterations | 52 |\n", "| time_elapsed | 1103 |\n", "| total_timesteps | 851968 |\n", "| train/ | |\n", "| approx_kl | 0.0034328622 |\n", "| clip_fraction | 0.0345 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -0.846 |\n", "| explained_variance | 0.986 |\n", "| learning_rate | 0.0003 |\n", "| loss | 0.717 |\n", "| n_updates | 204 |\n", "| policy_gradient_loss | -0.000136 |\n", "| value_loss | 19 |\n", "------------------------------------------\n", "-----------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 974 |\n", "| ep_rew_mean | 152 |\n", "| time/ | |\n", "| fps | 771 |\n", "| iterations | 53 |\n", "| time_elapsed | 1126 |\n", "| total_timesteps | 868352 |\n", "| train/ | |\n", "| approx_kl | 0.004795182 |\n", "| clip_fraction | 0.0416 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -0.848 |\n", "| explained_variance | 0.985 |\n", "| learning_rate | 0.0003 |\n", "| loss | 2.46 |\n", "| n_updates | 208 |\n", "| policy_gradient_loss | -0.00101 |\n", "| value_loss | 22.8 |\n", "-----------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 957 |\n", "| ep_rew_mean | 156 |\n", "| time/ | |\n", "| fps | 769 |\n", "| iterations | 54 |\n", "| time_elapsed | 1149 |\n", "| total_timesteps | 884736 |\n", "| train/ | |\n", "| approx_kl | 0.0033361523 |\n", "| clip_fraction | 0.0448 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -0.843 |\n", "| explained_variance | 0.996 |\n", "| learning_rate | 0.0003 |\n", "| loss | 2.47 |\n", "| n_updates | 212 |\n", "| policy_gradient_loss | 3.24e-05 |\n", "| value_loss | 4.12 |\n", "------------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 954 |\n", "| ep_rew_mean | 160 |\n", "| time/ | |\n", "| fps | 769 |\n", "| iterations | 55 |\n", "| time_elapsed | 1171 |\n", "| total_timesteps | 901120 |\n", "| train/ | |\n", "| approx_kl | 0.0052519822 |\n", "| clip_fraction | 0.0315 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -0.802 |\n", "| explained_variance | 0.959 |\n", "| learning_rate | 0.0003 |\n", "| loss | 88.5 |\n", "| n_updates | 216 |\n", "| policy_gradient_loss | -0.00103 |\n", "| value_loss | 66.2 |\n", "------------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 964 |\n", "| ep_rew_mean | 161 |\n", "| time/ | |\n", "| fps | 767 |\n", "| iterations | 56 |\n", "| time_elapsed | 1194 |\n", "| total_timesteps | 917504 |\n", "| train/ | |\n", "| approx_kl | 0.0032495146 |\n", "| clip_fraction | 0.0366 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -0.8 |\n", "| explained_variance | 0.987 |\n", "| learning_rate | 0.0003 |\n", "| loss | 0.871 |\n", "| n_updates | 220 |\n", "| policy_gradient_loss | 0.000221 |\n", "| value_loss | 21.7 |\n", "------------------------------------------\n", "-----------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 916 |\n", "| ep_rew_mean | 177 |\n", "| time/ | |\n", "| fps | 767 |\n", "| iterations | 57 |\n", "| time_elapsed | 1216 |\n", "| total_timesteps | 933888 |\n", "| train/ | |\n", "| approx_kl | 0.003332762 |\n", "| clip_fraction | 0.0385 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -0.724 |\n", "| explained_variance | 0.988 |\n", "| learning_rate | 0.0003 |\n", "| loss | 5.36 |\n", "| n_updates | 224 |\n", "| policy_gradient_loss | -0.000567 |\n", "| value_loss | 18.4 |\n", "-----------------------------------------\n", "-----------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 816 |\n", "| ep_rew_mean | 189 |\n", "| time/ | |\n", "| fps | 768 |\n", "| iterations | 58 |\n", "| time_elapsed | 1237 |\n", "| total_timesteps | 950272 |\n", "| train/ | |\n", "| approx_kl | 0.006429377 |\n", "| clip_fraction | 0.0878 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -0.713 |\n", "| explained_variance | 0.896 |\n", "| learning_rate | 0.0003 |\n", "| loss | 172 |\n", "| n_updates | 228 |\n", "| policy_gradient_loss | -0.00454 |\n", "| value_loss | 213 |\n", "-----------------------------------------\n", "-----------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 640 |\n", "| ep_rew_mean | 208 |\n", "| time/ | |\n", "| fps | 769 |\n", "| iterations | 59 |\n", "| time_elapsed | 1256 |\n", "| total_timesteps | 966656 |\n", "| train/ | |\n", "| approx_kl | 0.005483403 |\n", "| clip_fraction | 0.0622 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -0.675 |\n", "| explained_variance | 0.853 |\n", "| learning_rate | 0.0003 |\n", "| loss | 176 |\n", "| n_updates | 232 |\n", "| policy_gradient_loss | -0.00389 |\n", "| value_loss | 278 |\n", "-----------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 471 |\n", "| ep_rew_mean | 231 |\n", "| time/ | |\n", "| fps | 770 |\n", "| iterations | 60 |\n", "| time_elapsed | 1275 |\n", "| total_timesteps | 983040 |\n", "| train/ | |\n", "| approx_kl | 0.0076946244 |\n", "| clip_fraction | 0.0568 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -0.676 |\n", "| explained_variance | 0.764 |\n", "| learning_rate | 0.0003 |\n", "| loss | 225 |\n", "| n_updates | 236 |\n", "| policy_gradient_loss | -0.00373 |\n", "| value_loss | 376 |\n", "------------------------------------------\n", "-----------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 390 |\n", "| ep_rew_mean | 241 |\n", "| time/ | |\n", "| fps | 771 |\n", "| iterations | 61 |\n", "| time_elapsed | 1294 |\n", "| total_timesteps | 999424 |\n", "| train/ | |\n", "| approx_kl | 0.008371659 |\n", "| clip_fraction | 0.0696 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -0.748 |\n", "| explained_variance | 0.79 |\n", "| learning_rate | 0.0003 |\n", "| loss | 112 |\n", "| n_updates | 240 |\n", "| policy_gradient_loss | -0.00196 |\n", "| value_loss | 257 |\n", "-----------------------------------------\n", "------------------------------------------\n", "| rollout/ | |\n", "| ep_len_mean | 363 |\n", "| ep_rew_mean | 250 |\n", "| time/ | |\n", "| fps | 774 |\n", "| iterations | 62 |\n", "| time_elapsed | 1312 |\n", "| total_timesteps | 1015808 |\n", "| train/ | |\n", "| approx_kl | 0.0058010696 |\n", "| clip_fraction | 0.0614 |\n", "| clip_range | 0.2 |\n", "| entropy_loss | -0.748 |\n", "| explained_variance | 0.88 |\n", "| learning_rate | 0.0003 |\n", "| loss | 185 |\n", "| n_updates | 244 |\n", "| policy_gradient_loss | -0.00189 |\n", "| value_loss | 156 |\n", "------------------------------------------\n" ] }, { "data": { "text/html": [ "
\n"
      ],
      "text/plain": []
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# TODO: Train it for 1,000,000 timesteps\n",
    "model.learn(total_timesteps=1000000, progress_bar=True)\n",
    "\n",
    "# TODO: Specify file name for model and save the model to file\n",
    "model_name = \"ppo-LunarLander-v2\"\n",
    "model.save(model_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "poBCy9u_csyR"
   },
   "outputs": [],
   "source": [
    "# SOLUTION\n",
    "# Train it for 1,000,000 timesteps\n",
    "model.learn(total_timesteps=1000000)\n",
    "# Save the model\n",
    "model_name = \"ppo-LunarLander-v2\"\n",
    "model.save(model_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "BY_HuedOoISR"
   },
   "source": [
    "## Evaluate the agent ๐Ÿ“ˆ\n",
    "- Remember to wrap the environment in a [Monitor](https://stable-baselines3.readthedocs.io/en/master/common/monitor.html).\n",
    "- Now that our Lunar Lander agent is trained ๐Ÿš€, we need to **check its performance**.\n",
    "- Stable-Baselines3 provides a method to do that: `evaluate_policy`.\n",
    "- To fill that part you need to [check the documentation](https://stable-baselines3.readthedocs.io/en/master/guide/examples.html#basic-usage-training-saving-loading)\n",
    "- In the next step,  we'll see **how to automatically evaluate and share your agent to compete in a leaderboard, but for now let's do it ourselves**\n",
    "\n",
    "\n",
    "๐Ÿ’ก When you evaluate your agent, you should not use your training environment but create an evaluation environment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "id": "yRpno0glsADy"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mean_reward=253.75 +/- 15.327396614932685\n"
     ]
    }
   ],
   "source": [
    "# TODO: Evaluate the agent\n",
    "# Create a new environment for evaluation\n",
    "from stable_baselines3.common.evaluation import evaluate_policy\n",
    "eval_env = Monitor(gym.make(\"LunarLander-v2\", render_mode='rgb_array'))\n",
    "\n",
    "# Evaluate the model with 10 evaluation episodes and deterministic=True\n",
    "mean_reward, std_reward = evaluate_policy(model, eval_env, n_eval_episodes=10, deterministic=True)\n",
    "\n",
    "# Print the results\n",
    "print(f\"mean_reward={mean_reward:.2f} +/- {std_reward}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "IK_kR78NoNb2"
   },
   "source": [
    "## Publish our trained model on the Hub ๐Ÿ”ฅ\n",
    "Now that we saw we got good results after the training, we can publish our trained model on the hub ๐Ÿค— with one line of code.\n",
    "\n",
    "๐Ÿ“š The libraries documentation ๐Ÿ‘‰ https://github.com/huggingface/huggingface_sb3/tree/main#hugging-face--x-stable-baselines3-v20\n",
    "\n",
    "Here's an example of a Model Card (with Space Invaders):"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Gs-Ew7e1gXN3"
   },
   "source": [
    "By using `package_to_hub` **you evaluate, record a replay, generate a model card of your agent and push it to the hub**.\n",
    "\n",
    "This way:\n",
    "- You can **showcase our work** ๐Ÿ”ฅ\n",
    "- You can **visualize your agent playing** ๐Ÿ‘€\n",
    "- You can **share with the community an agent that others can use** ๐Ÿ’พ\n",
    "- You can **access a leaderboard ๐Ÿ† to see how well your agent is performing compared to your classmates** ๐Ÿ‘‰ https://huggingface.co/spaces/huggingface-projects/Deep-Reinforcement-Learning-Leaderboard\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "JquRrWytA6eo"
   },
   "source": [
    "To be able to share your model with the community there are three more steps to follow:\n",
    "\n",
    "1๏ธโƒฃ (If it's not already done) create an account on Hugging Face โžก https://huggingface.co/join\n",
    "\n",
    "2๏ธโƒฃ Sign in and then, you need to store your authentication token from the Hugging Face website.\n",
    "- Create a new token (https://huggingface.co/settings/tokens) **with write role**\n",
    "\n",
    "\"Create\n",
    "\n",
    "- Copy the token\n",
    "- Run the cell below and paste the token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "id": "GZiFBBlzxzxY"
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a29d044cfb8047068f6ef5c5d1bc0436",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(HTML(value='
#0:0 (h264 (native) -> h264 (libx264))\n", "Press [q] to stop, [?] for help\n", "[libx264 @ 0x55fb4d5d6180] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2\n", "[libx264 @ 0x55fb4d5d6180] profile High, level 3.1, 4:2:0, 8-bit\n", "[libx264 @ 0x55fb4d5d6180] 264 - core 157 - H.264/MPEG-4 AVC codec - Copyleft 2003-2018 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00\n", "Output #0, mp4, to '/tmp/tmpuosqirzu/replay.mp4':\n", " Metadata:\n", " major_brand : isom\n", " minor_version : 512\n", " compatible_brands: isomiso2avc1mp41\n", " encoder : Lavf58.29.100\n", " Stream #0:0(und): Video: h264 (libx264) (avc1 / 0x31637661), yuv420p, 600x400, q=-1--1, 50 fps, 12800 tbn, 50 tbc (default)\n", " Metadata:\n", " handler_name : VideoHandler\n", " encoder : Lavc58.54.100 libx264\n", " Side data:\n", " cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "MoviePy - Done !\n", "MoviePy - video ready /tmp/tmpdeqcz7ep/-step-0-to-step-1000.mp4\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "frame= 1000 fps=929 q=-1.0 Lsize= 168kB time=00:00:19.94 bitrate= 69.2kbits/s speed=18.5x \n", "video:156kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 7.990535%\n", "[libx264 @ 0x55fb4d5d6180] frame I:5 Avg QP:10.96 size: 2008\n", "[libx264 @ 0x55fb4d5d6180] frame P:257 Avg QP:22.66 size: 215\n", "[libx264 @ 0x55fb4d5d6180] frame B:738 Avg QP:24.83 size: 127\n", "[libx264 @ 0x55fb4d5d6180] consecutive B-frames: 0.5% 2.2% 3.3% 94.0%\n", "[libx264 @ 0x55fb4d5d6180] mb I I16..4: 78.8% 14.8% 6.4%\n", "[libx264 @ 0x55fb4d5d6180] mb P I16..4: 0.2% 0.4% 0.1% P16..4: 1.8% 0.5% 0.2% 0.0% 0.0% skip:96.8%\n", "[libx264 @ 0x55fb4d5d6180] mb B I16..4: 0.0% 0.0% 0.1% B16..8: 2.7% 0.3% 0.0% direct: 0.1% skip:96.8% L0:55.1% L1:44.2% BI: 0.7%\n", "[libx264 @ 0x55fb4d5d6180] 8x8 transform intra:26.2% inter:17.9%\n", "[libx264 @ 0x55fb4d5d6180] coded y,uvDC,uvAC intra: 7.3% 11.9% 10.9% inter: 0.2% 0.3% 0.2%\n", "[libx264 @ 0x55fb4d5d6180] i16 v,h,dc,p: 85% 10% 5% 0%\n", "[libx264 @ 0x55fb4d5d6180] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 5% 23% 72% 0% 0% 0% 0% 0% 0%\n", "[libx264 @ 0x55fb4d5d6180] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 12% 16% 60% 2% 3% 1% 3% 1% 2%\n", "[libx264 @ 0x55fb4d5d6180] i8c dc,h,v,p: 92% 5% 3% 0%\n", "[libx264 @ 0x55fb4d5d6180] Weighted P-Frames: Y:0.0% UV:0.0%\n", "[libx264 @ 0x55fb4d5d6180] ref P L0: 64.8% 1.7% 22.1% 11.5%\n", "[libx264 @ 0x55fb4d5d6180] ref B L0: 66.2% 28.9% 4.9%\n", "[libx264 @ 0x55fb4d5d6180] ref B L1: 91.5% 8.5%\n", "[libx264 @ 0x55fb4d5d6180] kb/s:63.63\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[38;5;4mโ„น Pushing repo Anish13/ppo-LunarLander-v2 to the Hugging Face Hub\u001b[0m\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "b47221d0faae45ac99b682760486090e", "version_major": 2, "version_minor": 0 }, "text/plain": [ "ppo-LunarLander-v2.zip: 0%| | 0.00/148k [00:00" ] }, { "cell_type": "markdown", "metadata": { "id": "hNPLJF2bfiUw" }, "source": [ "2. Then we just need to use load_from_hub with:\n", "- The repo_id\n", "- The filename: the saved model inside the repo and its extension (*.zip)" ] }, { "cell_type": "markdown", "metadata": { "id": "bhb9-NtsinKB" }, "source": [ "Because the model I download from the Hub was trained with Gym (the former version of Gymnasium) we need to install shimmy a API conversion tool that will help us to run the environment correctly.\n", "\n", "Shimmy Documentation: https://github.com/Farama-Foundation/Shimmy" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "03WI-bkci1kH" }, "outputs": [], "source": [ "!pip install shimmy" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "oj8PSGHJfwz3" }, "outputs": [], "source": [ "from huggingface_sb3 import load_from_hub\n", "repo_id = \"Anish13/ppo-LunarLander-v2\" # The repo_id\n", "filename = \"ppo-LunarLander-v2.zip\" # The model filename.zip\n", "\n", "# When the model was trained on Python 3.8 the pickle protocol is 5\n", "# But Python 3.6, 3.7 use protocol 4\n", "# In order to get compatibility we need to:\n", "# 1. Install pickle5 (we done it at the beginning of the colab)\n", "# 2. Create a custom empty object we pass as parameter to PPO.load()\n", "custom_objects = {\n", " \"learning_rate\": 0.0,\n", " \"lr_schedule\": lambda _: 0.0,\n", " \"clip_range\": lambda _: 0.0,\n", "}\n", "\n", "checkpoint = load_from_hub(repo_id, filename)\n", "model = PPO.load(checkpoint, custom_objects=custom_objects, print_system_info=True)" ] }, { "cell_type": "markdown", "metadata": { "id": "Fs0Y-qgPgLUf" }, "source": [ "Let's evaluate this agent:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "PAEVwK-aahfx" }, "outputs": [], "source": [ "#@title\n", "eval_env = Monitor(gym.make(\"LunarLander-v2\"))\n", "mean_reward, std_reward = evaluate_policy(model, eval_env, n_eval_episodes=10, deterministic=True)\n", "print(f\"mean_reward={mean_reward:.2f} +/- {std_reward}\")" ] }, { "cell_type": "markdown", "metadata": { "id": "BQAwLnYFPk-s" }, "source": [ "## Some additional challenges ๐Ÿ†\n", "The best way to learn **is to try things by your own**! As you saw, the current agent is not doing great. As a first suggestion, you can train for more steps. With 1,000,000 steps, we saw some great results!\n", "\n", "In the [Leaderboard](https://huggingface.co/spaces/huggingface-projects/Deep-Reinforcement-Learning-Leaderboard) you will find your agents. Can you get to the top?\n", "\n", "Here are some ideas to achieve so:\n", "* Train more steps\n", "* Try different hyperparameters for `PPO`. You can see them at https://stable-baselines3.readthedocs.io/en/master/modules/ppo.html#parameters.\n", "* Check the [Stable-Baselines3 documentation](https://stable-baselines3.readthedocs.io/en/master/modules/dqn.html) and try another model such as DQN.\n", "* **Push your new trained model** on the Hub ๐Ÿ”ฅ\n", "\n", "**Compare the results of your LunarLander-v2 with your classmates** using the [leaderboard](https://huggingface.co/spaces/huggingface-projects/Deep-Reinforcement-Learning-Leaderboard) ๐Ÿ†\n", "\n", "Is moon landing too boring for you? Try to **change the environment**, why not use MountainCar-v0, CartPole-v1 or CarRacing-v0? Check how they work [using the gym documentation](https://www.gymlibrary.dev/) and have fun ๐ŸŽ‰." ] }, { "cell_type": "markdown", "metadata": { "id": "9lM95-dvmif8" }, "source": [ "________________________________________________________________________\n", "Congrats on finishing this chapter! That was the biggest one, **and there was a lot of information.**\n", "\n", "If youโ€™re still feel confused with all these elements...it's totally normal! **This was the same for me and for all people who studied RL.**\n", "\n", "Take time to really **grasp the material before continuing and try the additional challenges**. Itโ€™s important to master these elements and have a solid foundations.\n", "\n", "Naturally, during the course, weโ€™re going to dive deeper into these concepts but **itโ€™s better to have a good understanding of them now before diving into the next chapters.**\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "BjLhT70TEZIn" }, "source": [ "Next time, in the bonus unit 1, you'll train Huggy the Dog to fetch the stick.\n", "\n", "\"Huggy\"/\n", "\n", "## Keep learning, stay awesome ๐Ÿค—" ] } ], "metadata": { "accelerator": "GPU", "colab": { "collapsed_sections": [ "QAN7B0_HCVZC", "BqPKw3jt_pG5" ], "private_outputs": true, "provenance": [] }, "gpuClass": "standard", "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.6" }, "vscode": { "interpreter": { "hash": "ed7f8024e43d3b8f5ca3c5e1a8151ab4d136b3ecee1e3fd59e0766ccc55e1b10" } } }, "nbformat": 4, "nbformat_minor": 4 }