python - 如何在配色方案中可视化相似度分数?(张量)
问题描述
我在“sims”变量中有一个相似度分数,我想在矩阵中绘制相似度分数,最好是在配色方案中。有关“sims”变量的更多信息是这样的:
import tensorflow.compat.v2 as tf
print(sims) #Tensor("ExpandDims:0", shape=(1, 64, 64, 1), dtype=float32)
print(sims[0]) #Tensor("strided_slice_6:0", shape=(64, 64, 1), dtype=float32)
tf.print(sims[0][:], output_stream=sys.stderr)
这些行的输出给出了以下屏幕截图。
为了可视化颜色矩阵中的分数,我尝试使用 plt.matshow 方法,但我无法将张量转换为 numpy 数组 (sims[0].numpy()),因为它会给出错误。
AttributeError:“张量”对象没有属性“numpy”。
我也不能使用 plt.matshow。它给出了以下错误:
NotImplementedError:无法将符号张量 (strided_slice_10:0) 转换为 numpy 数组。此错误可能表明您正在尝试将张量传递给 NumPy 调用,这是不受支持的
如何在这样的矩阵(64x64)中可视化这些分数?
解决方案
Tensor
您可以通过将数组转换为Numpy
数组并按如下方式绘制图像,以一种简单的方式通过几种方法来完成此操作。
使用 PIL:
# sims is a Tensor <tf.Tensor: shape=(64, 64, 1), dtype=float64>
import tensorflow as tf
import numpy as np
from PIL import Image
from matplotlib import pyplot as plt
%matplotlib inline
data = sims.numpy()
img = Image.fromarray(data,'RGB')
img.save('my.png')
img.show()
使用 Matplot:
data = sims.numpy()
data.resize(64,64)
plt.figure(figsize = (10,10))
plt.imshow(data, interpolation='nearest')
plt.show()
推荐阅读
- javascript - 如何使用 Wix 代码插入参考?
- php - Laravel 5.4 - 在已经登录的情况下创建另一个用户帐户
- sql-server - 如何创建一个主键,其值是与另一列连接的现有列?
- python - 即使明确指定 tf.device('gpu:0'),Tensorflow 设备也只会确认 CPU
- c# - WPF 将 ObservableCollection 绑定到 ItemsControl - UI 不会更新
- 68000 - 68K 中的 EQU 是否会在内存中保存任何内容?
- python - 仅使用正则表达式捕获列表中的第一个元素(可以是多个,但不能一个接一个
- python - 将yaml文件内容映射到python中的列表
- c# - 有没有办法为自定义类型重载数学函数?
- c# - 将 base64 编码的图像字符串的内容保存到文件中