Tensorboard的PROJECTOR有着很美好的功能,它能够帮助我们查看网络中数据在二维、三维下的效果,但是一些Embedding示例过于简单,并没有网络的训练过程,所以将这些示例的用法用于真实模型训练时,会出现各种问题,导致Tensorboard中的PROJECTPR版块总是空白,显示不出写入的Embedding,归根到底还是对Tensorboard的Embedding机制不太了解,经过不停的摸索,总算初步掌握了Embedding的用法,以下将带大家深入了解Tensorflow Embedding.
Tensorflow的PROJECTOR包含三个过程:1.获得数据 2.获得数据标签 3.获得数据图像。以下将通过一个mnist的简单示例来解释(代码汇总请见最下面),先上一个效果图:
首先,我们要知道Tensorboard中显示的PROJECTOR数据是从saver()保存的张量数据中取出,所以在Embedding机制中通过一个tf.Variable变量(即下面的embedding_var)来存储需要投影的图像或者数据,为什么用tf.Variable呢?因为saver()主要保存的是网络的权值,而这些权值基本上都是由tf.Variable保存的,而像占位符tf.placeholder则不会保存,因为如果需要保存的话,那么所占内存就太大了,所以在Embedding机制中通过一个tf.Variable变量来存储需要投影的图像或者数据。-------而既然是tf.Variable,那么就需要用session初始化,所以embedding_var必须在sess.run(tf.global_variables_initializer())和saver.save()之前。对应代码如下:
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import os
from tensorflow.contrib.tensorboard.plugins import projector
from tensorflow.examples.tutorials.mnist import input_data
LOG_DIR = 'minimalsample'
NAME_TO_VISUALISE_VARIABLE = "mnistembedding"
TO_EMBED_COUNT = 500
mnist = input_data.read_data_sets("MNIST_data/", one_hot=False)
batch_xs, batch_ys = mnist.train.next_batch(TO_EMBED_COUNT)
embedding_var = tf.Variable(batch_xs, name=NAME_TO_VISUALISE_VARIABLE)
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver()
saver.save(sess, os.path.join(LOG_DIR, "model.ckpt"), 1)
以上就算是Embedding的最主要的部分了,通过Variable保存数据,然后Tensorboard就可以从saver()保存的ckpt文件中获得数据了。另外我们还要注册一个Projector,这样Tensorboard才显示Projector,而注册Projector则需要Embedding的信息,比如嵌入的变量名、tsv文件路径(下有解释)、sprite图像路径(下有解释)等。然后将写入tf.summary.FileWriter中就相当于是注册了projector了。代码如下:
config = projector.ProjectorConfig()
embedding = config.embeddings.add()
embedding.tensor_name = embedding_var.name
# Specify where you find the metadata
embedding.metadata_path = 'metadata.tsv' #'metadata.tsv'
# Specify where you find the sprite (we will create this later)
embedding.sprite.image_path = 'mnistdigits.png' #'mnistdigits.png'
embedding.sprite.single_image_dim.extend([28,28])
# Say that you want to visualise the embeddings
projector.visualize_embeddings(summary_writer, config)
这样Tensorboard就会显示Embedding的投影PROJECTOR了。
Tensorboard要有显示,这里涉及到两个文件:1.tsv文件 2.sprite图像。 在以下示例中是metadata.tsv和mnistdigits.png。
其中metadata.tsv生成如下:
with open(path_for_mnist_metadata,'w') as f:
f.write("Index\tLabel\n")
for index,label in enumerate(batch_ys):
f.write("%d\t%d\n" % (index,label))
可以很容易的看出metadata包含了数据的索引值和对应的标签,而这里的索引值和标签对应的embedding_var中数据的索引值和标签。
到这的时候,tensorboard上就会显示出数据点出来了,类似于:
其中不同颜色表示不同类别。
如果需要把这些数据点用图像进行表示,就需要用到mnistdigits.png,这里生成mnistdigits.png用到了三个函数:
def create_sprite_image(images):
"""Returns a sprite image consisting of images passed as argument. Images should be count x width x height"""
if isinstance(images, list):
images = np.array(images)
img_h = images.shape[1]
img_w = images.shape[2]
n_plots = int(np.ceil(np.sqrt(images.shape[0])))
spriteimage = np.ones((img_h * n_plots ,img_w * n_plots ))
for i in range(n_plots):
for j in range(n_plots):
this_filter = i * n_plots + j
if this_filter < images.shape[0]:
this_img = images[this_filter]
spriteimage[i * img_h:(i + 1) * img_h,
j * img_w:(j + 1) * img_w] = this_img
return spriteimage
def vector_to_matrix_mnist(mnist_digits):
"""Reshapes normal mnist digit (batch,28*28) to matrix (batch,28,28)"""
return np.reshape(mnist_digits,(-1,28,28))
def invert_grayscale(mnist_digits):
""" Makes black white, and white black """
return 1-mnist_digits
to_visualise = batch_xs
to_visualise = vector_to_matrix_mnist(to_visualise)
to_visualise = invert_grayscale(to_visualise)
sprite_image = create_sprite_image(to_visualise)
其中vector_to_matrix_mnist用于将向量转为矩阵形式(图像表示),invert_grayscale则用于给图像添加背景色,一般为以上的用法,即背景色为白色。create_sprite_image则是把经过vector_to_matrix_mnist、invert_grayscale处理的embedding_var中的数据变为一个图像墙,如下:
这里有500个手写体图像,这里的500与embedding_var数据的行数对应相同。到此为止,Tensorboard就可以根据索引值和每个数据点的大小[28*28]来取数据点对应的图像贴到PROJECTOR上对应的数据点了。大功告成!效果如下:
https://www.pinchofintelligence.com/simple-introduction-to-tensorboard-embedding-visualisation/
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
from tensorflow.contrib.tensorboard.plugins import projector
from tensorflow.examples.tutorials.mnist import input_data
LOG_DIR = 'minimalsample'
NAME_TO_VISUALISE_VARIABLE = "mnistembedding"
TO_EMBED_COUNT = 500
path_for_mnist_sprites = os.path.join(LOG_DIR,'mnistdigits.png')
path_for_mnist_metadata = os.path.join(LOG_DIR,'metadata.tsv')
mnist = input_data.read_data_sets("MNIST_data/", one_hot=False)
batch_xs, batch_ys = mnist.train.next_batch(TO_EMBED_COUNT)
embedding_var = tf.Variable(batch_xs, name=NAME_TO_VISUALISE_VARIABLE)
summary_writer = tf.summary.FileWriter(LOG_DIR)
def create_sprite_image(images):
"""Returns a sprite image consisting of images passed as argument. Images should be count x width x height"""
if isinstance(images, list):
images = np.array(images)
img_h = images.shape[1]
img_w = images.shape[2]
n_plots = int(np.ceil(np.sqrt(images.shape[0])))
spriteimage = np.ones((img_h * n_plots ,img_w * n_plots ))
for i in range(n_plots):
for j in range(n_plots):
this_filter = i * n_plots + j
if this_filter < images.shape[0]:
this_img = images[this_filter]
spriteimage[i * img_h:(i + 1) * img_h,
j * img_w:(j + 1) * img_w] = this_img
return spriteimage
def vector_to_matrix_mnist(mnist_digits):
"""Reshapes normal mnist digit (batch,28*28) to matrix (batch,28,28)"""
return np.reshape(mnist_digits,(-1,28,28))
def invert_grayscale(mnist_digits):
""" Makes black white, and white black """
return 1-mnist_digits
config = projector.ProjectorConfig()
embedding = config.embeddings.add()
embedding.tensor_name = embedding_var.name
# Specify where you find the metadata
embedding.metadata_path = 'metadata.tsv' #'metadata.tsv'
# Specify where you find the sprite (we will create this later)
embedding.sprite.image_path = 'mnistdigits.png' #'mnistdigits.png'
embedding.sprite.single_image_dim.extend([28,28])
# Say that you want to visualise the embeddings
projector.visualize_embeddings(summary_writer, config)
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver()
saver.save(sess, os.path.join(LOG_DIR, "model.ckpt"), 1)
to_visualise = batch_xs
to_visualise = vector_to_matrix_mnist(to_visualise)
to_visualise = invert_grayscale(to_visualise)
sprite_image = create_sprite_image(to_visualise)
plt.imsave(path_for_mnist_sprites,sprite_image,cmap='gray')
plt.imshow(sprite_image,cmap='gray')
with open(path_for_mnist_metadata,'w') as f:
f.write("Index\tLabel\n")
for index,label in enumerate(batch_ys):
f.write("%d\t%d\n" % (index,label))