首页 文章

如何使用嵌套形状的tf.data.Dataset.padded_batch?

提问于
浏览
2

我正在为每个元素构建一个具有两个形状[张,宽,高,3]和[批次,类]张量的数据集 . 为简单起见,我们可以说class = 5 .

你为 dataset.padded_batch(1000,shape) 提供了什么样的形状,以便沿宽度/高度/ 3轴填充图像?

我尝试过以下方法:

tf.TensorShape([[None,None,None,3],[None,5]])
[tf.TensorShape([None,None,None,3]),tf.TensorShape([None,5])]
[[None,None,None,3],[None,5]]
([None,None,None,3],[None,5])
(tf.TensorShape([None,None,None,3]),tf.TensorShape([None,5])‌​)

每次引发TypeError

The docs州:

padded_shapes:tf.TensorShape或tf.int64矢量张量类对象的嵌套结构,表示在批处理之前应填充每个输入元素的相应组件的形状 . 任何未知的尺寸(例如tf.TensorShape中的tf.Dimension(None)或类似张量的对象中的-1)将被填充到每个批次中该尺寸的最大尺寸 .

相关代码:

dataset = tf.data.Dataset.from_generator(generator,tf.float32)
shapes = (tf.TensorShape([None,None,None,3]),tf.TensorShape([None,5]))
batch = dataset.padded_batch(1,shapes)

2 回答

  • 0

    感谢mrry寻找解决方案 . 事实证明,from_generator中的类型必须与条目中的张量数相匹配 .

    新代码:

    dataset = tf.data.Dataset.from_generator(generator,(tf.float32,tf.float32))
    shapes = (tf.TensorShape([None,None,None,3]),tf.TensorShape([None,5]))
    batch = dataset.padded_batch(1,shapes)
    
  • 1

    TensorShape不接受嵌套列表 . tf.TensorShape([None, None, None, 3, None, 5])TensorShape(None) (注意没有 [] )是合法的 .

    但是,结合这两个张量对我来说听起来很奇怪 . 我不确定你要完成什么,但我建议你尝试不使用不同尺寸的张量 .

相关问题