{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "djUvWu41mtXa" }, "source": [ "##### Copyright 2021 The TensorFlow Authors." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "cellView": "form", "id": "su2RaORHpReL" }, "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": "NztQK2uFpXT-" }, "source": [ "# 在 TensorBoard 中显示文本数据\n", "\n", "\n", " \n", " \n", " \n", " \n", "
在 TensorFlow.org上查看 在 Google Colab 中运行 在 GitHub 上查看源代码\n", " 下载笔记本\n", "
" ] }, { "cell_type": "markdown", "metadata": { "id": "eDXRFe_qp5C3" }, "source": [ "## 概述\n", "\n", "使用 **TensorFlow Text Summary API**,您可以轻松地在 TensorBoard 中记录任意文本并进行查看。这在采样和检查输入数据,或在记录执行元数据或生成的文本方面非常实用。您还可以记录文本形式的诊断数据,这在模型开发过程中可能会有所帮助。\n", "\n", "在本教程中,您将尝试 Text Summary API 的一些基本用例。" ] }, { "cell_type": "markdown", "metadata": { "id": "dG-nnZK9qW9z" }, "source": [ "## 设置" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "id": "3U5gdCw_nSG3" }, "outputs": [], "source": [ "try:\n", " # %tensorflow_version only exists in Colab.\n", " %tensorflow_version 2.x\n", "except Exception:\n", " pass\n", "\n", "# Load the TensorBoard notebook extension.\n", "%load_ext tensorboard" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "id": "1qIKtOBrqc9Y" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "TensorFlow version: 2.5.0-dev20210219\n" ] } ], "source": [ "import tensorflow as tf\n", "\n", "from datetime import datetime\n", "import json\n", "from packaging import version\n", "import tempfile\n", "\n", "print(\"TensorFlow version: \", tf.__version__)\n", "assert version.parse(tf.__version__).release[0] >= 2, \\\n", " \"This notebook requires TensorFlow 2.0 or above.\"" ] }, { "cell_type": "markdown", "metadata": { "id": "qNsjMY0364j4" }, "source": [ "## 记录一段文本\n", "\n", "为了解 Text Summary API 的工作原理,现在您将在 TensorBoard 中简单记录一小段文本并查看其显示方式。\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "id": "FxMPcdmvBn9t" }, "outputs": [], "source": [ "my_text = \"Hello world! 😃\"" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "id": "IJNpyVyxbVtT" }, "outputs": [], "source": [ "# Clear out any prior log data.\n", "!rm -rf logs\n", "\n", "# Sets up a timestamped log directory.\n", "logdir = \"logs/text_basics/\" + datetime.now().strftime(\"%Y%m%d-%H%M%S\")\n", "# Creates a file writer for the log directory.\n", "file_writer = tf.summary.create_file_writer(logdir)\n", "\n", "# Using the file writer, log the text.\n", "with file_writer.as_default():\n", " tf.summary.text(\"first_text\", my_text, step=0)" ] }, { "cell_type": "markdown", "metadata": { "id": "rngALbRogXe6" }, "source": [ "现在,使用 TensorBoard 检查文本。等待几秒,直至界面出现。" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "id": "T_X-wIy-lD9f" }, "outputs": [], "source": [ "%tensorboard --logdir logs" ] }, { "cell_type": "markdown", "metadata": { "id": "c8n8YqGlT3-c" }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "id": "34enxJjjgWi7" }, "source": [ "## 组织多个文本流\n", "\n", "如果有多个文本流,可以将它们保存在单独的名称空间中,从而便于对其进行组织,就像标量或其他数据一样。\n", "\n", "请注意,如果您在多个步骤中记录文本,TensorBoard 将对要显示的步骤进行二次采样,以便演示内容易于管理。您可以使用 `--samples_per_plugin` 标志控制采样率。" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "id": "dda6960f0119" }, "outputs": [], "source": [ "# Sets up a second directory to not overwrite the first one.\n", "logdir = \"logs/multiple_texts/\" + datetime.now().strftime(\"%Y%m%d-%H%M%S\")\n", "# Creates a file writer for the log directory.\n", "file_writer = tf.summary.create_file_writer(logdir)\n", "\n", "# Using the file writer, log the text.\n", "with file_writer.as_default():\n", " with tf.name_scope(\"name_scope_1\"):\n", " for step in range(20):\n", " tf.summary.text(\"a_stream_of_text\", f\"Hello from step {step}\", step=step)\n", " tf.summary.text(\"another_stream_of_text\", f\"This can be kept separate {step}\", step=step)\n", " with tf.name_scope(\"name_scope_2\"):\n", " tf.summary.text(\"just_from_step_0\", \"This is an important announcement from step 0\", step=0)\n", " " ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "id": "515199f4b547" }, "outputs": [], "source": [ "%tensorboard --logdir logs/multiple_texts --samples_per_plugin 'text=5'" ] }, { "cell_type": "markdown", "metadata": { "id": "bjACE1lAsqUd" }, "source": [ "## Markdown 解释\n", "\n", "TensorBoard 将文本摘要解释为 Markdown,因为多种格式可以使您记录的数据更易于阅读和理解,如下所示。(如果您不需要 Markdown 解释,请参见[本议题](https://github.com/tensorflow/tensorboard/issues/830),了解抑制解释的解决方法。)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "id": "iHUjCXbetIpb" }, "outputs": [], "source": [ "# Sets up a third timestamped log directory under \"logs\"\n", "logdir = \"logs/markdown/\" + datetime.now().strftime(\"%Y%m%d-%H%M%S\")\n", "# Creates a file writer for the log directory.\n", "file_writer = tf.summary.create_file_writer(logdir)\n", "\n", "some_obj_worth_noting = {\n", " \"tfds_training_data\": {\n", " \"name\": \"mnist\",\n", " \"split\": \"train\",\n", " \"shuffle_files\": \"True\",\n", " },\n", " \"keras_optimizer\": {\n", " \"name\": \"Adagrad\",\n", " \"learning_rate\": \"0.001\",\n", " \"epsilon\": 1e-07,\n", " },\n", " \"hardware\": \"Cloud TPU\",\n", "}\n", "\n", "\n", "# TODO: Update this example when TensorBoard is released with\n", "# https://github.com/tensorflow/tensorboard/pull/4585\n", "# which supports fenced codeblocks in Markdown.\n", "def pretty_json(hp):\n", " json_hp = json.dumps(hp, indent=2)\n", " return \"\".join(\"\\t\" + line for line in json_hp.splitlines(True))\n", "\n", "markdown_text = \"\"\"\n", "### Markdown Text\n", "\n", "TensorBoard supports basic markdown syntax, including:\n", "\n", " preformatted code\n", "\n", "**bold text**\n", "\n", "| and | tables |\n", "| ---- | ---------- |\n", "| among | others |\n", "\"\"\"\n", "\n", "with file_writer.as_default():\n", " tf.summary.text(\"run_params\", pretty_json(some_obj_worth_noting), step=0)\n", " tf.summary.text(\"markdown_jubiliee\", markdown_text, step=0)\n", " " ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "id": "57082d8d6839" }, "outputs": [], "source": [ "%tensorboard --logdir logs/markdown" ] } ], "metadata": { "colab": { "collapsed_sections": [], "name": "text_summaries.ipynb", "toc_visible": true }, "kernelspec": { "display_name": "Python 3", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 0 }