在 TensorBoard 中顯示文字資料

在 TensorFlow.org 上檢視 在 Google Colab 中執行 在 GitHub 上檢視原始碼 下載筆記本

總覽

透過使用 TensorFlow Text Summary API,您可以輕鬆記錄任意文字並在 TensorBoard 中檢視。這對於取樣和檢查您的輸入資料,或記錄執行中繼資料或產生的文字非常有幫助。您也可以將診斷資料記錄為文字,這在模型開發過程中會很有幫助。

在本教學課程中,您將嘗試一些 Text Summary API 的基本使用案例。

設定

try:
  # %tensorflow_version only exists in Colab.
  %tensorflow_version 2.x
except Exception:
  pass

# Load the TensorBoard notebook extension.
%load_ext tensorboard
import tensorflow as tf

from datetime import datetime
import json
from packaging import version
import tempfile

print("TensorFlow version: ", tf.__version__)
assert version.parse(tf.__version__).release[0] >= 2, \
    "This notebook requires TensorFlow 2.0 or above."
TensorFlow version:  2.5.0-dev20210219

記錄單段文字

為了瞭解 Text Summary API 的運作方式,您將簡單記錄一些文字,並查看它在 TensorBoard 中的呈現方式。

my_text = "Hello world! 😃"
# Clear out any prior log data.
!rm -rf logs

# Sets up a timestamped log directory.
logdir = "logs/text_basics/" + datetime.now().strftime("%Y%m%d-%H%M%S")
# Creates a file writer for the log directory.
file_writer = tf.summary.create_file_writer(logdir)

# Using the file writer, log the text.
with file_writer.as_default():
  tf.summary.text("first_text", my_text, step=0)

現在,使用 TensorBoard 檢查文字。請稍候幾秒鐘,讓 UI 啟動。

%tensorboard --logdir logs

組織多個文字串流

如果您有多個文字串流,您可以將它們放在不同的命名空間中,以便像純量或其他資料一樣協助組織它們。

請注意,如果您在許多步驟中記錄文字,TensorBoard 將對步驟進行子取樣以進行顯示,以便讓呈現方式易於管理。您可以使用 --samples_per_plugin 標記控制取樣率。

# Sets up a second directory to not overwrite the first one.
logdir = "logs/multiple_texts/" + datetime.now().strftime("%Y%m%d-%H%M%S")
# Creates a file writer for the log directory.
file_writer = tf.summary.create_file_writer(logdir)

# Using the file writer, log the text.
with file_writer.as_default():
  with tf.name_scope("name_scope_1"):
    for step in range(20):
      tf.summary.text("a_stream_of_text", f"Hello from step {step}", step=step)
      tf.summary.text("another_stream_of_text", f"This can be kept separate {step}", step=step)
  with tf.name_scope("name_scope_2"):
    tf.summary.text("just_from_step_0", "This is an important announcement from step 0", step=0)
%tensorboard --logdir logs/multiple_texts --samples_per_plugin 'text=5'

Markdown 解譯

TensorBoard 將文字摘要解譯為 Markdown,因為豐富的格式設定可以讓您記錄的資料更易於閱讀和理解,如下所示。(如果您不想要 Markdown 解譯,請參閱這個問題以瞭解停用解譯的解決方法。)

# Sets up a third timestamped log directory under "logs"
logdir = "logs/markdown/" + datetime.now().strftime("%Y%m%d-%H%M%S")
# Creates a file writer for the log directory.
file_writer = tf.summary.create_file_writer(logdir)

some_obj_worth_noting = {
  "tfds_training_data": {
      "name": "mnist",
      "split": "train",
      "shuffle_files": "True",
  },
  "keras_optimizer": {
      "name": "Adagrad",
      "learning_rate": "0.001",
      "epsilon": 1e-07,
  },
  "hardware": "Cloud TPU",
}


# TODO: Update this example when TensorBoard is released with
# https://github.com/tensorflow/tensorboard/pull/4585
# which supports fenced codeblocks in Markdown.
def pretty_json(hp):
  json_hp = json.dumps(hp, indent=2)
  return "".join("\t" + line for line in json_hp.splitlines(True))

markdown_text = """
### Markdown Text

TensorBoard supports basic markdown syntax, including:

    preformatted code

**bold text**

| and | tables |
| ---- | ---------- |
| among | others |
"""

with file_writer.as_default():
  tf.summary.text("run_params", pretty_json(some_obj_worth_noting), step=0)
  tf.summary.text("markdown_jubiliee", markdown_text, step=0)
%tensorboard --logdir logs/markdown