How to use tensorboard Embedding Projector?

Bruno Oliveira picture Bruno Oliveira · Nov 28, 2016 · Viewed 34.5k times · Source

How do I use the Embedding Projector included in Tensorboard?

I can't find any documentation for it. There are some references to it here, but there's no step-by-step example/tutorial on how to use it.

Answer

enezhadian picture enezhadian · Dec 28, 2016

As far as I am aware this is the only documentation about embedding visualization on the TensorFlow website. Though the code snippet might not be very instructive for the first time users, so here is an example usage:

import os
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

LOG_DIR = 'logs'

mnist = input_data.read_data_sets('MNIST_data')
images = tf.Variable(mnist.test.images, name='images')

with tf.Session() as sess:
    saver = tf.train.Saver([images])

    sess.run(images.initializer)
    saver.save(sess, os.path.join(LOG_DIR, 'images.ckpt'))

Here first we create a TensoFlow variable (images) and then save it using tf.train.Saver. After executing the code we can launch TensorBoard by issuing tensorboard --logdir=logs command and opening localhost:6006 in a browser.

PCA Visualization However this visualisation is not very helpful because we do not see different classes to which each data point belongs. In order to distinguish each class from another one should provider some metadata:

import os
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.contrib.tensorboard.plugins import projector


LOG_DIR = 'logs'
metadata = os.path.join(LOG_DIR, 'metadata.tsv')

mnist = input_data.read_data_sets('MNIST_data')
images = tf.Variable(mnist.test.images, name='images')

with open(metadata, 'w') as metadata_file:
    for row in mnist.test.labels:
        metadata_file.write('%d\n' % row)

with tf.Session() as sess:
    saver = tf.train.Saver([images])

    sess.run(images.initializer)
    saver.save(sess, os.path.join(LOG_DIR, 'images.ckpt'))

    config = projector.ProjectorConfig()
    # One can add multiple embeddings.
    embedding = config.embeddings.add()
    embedding.tensor_name = images.name
    # Link this tensor to its metadata file (e.g. labels).
    embedding.metadata_path = metadata
    # Saves a config file that TensorBoard will read during startup.
    projector.visualize_embeddings(tf.summary.FileWriter(LOG_DIR), config)

Which gives us:

enter image description here