如何使用带有嵌套形状的 tf.data.Dataset.padded_batch? [英] How to use tf.data.Dataset.padded_batch with a nested shape?
问题描述
我正在为每个元素构建一个具有 [batch,width,heigh,3] 和 [batch,class] 形状张量的数据集.为简单起见,假设 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])][[无,无,无,3],[无,5]]([无,无,无,3],[无,5])(tf.TensorShape([None,None,None,3]),tf.TensorShape([None,5])
每次引发 TypeError
文档 状态:><块引用>
padded_shapes:tf.TensorShape 或 tf.int64 向量的嵌套结构代表形状的张量对象应在批处理之前填充每个输入元素的组件.任何未知的维度(例如 tf.TensorShape 中的 tf.Dimension(None) 或-1 在类似张量的对象中)将被填充到每个批次中该维度的最大大小.
相关代码:
dataset = tf.data.Dataset.from_generator(generator,tf.float32)形状 = (tf.TensorShape([None,None,None,3]),tf.TensorShape([None,5]))批处理 = dataset.padded_batch(1,shapes)
感谢 mrry 找到解决方案.原来 from_generator 中的类型必须与条目中的张量数量相匹配.
新代码:
dataset = tf.data.Dataset.from_generator(generator,(tf.float32,tf.float32))形状 = (tf.TensorShape([None,None,None,3]),tf.TensorShape([None,5]))批处理 = dataset.padded_batch(1,shapes)
I am building a dataset with two tensors of shape [batch,width,heigh,3] and [batch,class] for each element. For simplicity lets say class = 5.
What shape do you feed to dataset.padded_batch(1000,shape)
such that image is padded along the width/height/3 axis?
I have tried the following:
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]))
Each raising TypeError
The docs state:
padded_shapes: A nested structure of tf.TensorShape or tf.int64 vector tensor-like objects representing the shape to which the respective component of each input element should be padded prior to batching. Any unknown dimensions (e.g. tf.Dimension(None) in a tf.TensorShape or -1 in a tensor-like object) will be padded to the maximum size of that dimension in each batch.
The relevant code:
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)
Thanks to mrry for finding the solution. Turns out that the type in from_generator has to match the number of tensors in the entries.
new code:
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)
这篇关于如何使用带有嵌套形状的 tf.data.Dataset.padded_batch?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!