|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "<a href=\"https://colab.research.google.com/github/wandb/examples/blob/master/colabs/intro/run_quickstart.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>\n" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "##Use W&B to track, visualize, and manage machine learning experiments of any size.\n", |
| 15 | + "To authenticate your machine with W&B, you need a W&B API key.\n", |
| 16 | + "Save key to Google Colab Secrets under name WANDB_API_KEY. Enable notebook access for key.\n", |
| 17 | + "\n", |
| 18 | + "A [run](/models/runs/) is a core element of W&B. You use runs to [track metrics](/models/track/), [create logs](/models/track/log/), track artifacts, and more.\n" |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "code", |
| 23 | + "execution_count": null, |
| 24 | + "metadata": {}, |
| 25 | + "outputs": [], |
| 26 | + "source": [ |
| 27 | + "!pip install wandb" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "code", |
| 32 | + "execution_count": null, |
| 33 | + "metadata": {}, |
| 34 | + "outputs": [], |
| 35 | + "source": [ |
| 36 | + "import wandb\n", |
| 37 | + "import os\n", |
| 38 | + "from google.colab import userdata\n", |
| 39 | + "\n", |
| 40 | + "os.environ[\"WANDB_API_KEY\"] = userdata.get(\"WANDB_API_KEY\")\n", |
| 41 | + "\n", |
| 42 | + "\n", |
| 43 | + "wandb.login()\n" |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "cell_type": "markdown", |
| 48 | + "metadata": {}, |
| 49 | + "source": [ |
| 50 | + "## Set up project and track hyperparameters\n" |
| 51 | + ] |
| 52 | + }, |
| 53 | + { |
| 54 | + "cell_type": "code", |
| 55 | + "execution_count": null, |
| 56 | + "metadata": {}, |
| 57 | + "outputs": [], |
| 58 | + "source": [ |
| 59 | + "import wandb\n", |
| 60 | + "\n", |
| 61 | + "# Project that the run is recorded to\n", |
| 62 | + "project = \"my-run-quickstart\"\n", |
| 63 | + "\n", |
| 64 | + "# Hyperparameters\n", |
| 65 | + "config = {\n", |
| 66 | + " \"epochs\": 10,\n", |
| 67 | + " \"lr\": 0.01,\n", |
| 68 | + "}\n" |
| 69 | + ] |
| 70 | + }, |
| 71 | + { |
| 72 | + "cell_type": "markdown", |
| 73 | + "metadata": {}, |
| 74 | + "source": [ |
| 75 | + "\n", |
| 76 | + "\n", |
| 77 | + "## Create a machine learning training experiment\n", |
| 78 | + "\n", |
| 79 | + "This mock training script logs simulated accuracy and loss metrics to W&B.\n", |
| 80 | + "\n", |
| 81 | + "\n", |
| 82 | + "Initialize a W&B run object with [`wandb.init()`](/models/ref/python/experiments/run/). Use a dictionary for the `config` parameter\n", |
| 83 | + "to specify hyperparameter names and values. Within the `with` statement, you can log metrics and other information to W&B.\n" |
| 84 | + ] |
| 85 | + }, |
| 86 | + { |
| 87 | + "cell_type": "code", |
| 88 | + "execution_count": null, |
| 89 | + "metadata": {}, |
| 90 | + "outputs": [], |
| 91 | + "source": [ |
| 92 | + "\n", |
| 93 | + "import random\n", |
| 94 | + "\n", |
| 95 | + "\n", |
| 96 | + "with wandb.init(project=project, config=config) as run:\n", |
| 97 | + " offset = random.random() / 5\n", |
| 98 | + " print(f\"lr: {config['lr']}\")\n", |
| 99 | + "\n", |
| 100 | + " for epoch in range(2, config[\"epochs\"]):\n", |
| 101 | + " acc = 1 - 2**-config[\"epochs\"] - random.random() / config[\"epochs\"] - offset\n", |
| 102 | + " loss = 2**-config[\"epochs\"] + random.random() / config[\"epochs\"] + offset\n", |
| 103 | + "\n", |
| 104 | + " print(f\"epoch={epoch}, accuracy={acc:.4f}, loss={loss:.4f}\")\n", |
| 105 | + " run.log({\"epoch\": epoch, \"accuracy\": acc, \"loss\": loss})\n" |
| 106 | + ] |
| 107 | + }, |
| 108 | + { |
| 109 | + "cell_type": "markdown", |
| 110 | + "metadata": {}, |
| 111 | + "source": [ |
| 112 | + "Visit the custom \"View run\" link in the prior cell's output (or go to wandb.ai/home) to view recorded metrics such as accuracy and loss and how they changed during each training step. W&B Runs show the loss and accuracy tracked from each run. Each run object appears in the Runs page with auto-generated names." |
| 113 | + ] |
| 114 | + } |
| 115 | + ], |
| 116 | + "metadata": { |
| 117 | + "accelerator": "GPU", |
| 118 | + "colab": { |
| 119 | + "include_colab_link": true, |
| 120 | + "provenance": [], |
| 121 | + "toc_visible": true |
| 122 | + }, |
| 123 | + "kernelspec": { |
| 124 | + "display_name": "Python 3", |
| 125 | + "name": "python3" |
| 126 | + } |
| 127 | + }, |
| 128 | + "nbformat": 4, |
| 129 | + "nbformat_minor": 0 |
| 130 | +} |
0 commit comments