{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "h3Nuf-G4xJ0u" }, "source": [ "##### Copyright 2020 The TensorFlow Authors." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "id": "zZ81_4tLxSvd" }, "outputs": [], "source": [ "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# https://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License." ] }, { "cell_type": "markdown", "metadata": { "id": "wNBP_f0QUTfO" }, "source": [ "# Getting started with [TensorBoard.dev](https://tensorboard.dev)" ] }, { "cell_type": "markdown", "metadata": { "id": "DLXZ3t1PWdOp" }, "source": [ "[TensorBoard.dev](https://tensorboard.dev) is a free, public [TensorBoard](https://tensorflow.org/tensorboard) service that enables you to upload and share your ML experiments with everyone.\n", "\n", "This notebook trains a simple model and shows how to upload the logs to TensorBoard.dev. [Preview](https://tensorboard.dev/experiment/rldGbR8rRHeCEbkK61SWTQ)." ] }, { "cell_type": "markdown", "metadata": { "id": "yjBn-ptXTppA" }, "source": [ "### Setup and imports\n", "\n", "This notebook uses TensorBoard features which are only available for versions >= `2.3.0`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "L3ns52Luracm" }, "outputs": [], "source": [ "import tensorflow as tf\n", "import datetime\n", "from tensorboard.plugins.hparams import api as hp" ] }, { "cell_type": "markdown", "metadata": { "id": "GqUABmUTT1Cl" }, "source": [ "### Train a simple model and create TensorBoard logs" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "LZExSr2Qrc5S" }, "outputs": [], "source": [ "mnist = tf.keras.datasets.mnist\n", "\n", "(x_train, y_train),(x_test, y_test) = mnist.load_data()\n", "x_train, x_test = x_train / 255.0, x_test / 255.0\n", "\n", "def create_model():\n", " return tf.keras.models.Sequential([\n", " tf.keras.layers.Flatten(input_shape=(28, 28)),\n", " tf.keras.layers.Dense(512, activation='relu'),\n", " tf.keras.layers.Dropout(0.2),\n", " tf.keras.layers.Dense(10, activation='softmax')\n", " ])" ] }, { "cell_type": "markdown", "metadata": { "id": "FSv4C0dBrmAx" }, "source": [ "TensorBoard logs are created during training by passing the [TensorBoard](https://www.tensorflow.org/tensorboard/get_started) and [hyperparameters callbacks](https://www.tensorflow.org/tensorboard/hyperparameter_tuning_with_hparams) to Keras' Model.fit(). These logs can then be uploaded to TensorBoard.dev.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "dsVjm5CrUtXm" }, "outputs": [], "source": [ "model = create_model()\n", "model.compile(\n", " optimizer='adam',\n", " loss='sparse_categorical_crossentropy',\n", " metrics=['accuracy'])\n", "\n", "log_dir=\"logs/fit/\" + datetime.datetime.now().strftime(\"%Y%m%d-%H%M%S\")\n", "tensorboard_callback = tf.keras.callbacks.TensorBoard(\n", " log_dir=log_dir, histogram_freq=1)\n", "hparams_callback = hp.KerasCallback(log_dir, {\n", " 'num_relu_units': 512,\n", " 'dropout': 0.2\n", "})\n", "\n", "model.fit(\n", " x=x_train, \n", " y=y_train, \n", " epochs=5, \n", " validation_data=(x_test, y_test), \n", " callbacks=[tensorboard_callback, hparams_callback])" ] }, { "cell_type": "markdown", "metadata": { "id": "TgF35qdzIC3T" }, "source": [ "### (Jupyter only) Authorize TensorBoard.dev\n", "\n", "**This step is not necessary in Colab**\n", "\n", "This step requires you to auth in your shell console, outside of Jupyter. In your console, execute the following command.\n", "\n", "`tensorboard dev list`\n", "\n", "As part of this flow, you will be provided with an authorization code. This code is required to consent to the Terms of Service." ] }, { "cell_type": "markdown", "metadata": { "id": "oKW8V5chyx6e" }, "source": [ "### Upload to TensorBoard.dev\n", "\n", "Uploading the TensorBoard logs will give you a URL that can be shared with anyone.\n", "\n", "Uploaded TensorBoards are public, so do not upload sensitive data.\n", "\n", "The uploader will exit when the entire logdir has uploaded. (This is what the `--one_shot` flag specifies.)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "n2PvxhOkW7vn" }, "outputs": [], "source": [ "!tensorboard dev upload --logdir ./logs \\\n", " --name \"Simple experiment with MNIST\" \\\n", " --description \"Training results from https://colab.sandbox.google.com/github/tensorflow/tensorboard/blob/master/docs/tbdev_getting_started.ipynb\" \\\n", " --one_shot" ] }, { "cell_type": "markdown", "metadata": { "id": "5QH5k4AUNE27" }, "source": [ "Each individual upload has a unique experiment ID. This means that if you start a new upload with the same directory, you will get a new experiment ID.\n", "You can list all the experiments you have uploaded using \n", "```\n", "tensorboard dev list\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "C2Pj3RQCNQvP" }, "outputs": [], "source": [ "!tensorboard dev list" ] }, { "cell_type": "markdown", "metadata": { "id": "NyJsD3Ypyx6m" }, "source": [ "### Screenshot of TensorBoard.dev\n", "\n", "This is what it will look like when you navigate to your new experiment on TensorBoard.dev.\n", "\n", "![screenshot of TensorBoard.dev](images/tbdev_getting_started.png \"TensorBoard.dev screenshot\")" ] }, { "cell_type": "markdown", "metadata": { "id": "JcZOGmjQNWk_" }, "source": [ "### Deleting your TensorBoard.dev experiment\n", "\n", "To remove an experiment you have uploaded, use the `delete` command and specify the appropriate `experiment_id`.\n", "In the above screenshot, the experiment_id is listed in the bottom left corner: `w1lkBAOrR4eH35Y7Lg1DQQ`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "VSkJTT9rNWJq" }, "outputs": [], "source": [ "# You must replace YOUR_EXPERIMENT_ID with the value output from the previous\n", "# tensorboard `list` command or `upload` command. For example\n", "# `tensorboard dev delete --experiment_id pQpJNh00RG2Lf1zOe9BrQA`\n", "\n", "## !tensorboard dev delete --experiment_id YOUR_EXPERIMENT_ID_HERE" ] } ], "metadata": { "colab": { "collapsed_sections": [], "name": "tbdev_getting_started.ipynb", "toc_visible": true }, "kernelspec": { "display_name": "Python 3", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 0 }