Path: blob/master/site/en-snapshot/tensorboard/get_started.ipynb
25115 views
Copyright 2019 The TensorFlow Authors.
Get started with TensorBoard
In machine learning, to improve something you often need to be able to measure it. TensorBoard is a tool for providing the measurements and visualizations needed during the machine learning workflow. It enables tracking experiment metrics like loss and accuracy, visualizing the model graph, projecting embeddings to a lower dimensional space, and much more.
This quickstart will show how to quickly get started with TensorBoard. The remaining guides in this website provide more details on specific capabilities, many of which are not included here.
Using the MNIST dataset as the example, normalize the data and write a function that creates a simple Keras model for classifying the images into 10 classes.
Using TensorBoard with Keras Model.fit()
When training with Keras's Model.fit(), adding the tf.keras.callbacks.TensorBoard
callback ensures that logs are created and stored. Additionally, enable histogram computation every epoch with histogram_freq=1
(this is off by default)
Place the logs in a timestamped subdirectory to allow easy selection of different training runs.
Start TensorBoard through the command line or within a notebook experience. The two interfaces are generally the same. In notebooks, use the %tensorboard
line magic. On the command line, run the same command without "%".
A brief overview of the visualizations created in this example and the dashboards (tabs in top navigation bar) where they can be found:
Scalars show how the loss and metrics change with every epoch. You can use them to also track training speed, learning rate, and other scalar values. Scalars can be found in the Time Series or Scalars dashboards.
Graphs help you visualize your model. In this case, the Keras graph of layers is shown which can help you ensure it is built correctly. Graphs can be found in the Graphs dashboard.
Histograms and Distributions show the distribution of a Tensor over time. This can be useful to visualize weights and biases and verify that they are changing in an expected way. Histograms can be found in the Time Series or Histograms dashboards. Distributions can be found in the Distributions dashboard.
Additional TensorBoard dashboards are automatically enabled when you log other types of data. For example, the Keras TensorBoard callback lets you log images and embeddings as well. You can see what other dashboards are available in TensorBoard by clicking on the "inactive" dropdown towards the top right.
Using TensorBoard with other methods
When training with methods such as tf.GradientTape()
, use tf.summary
to log the required information.
Use the same dataset as above, but convert it to tf.data.Dataset
to take advantage of batching capabilities:
The training code follows the advanced quickstart tutorial, but shows how to log metrics to TensorBoard. Choose loss and optimizer:
Create stateful metrics that can be used to accumulate values during training and logged at any point:
Define the training and test functions:
Set up summary writers to write the summaries to disk in a different logs directory:
Start training. Use tf.summary.scalar()
to log metrics (loss and accuracy) during training/testing within the scope of the summary writers to write the summaries to disk. You have control over which metrics to log and how often to do it. Other tf.summary
functions enable logging other types of data.
Open TensorBoard again, this time pointing it at the new log directory. We could have also started TensorBoard to monitor training while it progresses.
That's it! You have now seen how to use TensorBoard both through the Keras callback and through tf.summary
for more custom scenarios.
TensorBoard.dev: Host and share your ML experiment results
TensorBoard.dev is a free public service that enables you to upload your TensorBoard logs and get a permalink that can be shared with everyone in academic papers, blog posts, social media, etc. This can enable better reproducibility and collaboration.
To use TensorBoard.dev, run the following command:
Note that this invocation uses the exclamation prefix (!
) to invoke the shell rather than the percent prefix (%
) to invoke the colab magic. When invoking this command from the command line there is no need for either prefix.
View an example here.
For more details on how to use TensorBoard.dev, see https://tensorboard.dev/#get-started