TensorBoard Tutorial: TensorFlow Graph Visualization
โก Smart Summary
TensorBoard is the visual front end for TensorFlow training runs, plotting loss curves, model graphs, weight histograms and embeddings. This walkthrough writes event files with an estimator, then opens the dashboard on port 6006.
What is TensorBoard?
TensorBoard is the interface used to visualize the graph and other tools to understand, debug, and optimize the model. It is a tool that provides measurements and visualizations for a machine learning workflow. It helps track metrics such as loss and accuracy, visualize the model graph, and project embeddings into lower-dimensional spaces.
TensorBoard ships with TensorFlow and runs as a small local web server that reads the event files a training job writes to disk.
TensorFlow Graph Visualization using TensorBoard Example
The image below comes from the TensorBoard graph you will generate in this tutorial. It is the main panel:
From the picture below, you can see the panel of TensorBoard graph visualization. The panel contains different tabs, which are linked to the level of information you add when you run the model.
- Scalars: Show different useful information during the model training
- Graphs: Show the model
- Histogram: Display weights with a histogram
- Distribution: Display the distribution of the weight
- Projector: Show Principal component analysis and T-SNE algorithm. The technique used for dimensionality reduction
During this TensorBoard tutorial, you will train a simple deep learning model, and the graph it produces is read from the bottom up.
If you look at the graph, you can understand how the model works. The numbered badges in the screenshot below mark these four stages:
- Enqueue the data to the model: Push an amount of data equal to the batch size to the model, i.e., Number of data feed after each iteration
- Feed the data to the Tensors
- Train the model
- Display the number of batches during the training. Save the model on the disk.
The basic idea behind TensorBoard is that a neural network can behave like a black box, and you need a tool to inspect what is inside that box. Think of TensorBoard as a flashlight for diving into the network.
It helps to understand the dependencies between operations, how the weights are computed, displays the loss function and much other useful information. When you bring all these pieces of information together, you have a great tool to debug and find how to improve the model.
To give you an idea of how useful the TensorBoard graph can be, look at the two loss curves below:
A neural network decides how to connect the different neurons and how many layers are needed before the model can predict an outcome. Once the architecture is defined, you not only need to train the model but also a metric that measures the accuracy of the prediction. That metric is called a loss function, and the objective is to minimize it, which simply means the model makes fewer errors.
Every training algorithm repeats the computation many times until the loss reaches a flatter line. Minimizing the loss also requires a learning rate, which is the speed at which the model learns. Set the learning rate too high and the model never has time to learn anything: that is the left-hand chart, where the line moves up and down because the model is effectively guessing. The chart on the right shows the loss decreasing over the iterations until the curve flattens, which means the model has found a solution.
TensorBoard is a great tool to visualize such metrics and highlight potential issues. A neural network can take hours or weeks before it finds a solution, and TensorBoard refreshes the metrics while the job is still running. You therefore do not need to wait until the end to see whether the model trains correctly: open TensorBoard, check how the training is going, and make the appropriate change if necessary.
How to Use TensorBoard?
In this section, you will learn how to open TensorBoard from the terminal on macOS and from the command line on Windows. The focus here is the tool rather than the model, so the training code is kept deliberately short.
First, you need to import the libraries you will use during the training.
## Import the library import tensorflow as tf import numpy as np
You create the data. It is an array of 10000 rows and 5 columns.
X_train = (np.random.sample((10000,5))) y_train = (np.random.sample((10000,1))) X_train.shape
Output
(10000, 5)
The code below transforms the data and creates the model.
Note that the learning rate is equal to 0.1. If you change this rate to a higher value, the model will not find a solution. This is what happened on the left side of the picture above.
The example uses a TensorFlow estimator, the high-level API that wraps the mathematical computations. Estimators belong to the TensorFlow 1.x API, so the two snippets below run under a 1.x install or through the tf.compat.v1 namespace.
To create the log files, you need to specify the path. This is done with the argument model_dir.
In the TensorBoard example below, you store the model inside the working directory, i.e., where you store the notebook or Python file. Inside this path, TensorFlow will create a folder called train with a child folder named linreg.
feature_columns = [
tf.feature_column.numeric_column('x', shape=X_train.shape[1:])]
DNN_reg = tf.estimator.DNNRegressor(feature_columns=feature_columns,
# Indicate where to store the log file
model_dir='train/linreg',
hidden_units=[500, 300],
optimizer=tf.train.ProximalAdagradOptimizer(
learning_rate=0.1,
l1_regularization_strength=0.001
)
)
Output
INFO:tensorflow:Using default config.
INFO:tensorflow:Using config: {'_model_dir': 'train/linreg', '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_save_checkpoints_secs': 600, '_session_config': None, '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_train_distribute': None, '_service': None, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x1818e63828>, '_task_type': 'worker', '_task_id': 0, '_global_id_in_cluster': 0, '_master': '', '_evaluation_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas': 1}
The last step of this TensorFlow visualize graph example consists of training the model. During the training, TensorFlow writes information into the model directory.
# Train the estimator train_input = tf.estimator.inputs.numpy_input_fn( x={"x": X_train}, y=y_train, shuffle=False,num_epochs=None) DNN_reg.train(train_input,steps=3000)
Output
INFO:tensorflow:Calling model_fn. INFO:tensorflow:Done calling model_fn. INFO:tensorflow:Create CheckpointSaverHook. INFO:tensorflow:Graph was finalized. INFO:tensorflow:Running local_init_op. INFO:tensorflow:Done running local_init_op. INFO:tensorflow:Saving checkpoints for 1 into train/linreg/model.ckpt. INFO:tensorflow:loss = 40.060104, step = 1 INFO:tensorflow:global_step/sec: 197.061 INFO:tensorflow:loss = 10.62989, step = 101 (0.508 sec) INFO:tensorflow:global_step/sec: 172.487 INFO:tensorflow:loss = 11.255318, step = 201 (0.584 sec) INFO:tensorflow:global_step/sec: 193.295 INFO:tensorflow:loss = 10.604872, step = 301 (0.513 sec) INFO:tensorflow:global_step/sec: 175.378 INFO:tensorflow:loss = 10.090343, step = 401 (0.572 sec) INFO:tensorflow:global_step/sec: 209.737 INFO:tensorflow:loss = 10.057928, step = 501 (0.476 sec) INFO:tensorflow:global_step/sec: 171.646 INFO:tensorflow:loss = 10.460144, step = 601 (0.583 sec) INFO:tensorflow:global_step/sec: 192.269 INFO:tensorflow:loss = 10.529617, step = 701 (0.519 sec) INFO:tensorflow:global_step/sec: 198.264 INFO:tensorflow:loss = 9.100082, step = 801 (0.504 sec) INFO:tensorflow:global_step/sec: 226.842 INFO:tensorflow:loss = 10.485607, step = 901 (0.441 sec) INFO:tensorflow:global_step/sec: 152.929 INFO:tensorflow:loss = 10.052481, step = 1001 (0.655 sec) INFO:tensorflow:global_step/sec: 166.745 INFO:tensorflow:loss = 11.320213, step = 1101 (0.600 sec) INFO:tensorflow:global_step/sec: 161.854 INFO:tensorflow:loss = 9.603306, step = 1201 (0.619 sec) INFO:tensorflow:global_step/sec: 179.074 INFO:tensorflow:loss = 11.110269, step = 1301 (0.556 sec) INFO:tensorflow:global_step/sec: 202.776 INFO:tensorflow:loss = 11.929443, step = 1401 (0.494 sec) INFO:tensorflow:global_step/sec: 144.161 INFO:tensorflow:loss = 11.951693, step = 1501 (0.694 sec) INFO:tensorflow:global_step/sec: 154.144 INFO:tensorflow:loss = 8.620987, step = 1601 (0.649 sec) INFO:tensorflow:global_step/sec: 151.094 INFO:tensorflow:loss = 10.666125, step = 1701 (0.663 sec) INFO:tensorflow:global_step/sec: 193.644 INFO:tensorflow:loss = 11.0349865, step = 1801 (0.516 sec) INFO:tensorflow:global_step/sec: 189.707 INFO:tensorflow:loss = 9.860596, step = 1901 (0.526 sec) INFO:tensorflow:global_step/sec: 176.423 INFO:tensorflow:loss = 10.695, step = 2001 (0.567 sec) INFO:tensorflow:global_step/sec: 213.066 INFO:tensorflow:loss = 10.426752, step = 2101 (0.471 sec) INFO:tensorflow:global_step/sec: 220.975 INFO:tensorflow:loss = 10.594796, step = 2201 (0.452 sec) INFO:tensorflow:global_step/sec: 219.289 INFO:tensorflow:loss = 10.4212265, step = 2301 (0.456 sec) INFO:tensorflow:global_step/sec: 215.123 INFO:tensorflow:loss = 9.668612, step = 2401 (0.465 sec) INFO:tensorflow:global_step/sec: 175.65 INFO:tensorflow:loss = 10.009649, step = 2501 (0.569 sec) INFO:tensorflow:global_step/sec: 206.962 INFO:tensorflow:loss = 10.477722, step = 2601 (0.483 sec) INFO:tensorflow:global_step/sec: 229.627 INFO:tensorflow:loss = 9.877638, step = 2701 (0.435 sec) INFO:tensorflow:global_step/sec: 195.792 INFO:tensorflow:loss = 10.274586, step = 2801 (0.512 sec) INFO:tensorflow:global_step/sec: 176.803 INFO:tensorflow:loss = 10.061047, step = 2901 (0.566 sec) INFO:tensorflow:Saving checkpoints for 3000 into train/linreg/model.ckpt. INFO:tensorflow:Loss for final step: 10.73032. <tensorflow.python.estimator.canned.dnn.DNNRegressor at 0x1818e63630>
On TensorFlow 2 the same job is done by the Keras TensorBoard callback, which writes the graph and the per-epoch metrics without any estimator code:
logdir = "logs/fit/" + datetime.now().strftime("%Y%m%d-%H%M%S") tensorboard_callback = keras.callbacks.TensorBoard(log_dir=logdir) model.fit(train_images, train_labels, epochs=5, callbacks=[tensorboard_callback])
Once the run finishes, the log directory holds a checkpoint file, a graph.pbtxt file and one or more events.out.tfevents files โ exactly what TensorBoard reads.
For macOS users
The Finder view below shows the new train/linreg folder created inside the working directory.
For Windows users
On Windows the same files appear under the path you passed to model_dir, as highlighted in the address bar.
The same event-file format is produced by PyTorch, so the dashboard is not limited to TensorFlow runs.
Now that you have the log events written, you can open TensorBoard. TensorBoard serves on port 6006 by default (Jupyter uses port 8888). Use the Terminal on macOS or the Anaconda prompt on Windows.
For macOS users
# Different for you
cd /Users/Guru99/tuto_TF
source activate hello-tf!
The notebook is stored in the path /Users/Guru99/tuto_TF
For Windows users
cd C:\Users\Admin\Anaconda3 activate hello-tf
The notebook is stored in the path C:\Users\Admin\Anaconda3
To launch TensorBoard, run the following command from that directory.
For macOS users
tensorboard --logdir=./train/linreg
For Windows users
tensorboard --logdir=.\train\linreg
TensorBoard is then served at http://localhost:6006
| Flag | What it controls |
|---|---|
--logdir |
Directory holding the event files. This is the only required flag. |
--port |
Port to serve on. The default is 6006; change it when an earlier session still holds that port. |
--bind_all |
Serve on every network interface. TensorBoard binds to localhost only by default, so remote access needs this flag, and it cannot be combined with --host. |
--reload_interval |
How often, in seconds, the log directory is rescanned for new data. |
--logdir_spec |
Legacy fallback accepting several comma-separated paths; --logdir no longer supports that form. |
The tensorboard dev subcommand no longer works: the hosted TensorBoard.dev service shut down on 1 January 2024. Local use is unaffected.
On some machines the console prints the host name instead of localhost, as in the Anaconda prompt below. Either address opens the same dashboard.
Copy and paste the address into your favourite browser. You should see the Scalars dashboard shown below.
If you see something like this instead:
It means TensorBoard cannot find the log file. Make sure you point cd to the right path, or double-check that the log event was actually created. If it was not, re-run the training code.
If you want to close TensorBoard, press CTRL+C in the terminal window.
Hat tip: check your Anaconda prompt for the current working directory.
In the screenshot above the prompt sits at C:\Users\Admin, so the log file should be created under that folder.










