本文基于TensorFlow官网的How-Tos写成。

TensorBoard是TensorFlow自带的一个可视化工具,Embeddings是其中的一个功能,用于在二维或三维空间对高维数据进行探索。

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

本文使用MNIST数据讲解Embeddings的使用方法。

代码

# -*- coding: utf-8 -*-
# @author: 陈水平
# @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.90168.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 PATH_TO_MNIST_DATA = "MNIST_data"
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'),www.90168.org 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')
tf.global_variables_initializer().run() # 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)

首先,从这里下载图片,放到log目录下;然后执行上述代码;最后,执行下面的命令启动TensorBoard。

tensorboard --logdir=log

执行后,命令行会显示如下提示信息:

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

打开浏览器,输入上面的链接地址,点击导航栏的EMBEDDINGS即可看到效果:

资源

最新文章

  1. JAVA当中变量什么时候需要初始化
  2. sql server 还原数据库时提示数据库正在使用,无法进行操作的解决方法
  3. Android ActivityThread(主线程或UI线程)简介
  4. 游戏引擎网络开发者的64做与不做(二A):协议与API
  5. MVC控制器给View返回实体
  6. poj 2632 Crashing Robots_模拟
  7. 二叉查找树的Insert和Delete操作
  8. hdu 2824The Euler function
  9. 机器学习:python中如何使用朴素贝叶斯算法
  10. 如何在GitHub上生成ssh公钥并用NetBeans克隆项目
  11. jdbc 增删改查以及遇见的 数据库报错Can't get hostname for your address如何解决
  12. Skin Microstructure Deformation with Displacement Map Convolution项目小结
  13. 【hdu 6161】Big binary tree(二叉树、dp)
  14. rand_1tom 产生 rand_1ton
  15. UI事件
  16. MyEclipse下自定义(支持html5的)JSP模板--JSP
  17. java实现person类 override(重写) comparable接口
  18. Python+Selenium笔记(四):unittest的Test Suite(测试套件)
  19. 创建JDBCUtils工具类
  20. ator自动生成mybatis配置和类信息

热门文章

  1. iOS VIPER架构(二)
  2. JFinal教程:JFinal极速开发企业实战百集JFinal视频教程发布
  3. Android--View事件传递
  4. 第一篇Active Directory疑难解答概述(2)
  5. selenium3+webdriver学习笔记1(访问常用请求)
  6. 11g 新特性 Member Kill Escalation 简介
  7. javaweb基础(13)_session防止表单重复提交
  8. linux交换分区调整
  9. Oracle数据库常用的Sql语句整理
  10. IOS7的变化