diff --git a/colabs/wandb-log/Saving_Code_with_W&B.ipynb b/colabs/wandb-log/Saving_Code_with_W&B.ipynb index d615f724..70acad9e 100644 --- a/colabs/wandb-log/Saving_Code_with_W&B.ipynb +++ b/colabs/wandb-log/Saving_Code_with_W&B.ipynb @@ -38,7 +38,7 @@ "\n", "With Weights & Biases, you **won't need to worry** about that happening again!\n", "We'll **save the code you ran along with results and hyperparameters**, all in a centralized location with easy comparison and visualization tools.\n", - "Even better, in a Jupyter notebbok this feature **tracks all the cells you executed!**\n", + "Even better, in a Jupyter notebook this feature **tracks all the cells you executed!**\n", "\n", "Here is a simple implementation where we simulate logging some metrics and the code that generates them -- the same procedure works for both notebooks and Python scripts.\n", "\n", @@ -110,11 +110,11 @@ "with run:\n", " for step in range(100):\n", " # insert training process here\n", - " wandb.log({\n", + " run.log({\n", " \"acc\": math.log(0.1 + random.random() + step * 0.01),\n", " \"val_acc\": math.log(0.1 + random.random() + step * 0.01),\n", - " \"loss\": wandb.config.hyperparameter - math.log(0.1 + random.random() + step * 0.01),\n", - " \"val_loss\": wandb.config.hyperparameter - math.log(0.1 + random.random() + step * 0.01)})" + " \"loss\": run.config.hyperparameter - math.log(0.1 + random.random() + step * 0.01),\n", + " \"val_loss\": run.config.hyperparameter - math.log(0.1 + random.random() + step * 0.01)})" ] }, { @@ -137,10 +137,16 @@ "source": [ "# 💾 Logging Metrics and Saving Code\n", "\n", - "Adding code saving is easy: we just pass the argument\n", - "`save_code=True` to `wandb.init`. That's it!\n", + "To save code, we do two things:\n", "\n", - "_Hot Tip_: If you don't want to worry about setting this on every project,\n", + "1. Pass `save_code=True` to `wandb.init`. This turns on code saving for the run and, in a notebook, tells W&B to try to capture the notebook and the history of cells you executed in the session.\n", + "2. Call `run.log_code()` before the run finishes. This uploads a snapshot of your code -- the files that match the filter you pass (here, `.py` and `.ipynb` files), plus the notebook copy W&B stages in notebook sessions -- as a versioned code artifact. That artifact is what fills in the `{}` code viewer on the run page.\n", + "\n", + "Calling [`run.log_code`](https://docs.wandb.ai/models/ref/python/experiments/run/) is the most reliable way to make sure your code lands in W&B, whether you run a script, a Jupyter notebook, or Colab. In a script, you can also capture code automatically by passing `save_code=True` together with `settings=wandb.Settings(code_dir=\".\")` to `wandb.init`. See the [code saving docs](https://docs.wandb.ai/models/app/features/panels/code) for details.\n", + "\n", + "This time, we'll also let the run span two cells and close it explicitly with `run.finish()`. Whenever a cell executes while a run is active, W&B stages a copy of the notebook, so `run.log_code` can upload it even on platforms like Colab where the notebook file isn't sitting on disk.\n", + "\n", + "_Hot Tip_: If you don't want to worry about setting `save_code` on every project,\n", "just change your default on the [settings page](https://wandb.ai/settings), as below:" ] }, @@ -159,16 +165,27 @@ "source": [ "run = wandb.init(project=\"code_save\",\n", " config={\"hyperparameter\": 4},\n", - " save_code=True)\n", + " save_code=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for step in range(100):\n", + " # insert training process here\n", + " run.log({\n", + " \"acc\": math.log(0.1 + random.random() + step * 0.01),\n", + " \"val_acc\": math.log(0.1 + random.random() + step * 0.01),\n", + " \"loss\": run.config.hyperparameter - math.log(0.1 + random.random() + step * 0.01),\n", + " \"val_loss\": run.config.hyperparameter - math.log(0.1 + random.random() + step * 0.01)})\n", "\n", - "with run:\n", - " for step in range(100):\n", - " # insert training process here\n", - " wandb.log({\n", - " \"acc\": math.log(0.1 + random.random() + step * 0.01),\n", - " \"val_acc\": math.log(0.1 + random.random() + step * 0.01),\n", - " \"loss\": wandb.config.hyperparameter - math.log(0.1 + random.random() + step * 0.01),\n", - " \"val_loss\": wandb.config.hyperparameter - math.log(0.1 + random.random() + step * 0.01)})" + "# upload a snapshot of our code to the run as a code artifact\n", + "run.log_code(include_fn=lambda path: path.endswith(\".py\") or path.endswith(\".ipynb\"))\n", + "\n", + "run.finish()" ] }, { @@ -186,7 +203,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "If you click it, you'll see the entire history of the notebook session! That includes the code we ran for the section without code logging.\n", + "If you click it, you'll see the code that was saved for this run: the files uploaded by `run.log_code` and, in a notebook, the history of the cells executed in the session -- including the ones we ran for the section without code saving.\n", "\n", "W&B also automatically catches the standard out and standard error,\n", "plus system metrics!\n"