如何使用张量板嵌入投影仪? [英] How to use tensorboard Embedding Projector?
问题描述
如何使用 Tensorboard 中包含的嵌入投影仪?
我找不到它的任何文档.然而,这种可视化并不是很有帮助,因为我们没有看到每个数据点所属的不同类.为了将每个类与另一个类区分开来,应该提供一些元数据:
import os将张量流导入为 tf从 tensorflow.examples.tutorials.mnist 导入 input_data从 tensorflow.contrib.tensorboard.plugins 导入投影仪LOG_DIR = '日志'元数据 = os.path.join(LOG_DIR, 'metadata.tsv')mnist = input_data.read_data_sets('MNIST_data')图像 = tf.Variable(mnist.test.images, name='images')使用 open(metadata, 'w') 作为 metadata_file:对于 mnist.test.labels 中的行:metadata_file.write('%d\n' % 行)使用 tf.Session() 作为 sess:saver = tf.train.Saver([图片])sess.run(images.initializer)saver.save(sess, os.path.join(LOG_DIR, 'images.ckpt'))配置 = 投影仪.ProjectorConfig()# 一个可以添加多个嵌入.嵌入 = config.embeddings.add()embedding.tensor_name = images.name# 将此张量链接到其元数据文件(例如标签).embedding.metadata_path = 元数据# 保存 TensorBoard 在启动时读取的配置文件.投影仪.visualize_embeddings(tf.summary.FileWriter(LOG_DIR),配置)
这给了我们:
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.
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.
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:
这篇关于如何使用张量板嵌入投影仪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!