Tensorflow learning notes (6): embeddings of tensorboard



This paper is based on how to on tensorflow.

Tensorboard is a visualization tool of tensorflow. Embeddings is one of its functions, which is used to explore high-dimensional data in two-dimensional or three-dimensional space.

An embedding is a map from input data to points in Euclidean space.

This article uses MNIST data to explain how to use embeddings.


# -*- coding: utf-8 -*-
#@ Author: Chen Zhiping
# @date: 2017-02-08
# @description: hello world program to set up embedding projector in TensorBoard based on MNIST
# @ref: http://yann.lecun.com/exdb/mnist/, https://www.tensorflow.org/images/mnist_10k_sprite.png

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

LOG_DIR = "log"
IMAGE_NUM = 10000

# Read in MNIST data by utility functions provided by TensorFlow
mnist = input_data.read_data_sets(PATH_TO_MNIST_DATA, one_hot=False)

# Extract target MNIST image data
plot_array = mnist.test.images[:IMAGE_NUM]  # shape: (n_observations, n_features)

# Generate meta data
np.savetxt(os.path.join(LOG_DIR, 'metadata.tsv'), mnist.test.labels[:IMAGE_NUM], fmt='%d')

# Download sprite image
# https://www.tensorflow.org/images/mnist_10k_sprite.png, 100x100 thumbnails
PATH_TO_SPRITE_IMAGE = os.path.join(LOG_DIR, 'mnist_10k_sprite.png')  

# To visualise your embeddings, there are 3 things you need to do:
# 1) Setup a 2D tensor variable(s) that holds your embedding(s)
session = tf.InteractiveSession()
embedding_var = tf.Variable(plot_array, name='embedding')

# 2) Periodically save your embeddings in a LOG_DIR
# Here we just save the Tensor once, so we set global_step to a fixed number
saver = tf.train.Saver()
saver.save(session, os.path.join(LOG_DIR, "model.ckpt"), global_step=0)

# 3) Associate metadata and sprite image with your embedding
# Use the same LOG_DIR where you stored your checkpoint.
summary_writer = tf.summary.FileWriter(LOG_DIR)

config = projector.ProjectorConfig()
# You can add multiple embeddings. Here we add only one.
embedding = config.embeddings.add()
embedding.tensor_name = embedding_var.name
# Link this tensor to its metadata file (e.g. labels).
embedding.metadata_path = os.path.join(LOG_DIR, 'metadata.tsv')
# Link this tensor to its sprite image.
embedding.sprite.image_path = PATH_TO_SPRITE_IMAGE 
embedding.sprite.single_image_dim.extend([28, 28])
# Saves a configuration file that TensorBoard will read during startup.
projector.visualize_embeddings(summary_writer, config)

First, download the image from here and put it in the log directory; then execute the above code; finally, execute the following command to start tensorboard.

tensorboard --logdir=log

After execution, the command line displays the following prompt:

Starting TensorBoard 39 on port 6006
(You can navigate to http://xx.xxx.xx.xxx:6006)

Open the browser, input the above link address, and click theEMBEDDINGSYou can see the effect:

Tensorflow learning notes (6): embeddings of tensorboard


This paper makes a deep research on the visualization of MNIST, which is worth reading carefully.

Recommended Today

Keycloak ~ oidc & oauth2 & custom skin

1 OpenID & OAuth2 & SAMLone point one Relevant information https://github.com/keycloak/keycloak https://www.keycloak.org/docs/latest/server_development https://docs.cbioportal.org/2.2-authorization-and-authentication/authenticating-and-authorizing-users-via-keycloak https://access.redhat.com/documentation/en-us/red_hat_single_sign-on/7.3/html/server_developer_guide/providers https://www.baeldung.com/java-keycloak-custom-user-providers 1.2 OpenIDOpenid is an authentication standard. Many accounts on the Internet support openid, such as Google, Yahoo, paypal and so on.To use openid, users must obtain openid account (such as Google account) in openid identity provider (IDP). Users can use […]