2017-07-10 52 views
0

我想基于多个张量变量(即基于不同的嵌入变量)来可视化我的数据。换句话说,我需要做的是以下几点:使用Tensorflow可视化多个嵌入

我需要将100维矢量(图像特征/嵌入)存储到5个不同的变量中。然后我需要基于5个不同的变量来显示我的数据。也就是说,我需要基于前20个功能形象化我的数据,并基于第二个20个功能,依此类推...

当我在查看关于https://www.tensorflow.org/get_started/embedding_viz的嵌入可视化教程时,他们说我们可以添加多个嵌入。这是我正在寻找的。

如何在tensorflow中做到这一点?

任何帮助非常感谢!

回答

0

所以它没有工作的原因是因为我试图将100维嵌入分成100个不同的变量。这并不奏效。所以当我把我的嵌入分成5个不同的部分,也就是将它们分成5个不同的变量时,它就解决了。以下是我的代码:

import numpy as np 
import tensorflow as tf 
from tensorflow.contrib.tensorboard.plugins import projector 

LOG_DIR = \ 
    'C:/Users/user/PycharmProjects/VariationalAutoEncoder/' \ 
    'Tensorflow-DeconvNet-Segmentation/Embeddings/features_images.ckpt' 

feature_vectors = np.loadtxt('features.txt') 
feature_vectors = feature_vectors[:5329] 

print("feature_vectors_shape:",feature_vectors.shape) 

sub_features = [] 
for i in range(20): 
    features = tf.Variable(feature_vectors[:, 5 * i: 5 * (i + 1)], name=('features' + str(i))) 
    sub_features.append(features) 

with tf.Session() as sess: 

    saver = tf.train.Saver() 
    sess.run(tf.global_variables_initializer()) 
    saver.save(sess, LOG_DIR) 

    config = projector.ProjectorConfig() 
    for i in range(20): 
     embedding = config.embeddings.add() 
     embedding.tensor_name = sub_features[i].name 

     embedding.sprite.image_path = \ 
      'C:/Users/user/PycharmProjects/VariationalAutoEncoder/Tensorflow-DeconvNet-Segmentation/master.jpg' 
     embedding.sprite.single_image_dim.extend([112, 112]) 

    # Saves a config file that TensorBoard will read during startup. 
    projector.visualize_embeddings(tf.summary.FileWriter(LOG_DIR), config)