{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "SB93Ge748VQs" }, "source": [ "##### Copyright 2019 The TensorFlow Authors." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "id": "0sK8X2O9bTlz" }, "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": "HEYuO5NFwDK9" }, "source": [ "# 开始使用 TensorBoard\n", "\n", "\n", " \n", " \n", " \n", " \n", "
在 TensorFlow.org 上查看 在 Google Colab 中运行 在 Github 上查看源代码 下载笔记本
" ] }, { "cell_type": "markdown", "metadata": { "id": "56V5oun18ZdZ" }, "source": [ "在机器学习中,要改进模型的某些参数,您通常需要对其进行衡量。TensorBoard 是用于提供机器学习工作流期间所需测量和呈现的工具。它使您能够跟踪实验指标(例如损失和准确率),呈现模型计算图,将嵌入向量投影到较低维度的空间等。\n", "\n", "本快速入门将展示如何快速使用 TensorBoard 。该网站上的其余指南提供了有关特定功能的更多详细信息,此处未包括其中的许多功能。 " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "6B95Hb6YVgPZ" }, "outputs": [], "source": [ "# Load the TensorBoard notebook extension\n", "%load_ext tensorboard" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "_wqSAZExy6xV" }, "outputs": [], "source": [ "import tensorflow as tf\n", "import datetime" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "Ao7fJW1Pyiza" }, "outputs": [], "source": [ "# Clear any logs from previous runs\n", "!rm -rf ./logs/ " ] }, { "cell_type": "markdown", "metadata": { "id": "z5pr9vuHVgXY" }, "source": [ "在本例中使用 [MNIST](https://en.wikipedia.org/wiki/MNIST_database) 数据集。接下来编写一个函数对数据进行标准化,同时创建一个简单的Keras模型使图像分为10类。" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "j-DHsby18cot" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz\n", "11493376/11490434 [==============================] - 0s 0us/step\n" ] } ], "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), 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": "XKUjdIoV87um" }, "source": [ "## 通过 Keras Model.fit() 使用 TensorBoard" ] }, { "cell_type": "markdown", "metadata": { "id": "8CL_lxdn8-Sv" }, "source": [ "当使用 Keras's [Model.fit()](https://tensorflow.google.cn/api_docs/python/tf/keras/models/Model#fit) 函数进行训练时, 添加 `tf.keras.callback.TensorBoard` 回调可确保创建和存储日志.另外,在每个时期启用 `histogram_freq=1` 的直方图计算功能(默认情况下处于关闭状态)\n", "\n", "将日志放在带有时间戳的子目录中,以便轻松选择不同的训练运行。" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "WAQThq539CEJ" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Train on 60000 samples, validate on 10000 samples\n", "Epoch 1/5\n", "60000/60000 [==============================] - 15s 246us/sample - loss: 0.2217 - accuracy: 0.9343 - val_loss: 0.1019 - val_accuracy: 0.9685\n", "Epoch 2/5\n", "60000/60000 [==============================] - 14s 229us/sample - loss: 0.0975 - accuracy: 0.9698 - val_loss: 0.0787 - val_accuracy: 0.9758\n", "Epoch 3/5\n", "60000/60000 [==============================] - 14s 231us/sample - loss: 0.0718 - accuracy: 0.9771 - val_loss: 0.0698 - val_accuracy: 0.9781\n", "Epoch 4/5\n", "60000/60000 [==============================] - 14s 227us/sample - loss: 0.0540 - accuracy: 0.9820 - val_loss: 0.0685 - val_accuracy: 0.9795\n", "Epoch 5/5\n", "60000/60000 [==============================] - 14s 228us/sample - loss: 0.0433 - accuracy: 0.9862 - val_loss: 0.0623 - val_accuracy: 0.9823\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 6, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "model = create_model()\n", "model.compile(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(log_dir=log_dir, 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])" ] }, { "cell_type": "markdown", "metadata": { "id": "asjGpmD09dRl" }, "source": [ "通过命令行 (command) 或在 notebook 体验中启动 TensorBoard ,这两个接口通常是相同的。 在 notebooks, 使用 `%tensorboard` 命令。 在命令行中, 运行不带“%”的相同命令。" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "A4UKgTLb9fKI" }, "outputs": [], "source": [ "%tensorboard --logdir logs/fit" ] }, { "cell_type": "markdown", "metadata": { "id": "MCsoUNb6YhGc" }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "id": "Gi4PaRm39of2" }, "source": [ "在此示例中创建的可视化效果的简要概述以及可以找到它们的信息中心(顶部导航栏中的标签页):\n", "\n", "- **标量**显示损失和指标在每个周期如何变化。您还可以使用它们跟踪训练速度、学习率和其他标量值。可以在 **Time Series** 或 **Scalars** 信息中心找到标量。\n", "- **计算图**可以帮助您呈现模型。在这种情况下,将显示层的 Keras 计算图,这可以帮助您确保正确构建。可以在 **Graphs** 信息中心找到计算图。\n", "- **直方图**和**分布**显示张量随时间的分布。这对于呈现权重和偏差并验证它们是否以预期的方式变化很有用。可以在 **Time Series** 或 **Histograms** 信息中心中找到直方图。可以在 **Distributions** 信息中心中找到分布。\n", "\n", "当您记录其他类型的数据时,会自动启用其他 TensorBoard 信息中心。 例如,使用 Keras TensorBoard 回调还可以记录图像和嵌入向量。您可以通过点击右上角的“inactive”下拉列表来查看 TensorBoard 中还有哪些其他信息中心。" ] }, { "cell_type": "markdown", "metadata": { "id": "nB718NOH95yG" }, "source": [ "## 通过其他方法使用 TensorBoard\n" ] }, { "cell_type": "markdown", "metadata": { "id": "IKNt0nWs-Ekt" }, "source": [ "用以下方法训练时,例如 [`tf.GradientTape()`](https://tensorflow.google.cn/api_docs/python/tf/GradientTape), 会使用 `tf.summary` 记录所需的信息。\n", "\n", "使用与上述相同的数据集,但将其转换为 `tf.data.Dataset` 以利用批处理功能:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "nnHx4DsMezy1" }, "outputs": [], "source": [ "train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))\n", "test_dataset = tf.data.Dataset.from_tensor_slices((x_test, y_test))\n", "\n", "train_dataset = train_dataset.shuffle(60000).batch(64)\n", "test_dataset = test_dataset.batch(64)" ] }, { "cell_type": "markdown", "metadata": { "id": "SzpmTmJafJ10" }, "source": [ "训练代码遵循 [advanced quickstart](https://tensorflow.google.cn/tutorials/quickstart/advanced) 教程,但显示了如何将 log 记录到 TensorBoard 。 首先选择损失和优化器:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "H2Y5-aPbAANs" }, "outputs": [], "source": [ "loss_object = tf.keras.losses.SparseCategoricalCrossentropy()\n", "optimizer = tf.keras.optimizers.Adam()" ] }, { "cell_type": "markdown", "metadata": { "id": "cKhIIDj9Hbfy" }, "source": [ "创建可用于在训练期间累积值并在任何时候记录的有状态指标:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "jD0tEWrgH0TL" }, "outputs": [], "source": [ "# Define our metrics\n", "train_loss = tf.keras.metrics.Mean('train_loss', dtype=tf.float32)\n", "train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy('train_accuracy')\n", "test_loss = tf.keras.metrics.Mean('test_loss', dtype=tf.float32)\n", "test_accuracy = tf.keras.metrics.SparseCategoricalAccuracy('test_accuracy')" ] }, { "cell_type": "markdown", "metadata": { "id": "szw_KrgOg-OT" }, "source": [ "定义训练和测试代码:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "TTWcJO35IJgK" }, "outputs": [], "source": [ "def train_step(model, optimizer, x_train, y_train):\n", " with tf.GradientTape() as tape:\n", " predictions = model(x_train, training=True)\n", " loss = loss_object(y_train, predictions)\n", " grads = tape.gradient(loss, model.trainable_variables)\n", " optimizer.apply_gradients(zip(grads, model.trainable_variables))\n", "\n", " train_loss(loss)\n", " train_accuracy(y_train, predictions)\n", "\n", "def test_step(model, x_test, y_test):\n", " predictions = model(x_test)\n", " loss = loss_object(y_test, predictions)\n", "\n", " test_loss(loss)\n", " test_accuracy(y_test, predictions)" ] }, { "cell_type": "markdown", "metadata": { "id": "nucPZBKPJR3A" }, "source": [ "设置摘要编写器,以将摘要写到另一个日志目录中的磁盘上:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "3Qp-exmbWf4w" }, "outputs": [], "source": [ "current_time = datetime.datetime.now().strftime(\"%Y%m%d-%H%M%S\")\n", "train_log_dir = 'logs/gradient_tape/' + current_time + '/train'\n", "test_log_dir = 'logs/gradient_tape/' + current_time + '/test'\n", "train_summary_writer = tf.summary.create_file_writer(train_log_dir)\n", "test_summary_writer = tf.summary.create_file_writer(test_log_dir)" ] }, { "cell_type": "markdown", "metadata": { "id": "qgUJgDdKWUKF" }, "source": [ "开始训练。使用 `tf.summary.scalar()` 在摘要编写器范围内的训练/测试期间记录指标(损失和准确率)以将摘要写入磁盘。您可以控制记录哪些指标以及记录的频率。其他 `tf.summary` 函数可以记录其他类型的数据。" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "odWvHPpKJvb_" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Epoch 1, Loss: 0.24321186542510986, Accuracy: 92.84333801269531, Test Loss: 0.13006582856178284, Test Accuracy: 95.9000015258789\n", "Epoch 2, Loss: 0.10446818172931671, Accuracy: 96.84833526611328, Test Loss: 0.08867532759904861, Test Accuracy: 97.1199951171875\n", "Epoch 3, Loss: 0.07096975296735764, Accuracy: 97.80166625976562, Test Loss: 0.07875105738639832, Test Accuracy: 97.48999786376953\n", "Epoch 4, Loss: 0.05380449816584587, Accuracy: 98.34166717529297, Test Loss: 0.07712937891483307, Test Accuracy: 97.56999969482422\n", "Epoch 5, Loss: 0.041443776339292526, Accuracy: 98.71833038330078, Test Loss: 0.07514958828687668, Test Accuracy: 97.5\n" ] } ], "source": [ "model = create_model() # reset our model\n", "\n", "EPOCHS = 5\n", "\n", "for epoch in range(EPOCHS):\n", " for (x_train, y_train) in train_dataset:\n", " train_step(model, optimizer, x_train, y_train)\n", " with train_summary_writer.as_default():\n", " tf.summary.scalar('loss', train_loss.result(), step=epoch)\n", " tf.summary.scalar('accuracy', train_accuracy.result(), step=epoch)\n", "\n", " for (x_test, y_test) in test_dataset:\n", " test_step(model, x_test, y_test)\n", " with test_summary_writer.as_default():\n", " tf.summary.scalar('loss', test_loss.result(), step=epoch)\n", " tf.summary.scalar('accuracy', test_accuracy.result(), step=epoch)\n", " \n", " template = 'Epoch {}, Loss: {}, Accuracy: {}, Test Loss: {}, Test Accuracy: {}'\n", " print (template.format(epoch+1,\n", " train_loss.result(), \n", " train_accuracy.result()*100,\n", " test_loss.result(), \n", " test_accuracy.result()*100))\n", "\n", " # Reset metrics every epoch\n", " train_loss.reset_states()\n", " test_loss.reset_states()\n", " train_accuracy.reset_states()\n", " test_accuracy.reset_states()" ] }, { "cell_type": "markdown", "metadata": { "id": "JikosQ84fzcA" }, "source": [ "再次打开 TensorBoard,这次将其指向新的日志目录。 我们也可以启动 TensorBoard 来监视训练进度。" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "-Iue509kgOyE" }, "outputs": [], "source": [ "%tensorboard --logdir logs/gradient_tape" ] }, { "cell_type": "markdown", "metadata": { "id": "NVpnilhEgQXk" }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "id": "ozbwXgPIkCKV" }, "source": [ "您现在已经了解了如何通过 Keras 回调和通过 `tf.summary` 使用 TensorBoard 来实现更多自定义场景。 " ] }, { "cell_type": "markdown", "metadata": { "id": "vsowjhkBdkbK" }, "source": [ "## TensorBoard.dev:托管并共享您的机器学习实验结果\n", "\n", "[TensorBoard.dev](https://tensorboard.dev) 是一项免费的公共服务,可让您上传您的 TensorBoard 日志并获得可在学术论文、博文、社交媒体等中与所有人共享的永久链接。这有助于实现更好的重现性和协作。\n", "\n", "要使用 TensorBoard.dev,请运行以下命令:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "Q3nupQL24E5E" }, "outputs": [], "source": [ "!tensorboard dev upload \\\n", " --logdir logs/fit \\\n", " --name \"(optional) My latest experiment\" \\\n", " --description \"(optional) Simple comparison of several hyperparameters\" \\\n", " --one_shot" ] }, { "cell_type": "markdown", "metadata": { "id": "lAgEh_Ow4EX6" }, "source": [ "请注意,此调用使用感叹号前缀 (`!`) 来调用 shell,而不是使用百分比前缀 (`%`) 来调用 colab 魔法。从命令行调用此命令时,不需要任何前缀。\n", "\n", "在[此处](https://tensorboard.dev/experiment/EDZb7XgKSBKo6Gznh3i8hg/#scalars)查看示例。\n", "\n", "要了解如何使用 TensorBoard.dev 的更多详细信息,请参阅 https://tensorboard.dev/#get-started" ] } ], "metadata": { "colab": { "collapsed_sections": [], "name": "get_started.ipynb", "toc_visible": true }, "kernelspec": { "display_name": "Python 3", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 0 }