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

问题描述

我正在尝试编写一个自定义模型,其中我正在编写一个自定义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)

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

def __iter__(self):
    for item in (self[i] for i in range(len(self))):
        yield item

但是,当我试图检索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()属性的张量

这哪里错了??

标签: tensorflowkerastensorflow2.0tensorflow-datasetstf.keras

解决方案


推荐阅读