Save the date! Google I/O returns May 18-20 Register now

Visualizing Data using the Embedding Projector in TensorBoard

View on View source on GitHub Download notebook


Using the TensorBoard Embedding Projector, you can graphically represent high dimensional embeddings. This can be helpful in visualizing, examining, and understanding your embedding layers.

Screenshot of the embedding projector

In this tutorial, you will learn how visualize this type of trained layer.


For this tutorial, we will be using TensorBoard to visualize an embedding layer generated for classifying movie review data.

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

%load_ext tensorboard
import os
import tensorflow as tf
import tensorflow_datasets as tfds
from tensorboard.plugins import projector


We will be using a dataset of 25,000 movies reviews from IMDB, labeled by sentiment (positive/negative). Reviews have been preprocessed, and each review is encoded as a sequence of word indexes (integers). For convenience, words are indexed by overall frequency in the dataset, so that for instance the integer "3" encodes the 3rd most frequent word in the data. This allows for quick filtering operations such as: "only consider the top 10,000 most common words, but eliminate the top 20 most common words".

As a convention, "0" does not stand for a specific word, but instead is used to encode any unknown word. Later in the tutorial, we will be removing this row from the visualization.

(train_data, test_data), info = tfds.load(
    split=(tfds.Split.TRAIN, tfds.Split.TEST),
encoder = info.features["text"].encoder

# shuffle and pad the data.
train_batches = train_data.shuffle(1000).padded_batch(
    10, padded_shapes=((None,), ())
test_batches = test_data.shuffle(1000).padded_batch(
    10, padded_shapes=((None,), ())
train_batch, train_labels = next(iter(train_batches))

Keras Embedding Layer

A Keras Embedding Layer can be used to train an embedding for each word in your volcabulary. Each word (or sub-word in this case) will be associated with a 16-dimensional vector (or embedding) that will be trained by the model.

See this tutorial to learn more about word embeddings.

# Create an embedding layer
embedding_dim = 16
embedding = tf.keras.layers.Embedding(encoder.vocab_size, embedding_dim)
# Train this embedding as part of a keras model
model = tf.keras.Sequential(
        embedding, # The embedding layer should be the first layer in a model.
        tf.keras.layers.Dense(16, activation="relu"),

# Compile model

# Train model
history =
    train_batches, epochs=1, validation_data=test_batches, validation_steps=20
2500/2500 [==============================] - 13s 5ms/step - loss: 0.5330 - accuracy: 0.6769 - val_loss: 0.4043 - val_accuracy: 0.7800

Saving data for TensorBoard

TensorBoard reads tensors and metadata from your tensorflow projects from the logs in the specified log_dir directory. For this tutorial, we will be using /logs/imdb-example/.

In order to visualize this data, we will be saving a checkpoint to that directory, along with metadata to understand which layer to visualize.

# Set up a logs directory, so Tensorboard knows where to look for files
if not os.path.exists(log_dir):

# Save Labels separately on a line-by-line manner.
with open(os.path.join(log_dir, 'metadata.tsv'), "w") as f:
  for subwords in encoder.subwords:
  # Fill in the rest of the labels with "unknown"
  for unknown in range(1, encoder.vocab_size - len(encoder.subwords)):
    f.write("unknown #{}\n".format(unknown))

# Save the weights we want to analyse as a variable. Note that the first
# value represents any unknown word, which is not in the metadata, so
# we will remove that value.
weights = tf.Variable(model.layers[0].get_weights()[0][1:])
# Create a checkpoint from embedding, the filename and key are
# name of the tensor.
checkpoint = tf.train.Checkpoint(embedding=weights), "embedding.ckpt"))

# Set up config
config = projector.ProjectorConfig()
embedding = config.embeddings.add()
# The name of the tensor will be suffixed by `/.ATTRIBUTES/VARIABLE_VALUE`
embedding.tensor_name = "embedding/.ATTRIBUTES/VARIABLE_VALUE"
embedding.metadata_path = 'metadata.tsv'
projector.visualize_embeddings(log_dir, config)
%tensorboard --logdir /logs/imdb-example/


The TensorBoard Projector is a great tool for analyzing your data and seeing embedding values relative to each other. The dashboard allows searching for specific terms, and highlights words that are nearby in the embedding space. From this example we can see that Wes Anderson and Alfred Hitchcock are both rather neutral terms, but that they are referenced in different contexts.

Hitchcock is closer associated to words like nightmare, which likely relates to his work in horror movies. While Anderson is closer to the word heart, reflecting his heartwarming style.