为什么在张量流标签中创建的数据集中显示形状和数据类型信息?

Why in created dataset in tensorflow Labels showed with shape and datatype information?

我是 Tensorflow 新手: 我想创建自己的数据集:

labels = ['N', 'N', 'N', 'A', 'A']

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
  data_dir , labels = labels,
  validation_split=0.2,
  subset="training",
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)

plt.figure(figsize=(10, 10))
for images, labels in train_ds.take(1):
  for i in range(2):
    ax = plt.subplot(2, 2, i + 1)
    plt.imshow(images[i].numpy().astype('uint8'))
    plt.title(labels[i])
    plt.axis('off')

为什么当我绘制图像时,标签是这样打印的,而不仅仅是标签名称?

当您迭代数据集时,您会得到 tf.Tensor 个对象,当您将其直接传递给 plt.title 时,它会转换为产生该结果的字符串。如果你想要张量的内容,你可以使用 .numpy(),就像你对图像所做的那样:

plt.title(labels[i].numpy())

编辑:b 前缀是因为 TensorFlow 字符串实际上对应于 Python 字节数组(当表示为字符串时,获取前缀)。如果你想得到一个实际的 str,所以你看不到,你可以使用 .decode():

plt.title(labels[i].numpy().decode())