{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "id": "TsHV-7cpVkyK" }, "outputs": [], "source": [ "##### Copyright 2019 The TensorFlow Authors." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "id": "atWM-s8yVnfX", "vscode": { "languageId": "python" } }, "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": "TB0wBWfcVqHz" }, "source": [ "# 在笔记本中使用 TensorBoard\n", "\n", "\n", " \n", " \n", " \n", " \n", "
在 TensorFlow.org 上查看 在 Google Colab 中运行 在 GitHub 中查看源代码 下载笔记本
" ] }, { "cell_type": "markdown", "metadata": { "id": "elH58gbhWAmn" }, "source": [ "TensorBoard 可以直接在诸如 [Colab](https://colab.research.google.com/) 和 [Jupyter](https://jupyter.org/) 一类的笔记本体验中使用。这有助于共享结果、将 TensorBoard 集成到现有工作流,以及在不进行任何本地安装的情况下使用 TensorBoard。" ] }, { "cell_type": "markdown", "metadata": { "id": "VszJNloY3ZU3" }, "source": [ "## 设置" ] }, { "cell_type": "markdown", "metadata": { "id": "E6QhA_dp3eRq" }, "source": [ "首先,安装 TF 2.0 并加载 TensorBoard 笔记本扩展程序:\n", "\n", "**对于 Jupyter 用户**:如果您已经将 Jupyter 和 TensorBoard 安装在同一 virtualenv 中,那么您无需进行其他设置。如果您使用更复杂的设置,例如为不同 Conda/virtualenv 环境使用全局 Jupyter 安装和内核,则必须确保 `tensorboard` 二进制文件位于 Jupyter 笔记本上下文内的 `PATH` 中。执行此操作的一种方法是修改 `kernel_spec`,在 `PATH` 前添加环境的 `bin` 目录,[如此处所述](https://github.com/ipython/ipykernel/issues/395#issuecomment-479787997)。\n" ] }, { "cell_type": "markdown", "metadata": { "id": "9w7Baxc8aCtJ" }, "source": [ "**对于 Docker 用户**:如果您[使用 TensorFlow 的 Nightly 版本运行 Jupyter Notebook 服务器的](https://docs.docker.com/install/) [Docker](https://tensorflow.google.cn/install/docker#examples_using_cpu-only_images) 镜像,则不仅要公开笔记本的端口,还要公开 TensorBoard 的端口。因此,使用以下命令运行容器:\n", "\n", "```\n", "docker run -it -p 8888:8888 -p 6006:6006 \\\n", "tensorflow/tensorflow:nightly-py3-jupyter\n", "```\n", "\n", "其中,`-p 6006` 为 TensorBoard 的默认端口。这将为您分配一个端口来运行一个 TensorBoard 实例。要运行并发实例,必须分配多个端口。此外,将 `--bind_all` 传递给 `%tensorboard` 可以在容器外公开端口。" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "id": "8p3Tbx8cWEFA", "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "# Load the TensorBoard notebook extension\n", "%load_ext tensorboard" ] }, { "cell_type": "markdown", "metadata": { "id": "9GtR_cTTkf9G" }, "source": [ "导入 TensorFlow、日期时间和操作系统:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "mVtYvbbIWRkV", "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "import tensorflow as tf\n", "import datetime, os" ] }, { "cell_type": "markdown", "metadata": { "id": "Cu1fbH-S3oAX" }, "source": [ "## 在笔记本中使用 TensorBoard" ] }, { "cell_type": "markdown", "metadata": { "id": "XfCa27_8kov6" }, "source": [ "下载 [FashionMNIST](https://github.com/zalandoresearch/fashion-mnist) 数据集并对其进行缩放:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "id": "z8b82G7YksOS", "vscode": { "languageId": "python" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz\n", "32768/29515 [=================================] - 0s 0us/step\n", "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz\n", "26427392/26421880 [==============================] - 0s 0us/step\n", "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz\n", "8192/5148 [===============================================] - 0s 0us/step\n", "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz\n", "4423680/4422102 [==============================] - 0s 0us/step\n" ] } ], "source": [ "fashion_mnist = tf.keras.datasets.fashion_mnist\n", "\n", "(x_train, y_train),(x_test, y_test) = fashion_mnist.load_data()\n", "x_train, x_test = x_train / 255.0, x_test / 255.0" ] }, { "cell_type": "markdown", "metadata": { "id": "lBk1BqAZKEKd" }, "source": [ "创建一个非常简单的模型:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "OS7qGYiMKGQl", "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "def create_model():\n", " return tf.keras.models.Sequential([\n", " tf.keras.layers.Flatten(input_shape=(28, 28), name='layers_flatten'),\n", " tf.keras.layers.Dense(512, activation='relu', name='layers_dense'),\n", " tf.keras.layers.Dropout(0.2, name='layers_dropout'),\n", " tf.keras.layers.Dense(10, activation='softmax', name='layers_dense_2')\n", " ])" ] }, { "cell_type": "markdown", "metadata": { "id": "RNaPPs5ZKNOV" }, "source": [ "使用 Keras 和 TensorBoard 回调训练模型:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "id": "lpUO9HqUKP6z", "vscode": { "languageId": "python" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Train on 60000 samples, validate on 10000 samples\n", "Epoch 1/5\n", "60000/60000 [==============================] - 11s 182us/sample - loss: 0.4976 - accuracy: 0.8204 - val_loss: 0.4143 - val_accuracy: 0.8538\n", "Epoch 2/5\n", "60000/60000 [==============================] - 10s 174us/sample - loss: 0.3845 - accuracy: 0.8588 - val_loss: 0.3855 - val_accuracy: 0.8626\n", "Epoch 3/5\n", "60000/60000 [==============================] - 10s 175us/sample - loss: 0.3513 - accuracy: 0.8705 - val_loss: 0.3740 - val_accuracy: 0.8607\n", "Epoch 4/5\n", "60000/60000 [==============================] - 11s 177us/sample - loss: 0.3287 - accuracy: 0.8793 - val_loss: 0.3596 - val_accuracy: 0.8719\n", "Epoch 5/5\n", "60000/60000 [==============================] - 11s 178us/sample - loss: 0.3153 - accuracy: 0.8825 - val_loss: 0.3360 - val_accuracy: 0.8782\n" ] } ], "source": [ "def train_model():\n", " \n", " model = create_model()\n", " model.compile(optimizer='adam',\n", " loss='sparse_categorical_crossentropy',\n", " metrics=['accuracy'])\n", "\n", " logdir = os.path.join(\"logs\", datetime.datetime.now().strftime(\"%Y%m%d-%H%M%S\"))\n", " tensorboard_callback = tf.keras.callbacks.TensorBoard(logdir, histogram_freq=1)\n", "\n", " model.fit(x=x_train, \n", " y=y_train, \n", " epochs=5, \n", " validation_data=(x_test, y_test), \n", " callbacks=[tensorboard_callback])\n", "\n", "train_model()" ] }, { "cell_type": "markdown", "metadata": { "id": "SxvXc4hoKW7d" }, "source": [ "使用[魔术命令](https://ipython.readthedocs.io/en/stable/interactive/magics.html)在笔记本中启动 TensorBoard:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "KBHp6M_zgjp4", "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "%tensorboard --logdir logs" ] }, { "cell_type": "markdown", "metadata": { "id": "Po7rTfQswAMT" }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "id": "aQq3UHgmLBpC" }, "source": [ "您现在可以查看 **Time Series**、**Graphs**、**Distributions** 等信息中心。某些信息中心在 Colab 中尚不可用(例如配置文件插件)。\n", "\n", "`%tensorboard` 魔术命令与 TensorBoard 命令行调用的格式基本相同,区别在于其开头带有 `%` 符号。" ] }, { "cell_type": "markdown", "metadata": { "id": "NiIMwOG8MR_g" }, "source": [ "您也可以在训练前启动 TensorBoard,对其进行监视:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "qyI5lrXoMw9K", "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "%tensorboard --logdir logs" ] }, { "cell_type": "markdown", "metadata": { "id": "ALxC8BbWWV91" }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "id": "GUSM8yLrO2yZ" }, "source": [ "通过发出相同的命令,可以重用相同的 TensorBoard 后端。如果选择了其他日志目录,将打开新的 TensorBoard 实例。将自动管理端口。\n", "\n", "开始训练新模型,观察 TensorBoard 每 30 秒自动更新一次,或者使用右上角的按钮进行刷新:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "id": "ixZlmtWhMyr4", "vscode": { "languageId": "python" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Train on 60000 samples, validate on 10000 samples\n", "Epoch 1/5\n", "60000/60000 [==============================] - 11s 184us/sample - loss: 0.4968 - accuracy: 0.8223 - val_loss: 0.4216 - val_accuracy: 0.8481\n", "Epoch 2/5\n", "60000/60000 [==============================] - 11s 176us/sample - loss: 0.3847 - accuracy: 0.8587 - val_loss: 0.4056 - val_accuracy: 0.8545\n", "Epoch 3/5\n", "60000/60000 [==============================] - 11s 176us/sample - loss: 0.3495 - accuracy: 0.8727 - val_loss: 0.3600 - val_accuracy: 0.8700\n", "Epoch 4/5\n", "60000/60000 [==============================] - 11s 179us/sample - loss: 0.3282 - accuracy: 0.8795 - val_loss: 0.3636 - val_accuracy: 0.8694\n", "Epoch 5/5\n", "60000/60000 [==============================] - 11s 176us/sample - loss: 0.3115 - accuracy: 0.8839 - val_loss: 0.3438 - val_accuracy: 0.8764\n" ] } ], "source": [ "train_model()" ] }, { "cell_type": "markdown", "metadata": { "id": "IlDz2oXBgnZ9" }, "source": [ "您可以使用 `tensorboard.notebook` API 进行更多控制:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "id": "ko9qeSQHLrEh", "vscode": { "languageId": "python" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Known TensorBoard instances:\n", " - port 6006: logdir logs (started 0:00:54 ago; pid 265)\n" ] } ], "source": [ "from tensorboard import notebook\n", "notebook.list() # View open TensorBoard instances" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "hzm9DNVILxJe", "vscode": { "languageId": "python" } }, "outputs": [], "source": [ "# Control TensorBoard display. If no port is provided, \n", "# the most recently launched TensorBoard is used\n", "notebook.display(port=6006, height=1000) " ] }, { "cell_type": "markdown", "metadata": { "id": "za2GqzKiWY-R" }, "source": [ "" ] } ], "metadata": { "colab": { "collapsed_sections": [], "name": "tensorboard_in_notebooks.ipynb", "toc_visible": true }, "kernelspec": { "display_name": "Python 3", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 0 }