Visualizing your deep learning features using TensorBoard

Aditya Dutt
4 min readMar 8, 2021

--

Introduction

TensorBoard is a tool that provides measurements and visualizations needed during machine learning workflow.

In this tutorial, I will give a quick tutorial on how to visualize your feature vectors on TensorBoard. Using a machine learning model, sometimes we want to learn embeddings of different classes. Embeddings are a way to map different inputs like (images, text, etc) to multi-dimensional vectors. Using TensorBoard, you can visualize the representations or feature vectors. You can also visualize the input data directly. TensorBoard also provides dimensionality reduction methods like PCA and t-SNE. You can easily apply PCA and t-SNE to your data with just one click and see visualizations both in 2D and 3D.

What is the Problem Statement?

We have a small dataset of different shapes (circle, triangle, rectangles) of different colors (red, green, blue). Let’s say we want to classify shapes based on colors. So, there are 3 target outputs: red, green, and blue. Using a machine learning model, we have learned a representation/ embedding for colors while classifying them. Now, we want to see how effective those representations are?

So, we will visualize these embeddings on TensorBoard.

Below is the dataset that is used.

Requirements

Note: We will use tensorflow.contrib, that’s why version 1.13.2 is required. In version 2.0, it does not exist anymore.

Implementation

  1. Create a visualization without sprites.

Sprites are blocks of several images arranged in form of a grid. Check out the image below for an example. Sprites make the visualization more impressive and intuitive.

Firstly, I will go through each step to create the code. After this, the complete code is also available. You can directly jump to that if you just want to use it.

Step 1: Firstly, import the libraries.

Step 2: Setup metadata, model, and checkpoint paths.

Step 3: Setup file writer and configurations.

Step 4: Write data to plot in CKPT format

Step 5: Write metadata in TSV format

In the TSV file, labels are written corresponding to the data indices. After this step, we are ready to plot.

Here is the complete code:

Steps to run:

  1. Open terminal and type: python visualize_without_sprite.py
  2. Now, type: tensorboard — logdir #YOUR_LOG_DIR_PATH# — host=127.0.0.1

Now, open the localhost on the web browser. And, you will see TensorBoard now.

Output visualization on TensorBoard is shown below.

Visualization on Tensorboard without sprites. Each class is shown in a different color.

2. Create a visualization with sprites.

Everything is the same except the part where we add sprites. We add the following lines to create the sprites.

In line 4, we set up the sprites’ path also now. In line 6, we set the shape of a single image to display.

In line 9, we set up the variable which contains the input images to be visualized.

In line 20, we create an empty template for all sprites images. And, from line 22, we set an image in each plot. (It’s similar to what we do to create subplots in Matplotlib).

Follow the same steps to run the code.

Here is the complete code.

Output visualization with sprites is shown below. You can also see images of triangles and squares instead of a scatter plot of dots now.

Our goal was to classify images of different colors. And, you can see the same color shapes are clustered together.

Visualizing features with sprites. You can see the original input images in the scatter plot now.

You can also apply T-SNE to data.

Demo of using T-SNE on TensorBoard

Now, we know how to visualize features using TensorBoard.

Conclusion

I have demonstrated how to visualize features embedding on Tensorboard with and without sprites. You can check out the full code on my GitHub profile.

Thanks for reading! I hope it’s helpful.

If you enjoyed this article please recommend and share.

--

--

Aditya Dutt
Aditya Dutt

Written by Aditya Dutt

Machine Learning PhD Student at University of Florida (he/him) https://adityadutt.github.io/

Responses (1)