From 6a7f65563c60a2b92c9bb1c2a81742368296c612 Mon Sep 17 00:00:00 2001 From: SuperSecureHuman Date: Sat, 7 May 2022 22:17:45 +0530 Subject: [PATCH] Added wandb logging --- unit1/unit1_bonus/Wandb Logging.ipynb | 383 ++++++++++++++++++++++++++ 1 file changed, 383 insertions(+) create mode 100644 unit1/unit1_bonus/Wandb Logging.ipynb diff --git a/unit1/unit1_bonus/Wandb Logging.ipynb b/unit1/unit1_bonus/Wandb Logging.ipynb new file mode 100644 index 0000000..b314b90 --- /dev/null +++ b/unit1/unit1_bonus/Wandb Logging.ipynb @@ -0,0 +1,383 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "59a253e3", + "metadata": {}, + "source": [ + "## This notebook will guide you through logging your experiments" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "2e020e33", + "metadata": { + "ExecuteTime": { + "end_time": "2022-05-07T16:36:51.138888Z", + "start_time": "2022-05-07T16:36:49.643694Z" + } + }, + "outputs": [], + "source": [ + "# Import libraries \n", + "\n", + "# The Training Libs\n", + "import gym\n", + "from stable_baselines3 import PPO\n", + "from stable_baselines3.common.evaluation import evaluate_policy\n", + "from stable_baselines3.common.env_util import make_vec_env\n", + "\n", + "from stable_baselines3.common.vec_env import VecVideoRecorder\n", + "\n", + "# Import wandb stuff\n", + "import wandb\n", + "from wandb.integration.sb3 import WandbCallback" + ] + }, + { + "cell_type": "markdown", + "id": "fa321aa8", + "metadata": {}, + "source": [ + "## Weights and biases (wandb)\n", + "\n", + "[Weights and Biases](https://wandb.ai/) is a free and open source logging platform that allows you to track multiple results when you are training your model. It collects various metrics, and organizes them in an simple web dashboard. \n", + "\n", + "In this notebook, we will see how simple it is to start tracking your experiments with weights and biases" + ] + }, + { + "cell_type": "markdown", + "id": "285ecb67", + "metadata": {}, + "source": [ + "It can log all the training metrics that is output during the training phase, along with a video of our agent at that point in our training. Additionally, it also logs system status like CPU/GPU usage and temperature.\n", + "\n", + "They have good documentation, which can be found here - https://docs.wandb.ai/" + ] + }, + { + "cell_type": "markdown", + "id": "fbefa8c4", + "metadata": {}, + "source": [ + "## Setup for wandb\n", + "\n", + "1. Create an account on wandb - https://wandb.ai/\n", + "\n", + "2. Install wandb: \n", + "\n", + " Wandb is distributed as a python package. To install it, run `pip install wandb`.\n", + " \n", + "3. Login into wandb:\n", + "\n", + " To login into wandb, run `wandb login`. Paste the api key. The key can be found here - https://wandb.ai/authorize" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "ae0fd212", + "metadata": { + "ExecuteTime": { + "end_time": "2022-05-07T16:36:59.329343Z", + "start_time": "2022-05-07T16:36:53.808583Z" + } + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "\u001b[34m\u001b[1mwandb\u001b[0m: Currently logged in as: \u001b[33msupersecurehuman\u001b[0m. Use \u001b[1m`wandb login --relogin`\u001b[0m to force relogin\n" + ] + }, + { + "data": { + "text/html": [ + "Tracking run with wandb version 0.12.16" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "Run data is saved locally in /home/venom/Desktop/deep-rl-class/unit1/unit1_bonus/wandb/run-20220507_220654-8czogswz" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "Syncing run brisk-fire-2 to Weights & Biases (docs)
" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# Initilaize wandb\n", + "# https://docs.wandb.ai/guides/integrations/other/stable-baselines-3\n", + "\n", + "config = {\n", + " \"policy_type\": \"MlpPolicy\",\n", + " \"total_timesteps\": 100000,\n", + " \"env_name\": \"LunarLander-v2\",\n", + "}\n", + "\n", + "run = wandb.init(\n", + " project=\"LunarLander-v2\", # It creates a project on wandb if it doesnt exist. The logging happens there\n", + " config=config,\n", + " sync_tensorboard=True, # auto-upload tensorboard metrics\n", + " monitor_gym=True, # auto-upload the videos of agents playing the game\n", + ")\n", + "\n", + "# After running this cell, go to https://wandb.ai/home to see your new project created" + ] + }, + { + "cell_type": "markdown", + "id": "5ee51e03", + "metadata": {}, + "source": [ + "## Now, we will create a environment and a model to log the training\n", + "\n", + "This part is covered in the main notebook\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "0b8046ae", + "metadata": { + "ExecuteTime": { + "end_time": "2022-05-07T16:37:34.011591Z", + "start_time": "2022-05-07T16:37:29.500555Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Using cuda device\n" + ] + } + ], + "source": [ + "env = make_vec_env('LunarLander-v2', n_envs=16)\n", + "# Use the folling line with caution. The video recorder will try to render the agent on the screen, so that ffmpeg can caputre it. Here, we have 16 envs set. Trying to render 16 envs on screen will\n", + "# be pretty resource intensive. \n", + "# env = VecVideoRecorder(env, f\"videos/{run.id}\", record_video_trigger=lambda x: x % 2000 == 0, video_length=200) # Set the video recorder, to record our agent during training\n", + "\n", + "model = PPO(\n", + " policy = config[\"policy_type\"],\n", + " env = env,\n", + " tensorboard_log=\"logs\",\n", + " verbose=1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f772cc3f", + "metadata": { + "ExecuteTime": { + "end_time": "2022-05-07T16:39:54.654747Z", + "start_time": "2022-05-07T16:37:35.052748Z" + }, + "scrolled": true + }, + "outputs": [], + "source": [ + "# Now we do the magical stuff of logging to wandb. All you have to do is add the wandb callback to the model's callback like this\n", + "\n", + "model.learn(total_timesteps=config[\"total_timesteps\"], callback=[WandbCallback()])" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "d2a6341c", + "metadata": { + "ExecuteTime": { + "end_time": "2022-05-07T16:40:06.824067Z", + "start_time": "2022-05-07T16:39:54.696684Z" + } + }, + "outputs": [ + { + "data": { + "text/html": [ + "Waiting for W&B process to finish... (success)." + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "VBox(children=(Label(value='0.002 MB of 0.002 MB uploaded (0.000 MB deduped)\\r'), FloatProgress(value=1.0, max…" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "\n", + "

Run history:


global_step▁▃▆█
rollout/ep_len_mean▁▃▄█
rollout/ep_rew_mean▁▄▆█
time/fps█▁▁▁
train/approx_kl▁▇█
train/clip_fraction▁▅█
train/clip_range▁▁▁
train/entropy_loss▁▄█
train/explained_variance▁▇█
train/learning_rate▁▁▁
train/loss█▅▁
train/policy_gradient_loss█▄▁
train/value_loss█▂▁

Run summary:


global_step131072
rollout/ep_len_mean107.59
rollout/ep_rew_mean-75.28564
time/fps1176.0
train/approx_kl0.01353
train/clip_fraction0.20614
train/clip_range0.2
train/entropy_loss-1.30999
train/explained_variance0.65612
train/learning_rate0.0003
train/loss44.55813
train/policy_gradient_loss-0.01972
train/value_loss212.05472

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "Synced brisk-fire-2: https://wandb.ai/supersecurehuman/LunarLander-v2/runs/8czogswz
Synced 6 W&B file(s), 0 media file(s), 0 artifact file(s) and 1 other file(s)" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "Find logs at: ./wandb/run-20220507_220654-8czogswz/logs" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# Finish run\n", + "run.finish()\n", + "\n", + "# This cell output will also give a global summary, along with giving you the link to view your run." + ] + }, + { + "cell_type": "markdown", + "id": "3ffa0dae", + "metadata": {}, + "source": [ + "### Note\n", + "\n", + "You need to enable tensorboard logging to view your training metrics in wandb dashboard." + ] + }, + { + "cell_type": "markdown", + "id": "a03b8746", + "metadata": {}, + "source": [ + "## Congarts!\n", + "\n", + "Now you have now started to use wandb in your project. Do checkout the docs to know what are the other amazing stuff it is capable off!" + ] + } + ], + "metadata": { + "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.7.12" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": false + }, + "varInspector": { + "cols": { + "lenName": 16, + "lenType": 16, + "lenVar": 40 + }, + "kernels_config": { + "python": { + "delete_cmd_postfix": "", + "delete_cmd_prefix": "del ", + "library": "var_list.py", + "varRefreshCmd": "print(var_dic_list())" + }, + "r": { + "delete_cmd_postfix": ") ", + "delete_cmd_prefix": "rm(", + "library": "var_list.r", + "varRefreshCmd": "cat(var_dic_list()) " + } + }, + "types_to_exclude": [ + "module", + "function", + "builtin_function_or_method", + "instance", + "_Feature" + ], + "window_display": false + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}