簡體   English   中英

tensorflow2 如何打印張量值

[英]tensorflow2 how to print tensor value

我嘗試打印從我的自定義 tfds 數據集加載的張量的實際值。 我不知道該怎么做。 我正在使用 Tensorflow2,因此不再鼓勵使用 session。 我試過使用.numpy() tf.print。 tf.executing.eagerly() 但沒有任何效果。 它要么只打印張量 object 向我展示形狀,要么在 .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")

控制台 output:

控制台 output

您可以使用 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)

output:

在此處輸入圖像描述

有關詳細信息,請參閱此要點。謝謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM