注:代码是网上下载的,但是找不到原始出处了,侵权则删

先写出visual类:

class TF_visualizer(object):
def __init__(self, dimension, vecs_file, metadata_file, output_path):
self.dimension = dimension
self.vecs_file = vecs_file
self.metadata_file = metadata_file
self.output_path = output_path self.vecs = []
with open(self.vecs_file, 'r') as vecs:
#with open(self.vecs_file, 'rb') as vecs:
for i, line in enumerate(vecs):
if line != '': self.vecs.append(line) def visualize(self):
# adding into projector
config = projector.ProjectorConfig() placeholder = np.zeros((len(self.vecs), self.dimension)) for i, line in enumerate( self.vecs ):
placeholder[i] = np.fromstring(line, sep=',')
#for i,line in enumerate(self.vecs):
# placeholder[i] = np.fromstring(line) embedding_var = tf.Variable(placeholder, trainable=False, name='amazon') embed = config.embeddings.add()
embed.tensor_name = embedding_var.name
embed.metadata_path = self.metadata_file # define the model without training
sess = tf.InteractiveSession() tf.global_variables_initializer().run()
saver = tf.train.Saver() saver.save(sess, os.path.join(self.output_path, 'w2x_metadata.ckpt')) writer = tf.summary.FileWriter(self.output_path, sess.graph)
projector.visualize_embeddings(writer, config)
sess.close()
print('Run `tensorboard --logdir={0}` to run visualize result on tensorboard'.format(self.output_path))

然后调用类:

output = '/home/xx'

# create a new tensor board visualizer
visualizer = TF_visualizer(dimension = 768,
vecs_file = os.path.join(output, 'amazon_vec.tsv'),
#vecs_file = os.path.join(output, 'mnist_10k_784d_tensors.bytes'),
metadata_file = os.path.join(output, 'amazon.tsv'),
output_path = output)
visualizer.visualize()

其中,amazon_vec.tsv中存放向量(包括词向量,句子向量...),amazon.tsv中存放原始数据,格式为id,label,title,id和title可以随意定义,label则为对应向量的标识,两个文件是 一一对应的(即amazon_vec中的第一行数据对应amazon中第一行数据)

最后,命令行输入

tensorboard --logdir=/home/xx

在浏览器输入http://xx-desktop:6006即可看到可视化的数据(6006是默认端口)

最新文章

  1. Gearman使用示例
  2. mongodb ubuntu下自启动
  3. Stream/Bytes[]/Image对象相互转化
  4. maven url
  5. 别出心裁的Linux系统调用学习法
  6. 没有body怎么添加onload事件
  7. java中的URLConnection和HttpURLConnection
  8. 微软职位内部推荐-Software Engineer II-News
  9. Azure VM 远程无法登陆问题(No Remote Desktop License)
  10. BootStrap2学习日记3--响应式布局实用类
  11. Unity3D中的第三人称镜头的脚本控制
  12. django ORM中update_or_create功能,如果只要匹配某一特定字段呢
  13. JavaScript高级程序设计43.pdf
  14. ajax_post方式
  15. Python学习之二:Python 与 C 区别
  16. ExecutorService
  17. EBS 可拓展的外部信用风险导入
  18. git 一些实用的api
  19. 工作流程引挈 https://www.flowable.org/
  20. Guava Preconditions 工具参数前置校验

热门文章

  1. 《深入理解java虚拟机》读书笔记六——第七章
  2. 使用yum安装报错:[Errno 256] No more mirrors to try
  3. 如何通过给MM修电脑培养感情
  4. flask入门(三)
  5. LINUX使用SSH远程终端时,如何将运行时间长的程序在后台挂起,下次SSH登陆时继续使用同一个SHELL?
  6. Sublime Text(代码编辑软件)
  7. leetcode腾讯精选练习之相交链表(六)
  8. 零基础学习Python web开发、Python爬虫、Python数据分析,从基础到项目实战!
  9. Qt中实现点击一个label,跳转到打开一个浏览器链接
  10. C语言 malloc函数