Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tensorflow
GitHub Repository: tensorflow/docs-l10n
Path: blob/master/site/ja/tensorboard/text_summaries.ipynb
25115 views
Kernel: Python 3
#@title Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License.

概要

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

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'

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