mirror of
https://github.com/huggingface/deep-rl-class.git
synced 2026-04-13 17:29:52 +08:00
Convert Huggy ipynb to mdx
This commit is contained in:
@@ -37,22 +37,12 @@
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"TODO: ADD TEXT LIVE INFO"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "QQnzAjfcIO2z"
|
||||
}
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"TODO: ADD IF YOU HAVE QUESTIONS\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"### The environment 🎮\n",
|
||||
"\n",
|
||||
"- Huggy the Dog, an environment created by [Thomas Simonini](https://twitter.com/ThomasSimonini) based on [Puppo The Corgi](https://blog.unity.com/technology/puppo-the-corgi-cuteness-overload-with-the-unity-ml-agents-toolkit)\n",
|
||||
"\n",
|
||||
"### The library used 📚\n",
|
||||
"\n",
|
||||
"- [MLAgents (Hugging Face version)](https://github.com/huggingface/ml-agents)"
|
||||
],
|
||||
"metadata": {
|
||||
@@ -72,7 +62,9 @@
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"## Objectives of this notebook 🏆\n",
|
||||
"\n",
|
||||
"At the end of the notebook, you will:\n",
|
||||
"\n",
|
||||
"- Understand **the state space, action space and reward function used to train Huggy**.\n",
|
||||
"- **Train your own Huggy** to fetch the stick.\n",
|
||||
"- Be able to play **with your trained Huggy directly in your browser**.\n",
|
||||
@@ -117,11 +109,12 @@
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"## Prerequisites 🏗️\n",
|
||||
"\n",
|
||||
"Before diving into the notebook, you need to:\n",
|
||||
"\n",
|
||||
"🔲 📚 **Develop an understanding of the foundations of Reinforcement learning** (MC, TD, Rewards hypothesis...) by doing Unit 1 👉 ADD LINK\n",
|
||||
"🔲 📚 **Develop an understanding of the foundations of Reinforcement learning** (MC, TD, Rewards hypothesis...) by doing Unit 1\n",
|
||||
"\n",
|
||||
"🔲 📚 **Read the introduction to Huggy** by doing Bonus Unit 1 👉 ADD LINK\n"
|
||||
"🔲 📚 **Read the introduction to Huggy** by doing Bonus Unit 1"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "6r7Hl0uywFSO"
|
||||
@@ -134,6 +127,7 @@
|
||||
},
|
||||
"source": [
|
||||
"## Clone the repository and install the dependencies 🔽\n",
|
||||
"\n",
|
||||
"- We need to clone the repository, that **contains the experimental version of the library that allows you to push your trained agent to the Hub.**"
|
||||
]
|
||||
},
|
||||
@@ -172,6 +166,7 @@
|
||||
},
|
||||
"source": [
|
||||
"## Download and move the environment zip file in `./trained-envs-executables/linux/`\n",
|
||||
"\n",
|
||||
"- Our environment executable is in a zip file.\n",
|
||||
"- We need to download it and place it to `./trained-envs-executables/linux/`"
|
||||
]
|
||||
@@ -244,9 +239,11 @@
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"## Let's recap how this environment works\n",
|
||||
"\n",
|
||||
"### The State Space: what Huggy \"perceives.\"\n",
|
||||
"\n",
|
||||
"Huggy doesn't \"see\" his environment. Instead, we provide him information about the environment:\n",
|
||||
"\n",
|
||||
"- The target (stick) position\n",
|
||||
"- The relative position between himself and the target\n",
|
||||
"- The orientation of his legs.\n",
|
||||
@@ -286,9 +283,8 @@
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"## Check the Huggy config file\n",
|
||||
"\n",
|
||||
"- In ML-Agents, you define the **training hyperparameters into config.yaml files.**\n",
|
||||
"For this first training, we’ll modify one thing:\n",
|
||||
"- The total training steps hyperparameter is too 2M that's enough for having very good results.\n",
|
||||
"\n",
|
||||
"- For the scope of this notebook, we're not going to modify the hyperparameters but if you want to try as an experimentation, you should also try to modify some other hyperparameters, Unity provides a very [good documentation explaining each of them here](https://github.com/Unity-Technologies/ml-agents/blob/main/docs/Training-Configuration-File.md)."
|
||||
],
|
||||
@@ -360,6 +356,7 @@
|
||||
},
|
||||
"source": [
|
||||
"## Push the agent to the 🤗 Hub\n",
|
||||
"\n",
|
||||
"- Now that we trained our agent, we’re **ready to push it to the Hub to be able to play with Huggy on your browser🔥.**"
|
||||
]
|
||||
},
|
||||
|
||||
256
notebooks/bonus-unit1/bonus-unit1.mdx
Normal file
256
notebooks/bonus-unit1/bonus-unit1.mdx
Normal file
@@ -0,0 +1,256 @@
|
||||
# Bonus Unit 1: Let's train Huggy the Dog 🐶 to fetch a stick
|
||||
|
||||
<img src="https://huggingface.co/datasets/huggingface-deep-rl-course/course-images/resolve/main/en/unit2/thumbnail.png" alt="Bonus Unit 1Thumbnail">
|
||||
|
||||
In this notebook, we'll reinforce what we learn in the first Unit by **teaching Huggy the Dog to fetch the stick and then play with it directly in your browser**
|
||||
|
||||
⬇️ Here is an example of what **you will achieve at the end of the unit.** ⬇️ (launch ▶ to see)
|
||||
|
||||
```python
|
||||
%%html
|
||||
<video controls autoplay><source src="https://huggingface.co/datasets/huggingface-deep-rl-course/course-images/resolve/main/en/notebooks/unit-bonus1/huggy.mp4" type="video/mp4"></video>
|
||||
```
|
||||
|
||||
### The environment 🎮
|
||||
|
||||
- Huggy the Dog, an environment created by [Thomas Simonini](https://twitter.com/ThomasSimonini) based on [Puppo The Corgi](https://blog.unity.com/technology/puppo-the-corgi-cuteness-overload-with-the-unity-ml-agents-toolkit)
|
||||
|
||||
### The library used 📚
|
||||
|
||||
- [MLAgents (Hugging Face version)](https://github.com/huggingface/ml-agents)
|
||||
|
||||
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).
|
||||
|
||||
## Objectives of this notebook 🏆
|
||||
|
||||
At the end of the notebook, you will:
|
||||
|
||||
- Understand **the state space, action space and reward function used to train Huggy**.
|
||||
- **Train your own Huggy** to fetch the stick.
|
||||
- Be able to play **with your trained Huggy directly in your browser**.
|
||||
|
||||
|
||||
|
||||
|
||||
## This notebook is from Deep Reinforcement Learning Course
|
||||
<img src="https://huggingface.co/datasets/huggingface-deep-rl-course/course-images/resolve/main/en/notebooks/deep-rl-course-illustration.jpg" alt="Deep RL Course illustration"/>
|
||||
|
||||
In this free course, you will:
|
||||
|
||||
- 📖 Study Deep Reinforcement Learning in **theory and practice**.
|
||||
- 🧑💻 Learn to **use famous Deep RL libraries** such as Stable Baselines3, RL Baselines3 Zoo, CleanRL and Sample Factory 2.0.
|
||||
- 🤖 Train **agents in unique environments**
|
||||
|
||||
And more check 📚 the syllabus 👉 https://simoninithomas.github.io/deep-rl-course
|
||||
|
||||
Don’t forget to **<a href="http://eepurl.com/ic5ZUD">sign up to the course</a>** (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).**
|
||||
|
||||
|
||||
The best way to keep in touch is to join our discord server to exchange with the community and with us 👉🏻 https://discord.gg/ydHrjt3WP5
|
||||
|
||||
## Prerequisites 🏗️
|
||||
|
||||
Before diving into the notebook, you need to:
|
||||
|
||||
🔲 📚 **Develop an understanding of the foundations of Reinforcement learning** (MC, TD, Rewards hypothesis...) by doing Unit 1
|
||||
|
||||
🔲 📚 **Read the introduction to Huggy** by doing Bonus Unit 1
|
||||
|
||||
## Clone the repository and install the dependencies 🔽
|
||||
|
||||
- We need to clone the repository, that **contains the experimental version of the library that allows you to push your trained agent to the Hub.**
|
||||
|
||||
```python
|
||||
%%capture
|
||||
# Clone this specific repository (can take 3min)
|
||||
!git clone https://github.com/huggingface/ml-agents/
|
||||
```
|
||||
|
||||
```python
|
||||
%%capture
|
||||
# Go inside the repository and install the package (can take 3min)
|
||||
%cd ml-agents
|
||||
!pip3 install -e ./ml-agents-envs
|
||||
!pip3 install -e ./ml-agents
|
||||
```
|
||||
|
||||
## Download and move the environment zip file in `./trained-envs-executables/linux/`
|
||||
|
||||
- Our environment executable is in a zip file.
|
||||
- We need to download it and place it to `./trained-envs-executables/linux/`
|
||||
|
||||
```python
|
||||
!mkdir ./trained-envs-executables
|
||||
!mkdir ./trained-envs-executables/linux
|
||||
```
|
||||
|
||||
```python
|
||||
!wget --load-cookies /tmp/cookies.txt "https://docs.google.com/uc?export=download&confirm=$(wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id=1zv3M95ZJTWHUVOWT6ckq_cm98nft8gdF' -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id=1zv3M95ZJTWHUVOWT6ckq_cm98nft8gdF" -O ./trained-envs-executables/linux/Huggy.zip && rm -rf /tmp/cookies.txt
|
||||
```
|
||||
|
||||
Download the file Huggy.zip from https://drive.google.com/uc?export=download&id=1zv3M95ZJTWHUVOWT6ckq_cm98nft8gdF using `wget`. Check out the full solution to download large files from GDrive [here](https://bcrf.biochem.wisc.edu/2021/02/05/download-google-drive-files-using-wget/)
|
||||
|
||||
```python
|
||||
%%capture
|
||||
!unzip -d ./trained-envs-executables/linux/ ./trained-envs-executables/linux/Huggy.zip
|
||||
```
|
||||
|
||||
Make sure your file is accessible
|
||||
|
||||
```python
|
||||
!chmod -R 755 ./trained-envs-executables/linux/Huggy
|
||||
```
|
||||
|
||||
## Let's recap how this environment works
|
||||
|
||||
### The State Space: what Huggy "perceives."
|
||||
|
||||
Huggy doesn't "see" his environment. Instead, we provide him information about the environment:
|
||||
|
||||
- The target (stick) position
|
||||
- The relative position between himself and the target
|
||||
- The orientation of his legs.
|
||||
|
||||
Given all this information, Huggy **can decide which action to take next to fulfill his goal**.
|
||||
|
||||
<img src="https://huggingface.co/datasets/huggingface-deep-rl-course/course-images/resolve/main/en/notebooks/unit-bonus1/huggy.jpg" alt="Huggy" width="100%">
|
||||
|
||||
|
||||
### The Action Space: what moves Huggy can do
|
||||
<img src="https://huggingface.co/datasets/huggingface-deep-rl-course/course-images/resolve/main/en/notebooks/unit-bonus1/huggy-action.jpg" alt="Huggy action" width="100%">
|
||||
|
||||
**Joint motors drive huggy legs**. It means that to get the target, Huggy needs to **learn to rotate the joint motors of each of his legs correctly so he can move**.
|
||||
|
||||
### The Reward Function
|
||||
|
||||
The reward function is designed so that **Huggy will fulfill his goal** : fetch the stick.
|
||||
|
||||
Remember that one of the foundations of Reinforcement Learning is the *reward hypothesis*: a goal can be described as the **maximization of the expected cumulative reward**.
|
||||
|
||||
Here, our goal is that Huggy **goes towards the stick but without spinning too much**. Hence, our reward function must translate this goal.
|
||||
|
||||
Our reward function:
|
||||
|
||||
<img src="https://huggingface.co/datasets/huggingface-deep-rl-course/course-images/resolve/main/en/notebooks/unit-bonus1/reward.jpg" alt="Huggy reward function" width="100%">
|
||||
|
||||
- *Orientation bonus*: we **reward him for getting close to the target**.
|
||||
- *Time penalty*: a fixed-time penalty given at every action to **force him to get to the stick as fast as possible**.
|
||||
- *Rotation penalty*: we penalize Huggy if **he spins too much and turns too quickly**.
|
||||
- *Getting to the target reward*: we reward Huggy for **reaching the target**.
|
||||
|
||||
## Check the Huggy config file
|
||||
|
||||
- In ML-Agents, you define the **training hyperparameters into config.yaml files.**
|
||||
|
||||
- For the scope of this notebook, we're not going to modify the hyperparameters but if you want to try as an experimentation, you should also try to modify some other hyperparameters, Unity provides a very [good documentation explaining each of them here](https://github.com/Unity-Technologies/ml-agents/blob/main/docs/Training-Configuration-File.md).
|
||||
|
||||
- Click here to open the config.yaml: `/content/ml-agents/config/ppo/Huggy.yaml`
|
||||
|
||||
|
||||
We’re now ready to train our agent 🔥.
|
||||
|
||||
## Train our agent
|
||||
|
||||
To train our agent, we just need to **launch mlagents-learn and select the executable containing the environment.**
|
||||
|
||||
<img src="https://huggingface.co/datasets/huggingface-deep-rl-course/course-images/resolve/main/en/notebooks/unit-bonus1/mllearn.png" alt="ml learn function" width="100%">
|
||||
|
||||
We define four parameters:
|
||||
|
||||
1. `mlagents-learn <config>`: the path where the hyperparameter config file is.
|
||||
2. `--env`: where the environment executable is.
|
||||
3. `--run_id`: the name you want to give to your training run id.
|
||||
4. `--no-graphics`: to not launch the visualization during the training.
|
||||
|
||||
Train the model and use the `--resume` flag to continue training in case of interruption.
|
||||
|
||||
> It will fail first time when you use `--resume`, try running the block again to bypass the error.
|
||||
|
||||
|
||||
|
||||
The training will take 30 to 45min depending on your machine, go take a ☕️you deserve it 🤗.
|
||||
|
||||
```python
|
||||
!mlagents-learn ./config/ppo/Huggy.yaml --env=./trained-envs-executables/linux/Huggy/Huggy --run-id="Huggy" --no-graphics
|
||||
```
|
||||
|
||||
## Push the agent to the 🤗 Hub
|
||||
|
||||
- Now that we trained our agent, we’re **ready to push it to the Hub to be able to play with Huggy on your browser🔥.**
|
||||
|
||||
To be able to share your model with the community there are three more steps to follow:
|
||||
|
||||
1️⃣ (If it's not already done) create an account to HF ➡ https://huggingface.co/join
|
||||
|
||||
2️⃣ Sign in and then, you need to store your authentication token from the Hugging Face website.
|
||||
- Create a new token (https://huggingface.co/settings/tokens) **with write role**
|
||||
|
||||
<img src="https://huggingface.co/datasets/huggingface-deep-rl-course/course-images/resolve/main/en/notebooks/create-token.jpg" alt="Create HF Token">
|
||||
|
||||
- Copy the token
|
||||
- Run the cell below and paste the token
|
||||
|
||||
```python
|
||||
from huggingface_hub import notebook_login
|
||||
|
||||
notebook_login()
|
||||
```
|
||||
|
||||
If you don't want to use a Google Colab or a Jupyter Notebook, you need to use this command instead: `huggingface-cli login`
|
||||
|
||||
Then, we simply need to run `mlagents-push-to-hf`.
|
||||
|
||||
<img src="https://huggingface.co/datasets/huggingface-deep-rl-course/course-images/resolve/main/en/notebooks/unit-bonus1/mlpush.png" alt="ml learn function" width="100%">
|
||||
|
||||
And we define 4 parameters:
|
||||
|
||||
1. `--run-id`: the name of the training run id.
|
||||
2. `--local-dir`: where the agent was saved, it’s results/<run_id name>, so in my case results/First Training.
|
||||
3. `--repo-id`: the name of the Hugging Face repo you want to create or update. It’s always <your huggingface username>/<the repo name>
|
||||
If the repo does not exist **it will be created automatically**
|
||||
4. `--commit-message`: since HF repos are git repository you need to define a commit message.
|
||||
|
||||
```python
|
||||
!mlagents-push-to-hf --run-id="HuggyTraining" --local-dir="./results/Huggy" --repo-id="ThomasSimonini/ppo-Huggy" --commit-message="Huggy"
|
||||
```
|
||||
|
||||
Else, if everything worked you should have this at the end of the process(but with a different url 😆) :
|
||||
|
||||
|
||||
|
||||
```
|
||||
Your model is pushed to the hub. You can view your model here: https://huggingface.co/ThomasSimonini/ppo-Huggy
|
||||
```
|
||||
|
||||
It’s the link to your model, it contains a model card that explains how to use it, your Tensorboard and your config file. **What’s awesome is that it’s a git repository, that means you can have different commits, update your repository with a new push etc.**
|
||||
|
||||
<img src="https://huggingface.co/datasets/huggingface-deep-rl-course/course-images/resolve/main/en/notebooks/unit-bonus1/modelcard.png" alt="ml learn function" width="100%">
|
||||
|
||||
But now comes the best: **being able to play with Huggy online 👀.**
|
||||
|
||||
## Play with your Huggy 🐕
|
||||
|
||||
For this step it’s simple:
|
||||
|
||||
- Open the game Huggy in your browser: https://huggingface.co/spaces/ThomasSimonini/Huggy
|
||||
|
||||
- Click on Play with my Huggy model
|
||||
|
||||
<img src="https://huggingface.co/datasets/huggingface-deep-rl-course/course-images/resolve/main/en/notebooks/unit-bonus1/load-huggy.jpg" alt="load-huggy" width="100%">
|
||||
|
||||
1. In step 1, choose your model repository which is the model id (in my case ThomasSimonini/ppo-Huggy).
|
||||
|
||||
2. In step 2, **choose what model you want to replay**:
|
||||
- I have multiple one, since we saved a model every 500000 timesteps.
|
||||
- But if I want the more recent I choose Huggy.onnx
|
||||
|
||||
👉 What’s nice **is to try with different models step to see the improvement of the agent.**
|
||||
|
||||
Congrats on finishing this bonus unit!
|
||||
|
||||
You can now sit and enjoy playing with your Huggy 🐶. And don't **forget to spread the love by sharing Huggy with your friends 🤗**. And if you share about it on social media, **please tag us @huggingface and me @simoninithomas**
|
||||
|
||||
<img src="https://huggingface.co/datasets/huggingface-deep-rl-course/course-images/resolve/main/en/notebooks/unit-bonus1/huggy-cover.jpeg" alt="Huggy cover" width="100%">
|
||||
|
||||
|
||||
## Keep Learning, Stay awesome 🤗
|
||||
File diff suppressed because one or more lines are too long
@@ -38,8 +38,10 @@
|
||||
title: Introduction
|
||||
- local: unitbonus1/how-huggy-works
|
||||
title: How Huggy works?
|
||||
- local: unitbonus1/train-play
|
||||
title: Train and play with Huggy
|
||||
- local: unitbonus1/train
|
||||
title: Train Huggy
|
||||
- local: unitbonus1/play
|
||||
title: Play with Huggy
|
||||
- local: unitbonus1/conclusion
|
||||
title: Conclusion
|
||||
- title: Unit 2. Introduction to Q-Learning
|
||||
|
||||
@@ -1,13 +1,4 @@
|
||||
# Let's train and play with Huggy 🐶 [[train-play]]
|
||||
|
||||
## Let's train Huggy 🐶
|
||||
|
||||
**To start to train Huggy, click on Open In Colab button** 👇 :
|
||||
|
||||
[]()
|
||||
|
||||
|
||||
## Play with Huggy
|
||||
# Play with Huggy [[play]]
|
||||
|
||||
Now that you've trained Huggy and pushed it to the Hub. **You will be able to play with him ❤️**
|
||||
|
||||
265
units/en/unitbonus1/train.mdx
Normal file
265
units/en/unitbonus1/train.mdx
Normal file
@@ -0,0 +1,265 @@
|
||||
# Let's train and play with Huggy 🐶 [[train-play]]
|
||||
|
||||
## Let's train Huggy 🐶
|
||||
|
||||
**To start to train Huggy, click on Open In Colab button** 👇 :
|
||||
|
||||
[]()
|
||||
|
||||
|
||||
# Bonus Unit 1: Let's train Huggy the Dog 🐶 to fetch a stick
|
||||
|
||||
<img src="https://huggingface.co/datasets/huggingface-deep-rl-course/course-images/resolve/main/en/unit2/thumbnail.png" alt="Bonus Unit 1Thumbnail">
|
||||
|
||||
In this notebook, we'll reinforce what we learn in the first Unit by **teaching Huggy the Dog to fetch the stick and then play with it directly in your browser**
|
||||
|
||||
⬇️ Here is an example of what **you will achieve at the end of the unit.** ⬇️ (launch ▶ to see)
|
||||
|
||||
```python
|
||||
%%html
|
||||
<video controls autoplay><source src="https://huggingface.co/datasets/huggingface-deep-rl-course/course-images/resolve/main/en/notebooks/unit-bonus1/huggy.mp4" type="video/mp4"></video>
|
||||
```
|
||||
|
||||
### The environment 🎮
|
||||
|
||||
- Huggy the Dog, an environment created by [Thomas Simonini](https://twitter.com/ThomasSimonini) based on [Puppo The Corgi](https://blog.unity.com/technology/puppo-the-corgi-cuteness-overload-with-the-unity-ml-agents-toolkit)
|
||||
|
||||
### The library used 📚
|
||||
|
||||
- [MLAgents (Hugging Face version)](https://github.com/huggingface/ml-agents)
|
||||
|
||||
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).
|
||||
|
||||
## Objectives of this notebook 🏆
|
||||
|
||||
At the end of the notebook, you will:
|
||||
|
||||
- Understand **the state space, action space and reward function used to train Huggy**.
|
||||
- **Train your own Huggy** to fetch the stick.
|
||||
- Be able to play **with your trained Huggy directly in your browser**.
|
||||
|
||||
|
||||
|
||||
|
||||
## This notebook is from Deep Reinforcement Learning Course
|
||||
<img src="https://huggingface.co/datasets/huggingface-deep-rl-course/course-images/resolve/main/en/notebooks/deep-rl-course-illustration.jpg" alt="Deep RL Course illustration"/>
|
||||
|
||||
In this free course, you will:
|
||||
|
||||
- 📖 Study Deep Reinforcement Learning in **theory and practice**.
|
||||
- 🧑💻 Learn to **use famous Deep RL libraries** such as Stable Baselines3, RL Baselines3 Zoo, CleanRL and Sample Factory 2.0.
|
||||
- 🤖 Train **agents in unique environments**
|
||||
|
||||
And more check 📚 the syllabus 👉 https://simoninithomas.github.io/deep-rl-course
|
||||
|
||||
Don’t forget to **<a href="http://eepurl.com/ic5ZUD">sign up to the course</a>** (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).**
|
||||
|
||||
|
||||
The best way to keep in touch is to join our discord server to exchange with the community and with us 👉🏻 https://discord.gg/ydHrjt3WP5
|
||||
|
||||
## Prerequisites 🏗️
|
||||
|
||||
Before diving into the notebook, you need to:
|
||||
|
||||
🔲 📚 **Develop an understanding of the foundations of Reinforcement learning** (MC, TD, Rewards hypothesis...) by doing Unit 1
|
||||
|
||||
🔲 📚 **Read the introduction to Huggy** by doing Bonus Unit 1
|
||||
|
||||
## Clone the repository and install the dependencies 🔽
|
||||
|
||||
- We need to clone the repository, that **contains the experimental version of the library that allows you to push your trained agent to the Hub.**
|
||||
|
||||
```python
|
||||
%%capture
|
||||
# Clone this specific repository (can take 3min)
|
||||
!git clone https://github.com/huggingface/ml-agents/
|
||||
```
|
||||
|
||||
```python
|
||||
%%capture
|
||||
# Go inside the repository and install the package (can take 3min)
|
||||
%cd ml-agents
|
||||
!pip3 install -e ./ml-agents-envs
|
||||
!pip3 install -e ./ml-agents
|
||||
```
|
||||
|
||||
## Download and move the environment zip file in `./trained-envs-executables/linux/`
|
||||
|
||||
- Our environment executable is in a zip file.
|
||||
- We need to download it and place it to `./trained-envs-executables/linux/`
|
||||
|
||||
```python
|
||||
!mkdir ./trained-envs-executables
|
||||
!mkdir ./trained-envs-executables/linux
|
||||
```
|
||||
|
||||
```python
|
||||
!wget --load-cookies /tmp/cookies.txt "https://docs.google.com/uc?export=download&confirm=$(wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id=1zv3M95ZJTWHUVOWT6ckq_cm98nft8gdF' -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id=1zv3M95ZJTWHUVOWT6ckq_cm98nft8gdF" -O ./trained-envs-executables/linux/Huggy.zip && rm -rf /tmp/cookies.txt
|
||||
```
|
||||
|
||||
Download the file Huggy.zip from https://drive.google.com/uc?export=download&id=1zv3M95ZJTWHUVOWT6ckq_cm98nft8gdF using `wget`. Check out the full solution to download large files from GDrive [here](https://bcrf.biochem.wisc.edu/2021/02/05/download-google-drive-files-using-wget/)
|
||||
|
||||
```python
|
||||
%%capture
|
||||
!unzip -d ./trained-envs-executables/linux/ ./trained-envs-executables/linux/Huggy.zip
|
||||
```
|
||||
|
||||
Make sure your file is accessible
|
||||
|
||||
```python
|
||||
!chmod -R 755 ./trained-envs-executables/linux/Huggy
|
||||
```
|
||||
|
||||
## Let's recap how this environment works
|
||||
|
||||
### The State Space: what Huggy "perceives."
|
||||
|
||||
Huggy doesn't "see" his environment. Instead, we provide him information about the environment:
|
||||
|
||||
- The target (stick) position
|
||||
- The relative position between himself and the target
|
||||
- The orientation of his legs.
|
||||
|
||||
Given all this information, Huggy **can decide which action to take next to fulfill his goal**.
|
||||
|
||||
<img src="https://huggingface.co/datasets/huggingface-deep-rl-course/course-images/resolve/main/en/notebooks/unit-bonus1/huggy.jpg" alt="Huggy" width="100%">
|
||||
|
||||
|
||||
### The Action Space: what moves Huggy can do
|
||||
<img src="https://huggingface.co/datasets/huggingface-deep-rl-course/course-images/resolve/main/en/notebooks/unit-bonus1/huggy-action.jpg" alt="Huggy action" width="100%">
|
||||
|
||||
**Joint motors drive huggy legs**. It means that to get the target, Huggy needs to **learn to rotate the joint motors of each of his legs correctly so he can move**.
|
||||
|
||||
### The Reward Function
|
||||
|
||||
The reward function is designed so that **Huggy will fulfill his goal** : fetch the stick.
|
||||
|
||||
Remember that one of the foundations of Reinforcement Learning is the *reward hypothesis*: a goal can be described as the **maximization of the expected cumulative reward**.
|
||||
|
||||
Here, our goal is that Huggy **goes towards the stick but without spinning too much**. Hence, our reward function must translate this goal.
|
||||
|
||||
Our reward function:
|
||||
|
||||
<img src="https://huggingface.co/datasets/huggingface-deep-rl-course/course-images/resolve/main/en/notebooks/unit-bonus1/reward.jpg" alt="Huggy reward function" width="100%">
|
||||
|
||||
- *Orientation bonus*: we **reward him for getting close to the target**.
|
||||
- *Time penalty*: a fixed-time penalty given at every action to **force him to get to the stick as fast as possible**.
|
||||
- *Rotation penalty*: we penalize Huggy if **he spins too much and turns too quickly**.
|
||||
- *Getting to the target reward*: we reward Huggy for **reaching the target**.
|
||||
|
||||
## Check the Huggy config file
|
||||
|
||||
- In ML-Agents, you define the **training hyperparameters into config.yaml files.**
|
||||
|
||||
- For the scope of this notebook, we're not going to modify the hyperparameters but if you want to try as an experimentation, you should also try to modify some other hyperparameters, Unity provides a very [good documentation explaining each of them here](https://github.com/Unity-Technologies/ml-agents/blob/main/docs/Training-Configuration-File.md).
|
||||
|
||||
- Click here to open the config.yaml: `/content/ml-agents/config/ppo/Huggy.yaml`
|
||||
|
||||
|
||||
We’re now ready to train our agent 🔥.
|
||||
|
||||
## Train our agent
|
||||
|
||||
To train our agent, we just need to **launch mlagents-learn and select the executable containing the environment.**
|
||||
|
||||
<img src="https://huggingface.co/datasets/huggingface-deep-rl-course/course-images/resolve/main/en/notebooks/unit-bonus1/mllearn.png" alt="ml learn function" width="100%">
|
||||
|
||||
We define four parameters:
|
||||
|
||||
1. `mlagents-learn <config>`: the path where the hyperparameter config file is.
|
||||
2. `--env`: where the environment executable is.
|
||||
3. `--run_id`: the name you want to give to your training run id.
|
||||
4. `--no-graphics`: to not launch the visualization during the training.
|
||||
|
||||
Train the model and use the `--resume` flag to continue training in case of interruption.
|
||||
|
||||
> It will fail first time when you use `--resume`, try running the block again to bypass the error.
|
||||
|
||||
|
||||
|
||||
The training will take 30 to 45min depending on your machine, go take a ☕️you deserve it 🤗.
|
||||
|
||||
```python
|
||||
!mlagents-learn ./config/ppo/Huggy.yaml --env=./trained-envs-executables/linux/Huggy/Huggy --run-id="Huggy" --no-graphics
|
||||
```
|
||||
|
||||
## Push the agent to the 🤗 Hub
|
||||
|
||||
- Now that we trained our agent, we’re **ready to push it to the Hub to be able to play with Huggy on your browser🔥.**
|
||||
|
||||
To be able to share your model with the community there are three more steps to follow:
|
||||
|
||||
1️⃣ (If it's not already done) create an account to HF ➡ https://huggingface.co/join
|
||||
|
||||
2️⃣ Sign in and then, you need to store your authentication token from the Hugging Face website.
|
||||
- Create a new token (https://huggingface.co/settings/tokens) **with write role**
|
||||
|
||||
<img src="https://huggingface.co/datasets/huggingface-deep-rl-course/course-images/resolve/main/en/notebooks/create-token.jpg" alt="Create HF Token">
|
||||
|
||||
- Copy the token
|
||||
- Run the cell below and paste the token
|
||||
|
||||
```python
|
||||
from huggingface_hub import notebook_login
|
||||
|
||||
notebook_login()
|
||||
```
|
||||
|
||||
If you don't want to use a Google Colab or a Jupyter Notebook, you need to use this command instead: `huggingface-cli login`
|
||||
|
||||
Then, we simply need to run `mlagents-push-to-hf`.
|
||||
|
||||
<img src="https://huggingface.co/datasets/huggingface-deep-rl-course/course-images/resolve/main/en/notebooks/unit-bonus1/mlpush.png" alt="ml learn function" width="100%">
|
||||
|
||||
And we define 4 parameters:
|
||||
|
||||
1. `--run-id`: the name of the training run id.
|
||||
2. `--local-dir`: where the agent was saved, it’s results/<run_id name>, so in my case results/First Training.
|
||||
3. `--repo-id`: the name of the Hugging Face repo you want to create or update. It’s always <your huggingface username>/<the repo name>
|
||||
If the repo does not exist **it will be created automatically**
|
||||
4. `--commit-message`: since HF repos are git repository you need to define a commit message.
|
||||
|
||||
```python
|
||||
!mlagents-push-to-hf --run-id="HuggyTraining" --local-dir="./results/Huggy" --repo-id="ThomasSimonini/ppo-Huggy" --commit-message="Huggy"
|
||||
```
|
||||
|
||||
Else, if everything worked you should have this at the end of the process(but with a different url 😆) :
|
||||
|
||||
|
||||
|
||||
```
|
||||
Your model is pushed to the hub. You can view your model here: https://huggingface.co/ThomasSimonini/ppo-Huggy
|
||||
```
|
||||
|
||||
It’s the link to your model, it contains a model card that explains how to use it, your Tensorboard and your config file. **What’s awesome is that it’s a git repository, that means you can have different commits, update your repository with a new push etc.**
|
||||
|
||||
<img src="https://huggingface.co/datasets/huggingface-deep-rl-course/course-images/resolve/main/en/notebooks/unit-bonus1/modelcard.png" alt="ml learn function" width="100%">
|
||||
|
||||
But now comes the best: **being able to play with Huggy online 👀.**
|
||||
|
||||
## Play with your Huggy 🐕
|
||||
|
||||
For this step it’s simple:
|
||||
|
||||
- Open the game Huggy in your browser: https://huggingface.co/spaces/ThomasSimonini/Huggy
|
||||
|
||||
- Click on Play with my Huggy model
|
||||
|
||||
<img src="https://huggingface.co/datasets/huggingface-deep-rl-course/course-images/resolve/main/en/notebooks/unit-bonus1/load-huggy.jpg" alt="load-huggy" width="100%">
|
||||
|
||||
1. In step 1, choose your model repository which is the model id (in my case ThomasSimonini/ppo-Huggy).
|
||||
|
||||
2. In step 2, **choose what model you want to replay**:
|
||||
- I have multiple one, since we saved a model every 500000 timesteps.
|
||||
- But if I want the more recent I choose Huggy.onnx
|
||||
|
||||
👉 What’s nice **is to try with different models step to see the improvement of the agent.**
|
||||
|
||||
Congrats on finishing this bonus unit!
|
||||
|
||||
You can now sit and enjoy playing with your Huggy 🐶. And don't **forget to spread the love by sharing Huggy with your friends 🤗**. And if you share about it on social media, **please tag us @huggingface and me @simoninithomas**
|
||||
|
||||
<img src="https://huggingface.co/datasets/huggingface-deep-rl-course/course-images/resolve/main/en/notebooks/unit-bonus1/huggy-cover.jpeg" alt="Huggy cover" width="100%">
|
||||
|
||||
|
||||
## Keep Learning, Stay awesome 🤗
|
||||
Reference in New Issue
Block a user