TensorFlow.orgで表示 | GoogleColabで実行 | GitHubでソースを表示 | ノートブックをダウンロード |
概要
TensorFlowテキストの要約APIを使って、簡単に任意のテキストを記録し、TensorBoardでそれを見ることができます。これは、入力データをサンプリングして調べたり、実行メタデータや生成されたテキストを記録したりするのに非常に役立ちます。モデル開発の過程で役立つテキストとして診断データをログに記録することもできます。
このチュートリアルでは、Text SummaryAPIのいくつかの基本的なユースケースを試してみます。
設定
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
1つのテキストをログに記録する
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'
マークダウンの解釈
TensorBoardは、テキストの要約を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
TensorFlow.orgで表示
GoogleColabで実行
GitHubでソースを表示
ノートブックをダウンロード