{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "![Neptune + Colab](https://neptune.ai/wp-content/uploads/2023/09/colab.svg)\n", "\n", "# Neptune + Google Colab\n", "\n", "\n", " \"Open\n", "\n", " \"Open\n", " \n", " \"Explore\n", "\n", " \"View\n", "" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "tags": [ "header", "comment" ] }, "source": [ "## Introduction" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "tags": [ "comment" ] }, "source": [ "This guide will show you how to:\n", "\n", "* Install `neptune`,\n", "* Connect Neptune to your Colab notebook and create the first run,\n", "* Log simple metrics to Neptune and explore them in the web app." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Before you start\n", "\n", "Make sure that you have a [Google account](https://support.google.com/accounts/answer/27441?hl=en).\n", "\n", "If you want to see the example logged to your own workspace instead:\n", "\n", " 1. Create a Neptune account. [Register →](https://neptune.ai/register)\n", " 1. Create a Neptune project that you will use for tracking metadata. For instructions, see [Creating a project](https://docs-legacy.neptune.ai/setup/creating_project) in the Neptune docs." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "tags": [ "header", "installation" ] }, "source": [ "## Install Neptune and dependencies" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "installation" ] }, "outputs": [], "source": [ "! pip install neptune" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import neptune" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "tags": [ "header" ] }, "source": [ "## Initialize Neptune" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "To create a new run for tracking the metadata, you tell Neptune who you are (with the API token) and where to send the data (your project).\n", "\n", "To find your API token:\n", "\n", "1. [Log in to Neptune](https://app.neptune.ai/).\n", "1. In the bottom-left corner, expand your user menu and select **Get your API token**.\n", "\n", "For more help, see [Setting Neptune credentials](https://docs-legacy.neptune.ai/setup/setting_credentials) in the Neptune docs.\n", "\n", "### Logging anonymously\n", "\n", "Replace the code below with the following:\n", "\n", "```python\n", "api_token = neptune.ANONYMOUS_API_TOKEN\n", "```\n", "\n", "**Note**: Public projects are cleaned regularly, so anonymous runs are only stored temporarily." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "code", "exclude" ] }, "outputs": [], "source": [ "from getpass import getpass\n", "\n", "api_token = getpass(\"Enter your private Neptune API token: \")" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "tags": [ "header", "exclude" ] }, "source": [ "### Initialize your project\n", "\n", "If you haven't already, [create a new project](https://docs-legacy.neptune.ai/setup/creating_project) that you will use for metadata tracking.\n", "\n", "You need to give the full project name, in the form `workspace-name/project-name`.\n", "\n", "To copy the project path, open the settings menu and select **Details & privacy**." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "code", "exclude" ] }, "outputs": [], "source": [ "workspace = \"YOUR_WORKSPACE\" # replace with your own\n", "project_name = \"YOUR_PROJECT\" # replace\n", "project = f\"{workspace}/{project_name}\"\n", "\n", "# if you are using ANONYMOUS api token, log to the project 'common/neptune-and-google-colab'\n", "# project = 'common/neptune-and-google-colab'\n", "\n", "print(project)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "We're ready to initialize a Neptune run." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "run = neptune.init_run(\n", " project=project,\n", " api_token=api_token,\n", " capture_hardware_metrics=True,\n", " capture_stderr=True,\n", " capture_stdout=True,\n", ") # Hardware metrics, stderr, and stdout are not captured by default in interactive kernels" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "tags": [ "comment", "exclude" ] }, "source": [ "**To open the run in the Neptune web app, click the link that appeared in the cell output.**\n", "\n", "We'll use the `run` object we just created to log metadata. You'll see the metadata appear in the app." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "tags": [ "comment" ] }, "source": [ "Runs can be viewed as dictionary-like structures - **namespaces** - that you can define in your code. You can apply a hierarchical structure to your metadata that will be reflected in the UI as well. Thanks to this you can easily organize your metadata in a way you feel is most convenient." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "tags": [ "header" ] }, "source": [ "## Log metadata during training" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "tags": [ "comment" ] }, "source": [ "Log metrics or losses under a name of your choice. You can log one or multiple values.\n", "\n", "Now run the cell below, and switch over to the Neptune app to view the live logging." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "code" ] }, "outputs": [], "source": [ "from time import sleep\n", "\n", "params = {\"learning_rate\": 0.1}\n", "\n", "# log params\n", "run[\"parameters\"] = params\n", "\n", "# log name and append tags\n", "run[\"sys/name\"] = \"colab-example\"\n", "run[\"sys/tags\"].add([\"colab\", \"simple\"])\n", "\n", "# log loss during training\n", "for epoch in range(132):\n", " sleep(0.1) # to see logging live\n", " run[\"train/loss\"].append(0.97**epoch)\n", " run[\"train/loss-pow-2\"].append((0.97**epoch) ** 2)\n", "\n", "# log train and validation scores\n", "run[\"train/accuracy\"] = 0.95\n", "run[\"valid/accuracy\"] = 0.93\n", "\n", "# log files/artifacts\n", "! echo \"Welcome to Neptune\" > file.txt\n", "run[\"artifacts/sample\"].upload(\"file.txt\") # file will be uploaded as sample.txt" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "The snippet above logs:\n", "\n", "* `parameters` with just one field: learning rate,\n", "* name of run and two tags,\n", "* `train/loss` and `train/loss-pow-2` as series of numbers, visualized as charts in UI,\n", "* `train/accuracy` and `valid/accuracy` as single values\n", "* `file.txt` which will be visible under All Metadata/artifacts as sample.txt" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "**Tip:**
\n", "To view the structure of a run, use the `print_structure()` method." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "run.print_structure()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Stop logging \n", "**Warning:**
\n", "Once you are done logging, you should stop tracking the run using the `stop()` method.\n", "This is needed only while logging from a notebook environment. While logging through a script, Neptune automatically stops tracking once the script has completed execution." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "run.stop()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "tags": [ "comment" ] }, "source": [ "## Explore the run in the Neptune app\n", "\n", "Go to the **All metadata** and **Charts** sections of the Neptune app to see them. You can also check an [example run](https://app.neptune.ai/o/common/org/showroom/e/SHOW-37/charts).\n", "\n", "You can see the hardware consumption in the **Monitoring** section of the Neptune app." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "tags": [ "comment" ] }, "source": [ "## Conclusion\n", "\n", "You’ve learned how to:\n", "* Install `neptune`,\n", "* Connect Neptune to your Google Colab notebook and create a run,\n", "* Log metadata to Neptune,\n", "* See your metrics parameters and scores,\n", "* See hardware consumption during the run." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "tags": [ "comment" ] }, "source": [ "## What's next\n", "\n", "Now that you know how to create runs and log metrics, you can learn:\n", "\n", "* [How to log other types of metadata to Neptune](https://docs-legacy.neptune.ai/logging/what_you_can_log/)\n", "* [How to download runs data from Neptune](https://docs-legacy.neptune.ai/usage/querying_metadata/)\n", "* [How to connect Neptune to the ML framework you are using](https://docs-legacy.neptune.ai/essentials/integrations)" ] } ], "metadata": { "colab": { "collapsed_sections": [], "name": "Basic-Colab-Example.ipynb", "private_outputs": true, "provenance": [], "toc_visible": true }, "kernelspec": { "display_name": "py38", "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.8.15" }, "toc-autonumbering": false, "toc-showcode": false, "toc-showmarkdowntxt": false, "toc-showtags": false, "vscode": { "interpreter": { "hash": "a9715cf0b0024f6e1c62cb31a4f1f43970eb41991212681878768b4bfe53050a" } } }, "nbformat": 4, "nbformat_minor": 4 }