如何可视化或绘制多维张量? [英] How do I visualize or plot a multidimensional tensor?

查看:59
本文介绍了如何可视化或绘制多维张量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这里是否有人试图以numpy可视化多维张量.如果是这样,你能和我分享一下我如何去做吗?我正在考虑将其简化为 2D 可视化.

我已经包含了一些示例输出.它的结构很奇怪,有省略号...",它有一个 4D 张量布局 [[[[ 这里的内容]]]]]

样本数据:

[[[[ -9.37186​​633e-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]]]]

解决方案

  • 为了绘制高维数据,有一种称为 T-SNE 的技术

  • T-SNE 由 tensorflow 作为 tesnorboard 功能提供

  • 您可以仅将张量提供为嵌入并运行张量板

  • 您可以在 3D 或 2D 中可视化高维数据

  • 这里是使用Tensor-board进行数据可视化的链接:

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

  • 以下是使用 scikit learn 的 TSNE 的示例代码

     # x 是我的数据,它是一个 nd 数组# 在使用 scikit-learn 的 tsne 之前,您必须将张量转换为 nd-array# 将你的张量转换为 x ======>x = tf.Session().run(tensor_x)标准= 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表示您打算将尺寸数据绘制为2Dx_test_2d = tsne.fit_transform(x_std)打印()标记 = ('s', 'd', 'o', '^', 'v', '8', 's', 'p', "_", '2')color_map = {0: '红色', 1: '蓝色', 2: '浅绿色', 3: '紫色', 4: '青色', 5: '黑色', 6: '黄色', 7: '洋红色',8:'李子',9:'黄绿色'}对于 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], 标记=markers[idx],标签=cl)plt.xlabel('t-SNE 中的 X')plt.ylabel('t-SNE中的'Y')plt.legend(loc='左上')plt.title('t-SNE 测试数据可视化')plt.show()

  • ScikitLearn的TSNE结果:

  • 您也可以使用 PCA 用于将高维数据绘制为 2D

  • 这是 PCA .

  • Scikit Learn PCA:https://scikit-learn.org/stable/modules/generation/sklearn.decomposition.PCA.html

I was wondering if anyone here has ever tried to visualize a multidimensional tensor in numpy. If so, could you share with me how I might go about doing this? I was thinking of reducing it to a 2D visualization.

I've included some sample output. It's weirdly structured, there are ellipses "..." and it's got a 4D tensor layout [[[[ content here]]]]

Sample Data:

[[[[ -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]]]]

解决方案

  • For plotting high dimensional data there is a technique called as T-SNE

  • T-SNE is provided by tensorflow as a tesnorboard feature

  • You can just provide the tensor as an embedding and run tensorboard

  • You can visualize high dimensional data in either 3D or 2d

  • Here is a link for Data Visualization using Tensor-board: https://github.com/jayshah19949596/Tensorboard-Visualization-Freezing-Graph

  • Your code should be something like this :

      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 visualization:

  • You can use scikit learn's TSNE to plot high dimensional data

  • Below is sample coede to use scikit learn's 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's TSNE Results:

  • You can also use PCA for plotting high dimensional data to 2D

  • Here is implementation of PCA.

  • Scikit Learn PCA: https://scikit-learn.org/stable/modules/generated/sklearn.decomposition.PCA.html

这篇关于如何可视化或绘制多维张量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆