如何可视化或绘制多维张量?

时间:2018-03-09 00:02:03

标签: numpy matplotlib multidimensional-array tensorflow visualize

我想知道这里有没有人试图想象一下numpy中的多维张量。如果是这样,你能和我分享我怎么做这个吗?我正在考虑将其缩减为2D可视化。

我已经包含了一些示例输出。这是奇怪的结构,有椭圆“......”,它有一个4D张量布局[[[[内容在这里]]]]

示例数据:

[[[[ -9.37186633e-05  -9.89684777e-05  -8.97786958e-05 ...,
     -1.08984910e-04  -1.07056971e-04  -8.68257193e-05]
  [[ -9.61350961e-05  -8.75062251e-05  -9.39425736e-05 ...,
     -1.17737654e-04  -9.66376538e-05  -8.78447026e-05]
   [ -1.06558400e-04  -9.04031331e-05  -1.04479543e-04 ...,
     -1.02786013e-04  -1.07974607e-04  -1.07524407e-04]]
 [[[ -1.09648725e-04  -1.01073667e-04  -9.39013553e-05 ...,
     -8.94383265e-05  -9.06078858e-05  -9.83356076e-05]
   [ -9.76310257e-05  -1.04029998e-04  -1.01905476e-04 ...,
     -9.50643880e-05  -8.29156561e-05  -9.75912480e-05]]]
   [ -1.12038200e-04  -1.00154917e-04  -9.00980813e-05 ...,
     -1.10244124e-04  -1.16597665e-04  -1.10604939e-04]]]]

1 个答案:

答案 0 :(得分:2)

  • 为了绘制高维数据,有一种称为T-SNE
  • 的技术
  • T-SNE由tensorflow作为tesnorboard功能提供
  • 您可以将张量提供为嵌入和运行张量板
  • 您可以在3D或2d
  • 中显示高维数据
  • 以下是使用Tensor-board进行数据可视化的链接:https://github.com/jayshah19949596/Tensorboard-Visualization-Freezing-Graph
  • 您的代码应该是这样的:

    tensor_x = tf.Variable(mnist.test.images, name='images')
    config = projector.ProjectorConfig()
    # One can add multiple embeddings.
    embedding = config.embeddings.add()
    embedding.tensor_name = tensor_x.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(logs_path), config)
    
  • Tensorboard可视化: enter image description here

  • 您可以使用scikit learn的TSNE绘制高维数据

  • 以下是使用scikit learn的TSNE

    的示例
    # x is my data which is a nd-array
    # You have to convert your tensor to nd-array before using scikit-learn's tsne
    # Convert your tensor to x =====> x = tf.Session().run(tensor_x)
    standard = StandardScaler()
    x_std = standard.fit_transform(x)
    plt.figure()
    
    label_encoder = LabelEncoder()
    y = label_encoder.fit_transform(y)
    
    tsne = TSNE(n_components=2, random_state=0)  # n_components means you mean to plot your dimensional data to 2D
    x_test_2d = tsne.fit_transform(x_std)
    
    print()
    
    markers = ('s', 'd', 'o', '^', 'v', '8', 's', 'p', "_", '2')
    color_map = {0: 'red', 1: 'blue', 2: 'lightgreen', 3: 'purple', 4: 'cyan', 5: 'black', 6: 'yellow', 7: 'magenta',
             8: 'plum', 9: 'yellowgreen'}
    for idx, cl in enumerate(np.unique(y)):
    
        plt.scatter(x=x_test_2d[y == cl, 0], y=x_test_2d[y == cl, 1], c=color_map[idx], marker=markers[idx],
                label=cl)
    plt.xlabel('X in t-SNE')
    plt.ylabel('Y in t-SNE')
    plt.legend(loc='upper left')
    plt.title('t-SNE visualization of test data')
    plt.show()
    
  • ScikitLearn的TSNE成绩: enter image description here

  • 您还可以使用PCA绘制高维数据到2D