tf.data.Dataset 迭代器返回 Tensor("IteratorGetNext:1", shape=(None, 16), dtype=int32) 但无法获取张量的值

Sil*_*tya 8 keras tensorflow tensorflow-datasets tf.keras tensorflow2.0

我正在尝试编写一个自定义模型,其中我正在编写一个自定义train_step函数

我正在从自定义数据生成器创建一个“tf.data.Dataset”,例如

tds = tf.data.Dataset.from_generator(tdg.__iter__,args=None,output_types = (tf.float32,tf.int32),output_shapes = (tf.TensorShape([16,64,64,3]),tf.TensorShape([16])))
tds = tds.batch(1)
Run Code Online (Sandbox Code Playgroud)

在自定义 DataGenerator 中,该__iter__方法定义为

def __iter__(self):
    for item in (self[i] for i in range(len(self))):
        yield item
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试检索train_step函数内的数据时,x,y = data我得到

Tensor("IteratorGetNext:0", shape=(None, 16, 64, 64, 3), dtype=float32)

Tensor("IteratorGetNext:1", shape=(None, 16), dtype=int32) 作为输出

如果我跑print(x[0])那么我得到

Tensor("strided_slice:0", shape=(16,), dtype=int32)

我没有得到具有numpy()属性的张量

这是哪里出错了??

小智 1

这适用于tf.data

for data_batch, label in tfds:
    print(image_batch.numpy().shape)
    for data in data_batch:
         print(image.numpy().shape)
Run Code Online (Sandbox Code Playgroud)