[英]How to use tf.data.Dataset.padded_batch with a nested shape?
我正在为每个元素构建一个具有两个形状[张,宽,高,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
文档说明:
padded_shapes:tf.TensorShape或tf.int64向量张量类对象的嵌套结构,表示在批处理之前应填充每个输入元素的相应组件的形状。 任何未知的尺寸(例如tf.TensorShape中的tf.Dimension(无)或类似张量的对象中的-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)
感谢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)
TensorShape不接受嵌套列表。 tf.TensorShape([None, None, None, 3, None, 5])
和TensorShape(None)
(注意没有[]
)是合法的。
但是,结合这两个张量对我来说听起来很奇怪。 我不确定你要完成什么,但我建议你尝试不使用不同尺寸的张量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.