【问题标题】:tensorflow2 how to print tensor valuetensorflow2 如何打印张量值
【发布时间】:2023-01-11 17:55:36
【问题描述】:

我尝试打印从我的自定义 tfds 数据集加载的张量的实际值。 我不知道该怎么做。我正在使用 Tensorflow2,因此不再鼓励使用该会话。 我尝试使用 .numpy() tf.print。 tf.executing.eagerly() 但没有任何效果。它要么打印 只是张量对象向我显示形状,或者在 .numpy() 的情况下它会在标题中抛出错误。 我需要张量的值,我需要将它带回 numpy 以便调试代码。

这就是我创建数据集的方式:

class dt(tfds.core.GeneratorBasedBuilder):
    ''' Dataset builder'''

    # DOuble check
    VERSION = tfds.core.Version('1.0.0')
    RELEASE_NOTES = {
      '1.0.0': 'Initial release.',
    }

    def _info(self) ->tfds.core.DatasetInfo:
        '''Dataset metadata'''

        return tfds.core.DatasetInfo(
            builder=self,
            features=tfds.features.FeaturesDict({
                "id": tf.int64,
                "image": tfds.features.Image(shape=(IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), encoding_format='png'),
                "mask": tfds.features.Image(shape=(IMG_HEIGHT, IMG_WIDTH, 1), encoding_format='png'),
                "label": tfds.features.ClassLabel(names=CLASSES),
            }),
        supervised_keys=('image', 'mask')
        )

    def _split_generators(self, dl_manager: tfds.download.DownloadManager):
        '''Splitgenerator for train and test splits'''

        path = DATASETS_ROOT
        return {
            "train": self._generate_examples(
                images_path=os.path.join(path, "train/rgb"),
                masks_path=os.path.join(path, "train/masks")
                ),
            "test": self._generate_examples(
                images_path=os.path.join(path, "test/rgb"),
                masks_path=os.path.join(path, "test/masks")
                )
        }

    def _generate_examples(self, images_path, masks_path):
        '''Generator of examples for each split'''
        
        for i, (image, mask) in enumerate(zip(glob.glob(images_path + "/*.png"), glob.glob(masks_path + "/*.png"))):
            yield i, {
                "id": i,
                "image": image,
                "mask": mask,
                "label": CLASSES[3],
            }

这就是我尝试提取 numpy 数组的方式

def custom_load_X_Y(training=True):

    if training:
        dt, dt_info = tfds.load("dt", split="train", shuffle_files=True, as_supervised=True, with_info=True)

        print(f'EAGERLY {tf.executing_eagerly()}')
        print(f'MOde type {type(dt)}')
        tf.print(f"aaaaa {dt.numpy()} aaaaaa")

控制台输出:

Console output

【问题讨论】:

  • 你似乎已经编辑了一点,“它在标题中引发了错误。”标题没有错误。所以你正在创建一个 tfds 数据集?你能做一个最小的可重现的例子吗?什么是“dt”和“dt_info”。我怀疑您需要做一些事情来获取数据,否则 dt.numpy() 可能是一个疯狂的操作。
  • 对不起这是我的错。我调试了太多次 :D dt 是根据编辑创建的数据集。
  • 你看过他们的榜样吗? tensorflow.org/datasets 您应该调用 ds.take 来获取数据集中的数值数据。
  • 谢谢!这应该够了吧!我只是对 tensorflow 中的调试感到困惑:O

标签: numpy tensorflow


【解决方案1】:

您可以使用 tfds.show_examples() 打印图像数据集

import tensorflow_datasets as tfds
ds, ds_info = tfds.load('cifar10', split='train', with_info=True)
fig = tfds.show_examples(ds, ds_info)

输出:

有关详细信息,请参阅此gist。谢谢。

【讨论】:

    猜你喜欢
    • 2021-12-16
    • 2017-09-12
    • 1970-01-01
    • 2018-06-26
    • 2020-08-19
    • 2017-01-14
    • 2019-08-13
    • 1970-01-01
    相关资源
    最近更新 更多