【发布时间】:2019-09-24 04:13:08
【问题描述】:
[在@mrry 评论后编辑#1] 我正在使用(伟大而令人惊叹的)数据集 API 以及 tf.contrib.data.rejection_resample 为输入训练管道设置特定的分布函数。
在将 tf.contrib.data.rejection_resample 添加到 input_fn 之前,我使用了一次性迭代器。唉,当开始使用后者时,我尝试使用 dataset.make_initializable_iterator() - 这是因为我们正在引入管道状态变量,并且需要在输入管道中的所有变量都初始化之后初始化迭代器。 正如@mrry 写的here.
我将 input_fn 传递给估计器并由实验包装。
问题是 - 在哪里挂钩迭代器的 init? 如果我尝试:
dataset = dataset.batch(batch_size)
if self.balance:
dataset = tf.contrib.data.rejection_resample(dataset, self.class_mapping_function, self.dist_target)
iterator = dataset.make_initializable_iterator()
tf.add_to_collection(tf.GraphKeys.TABLE_INITIALIZERS, iterator.initializer)
else:
iterator = dataset.make_one_shot_iterator()
image_batch, label_batch = iterator.get_next()
print (image_batch)
以及映射函数:
def class_mapping_function(self, feature, label):
"""
returns a a function to be used with dataset.map() to return class numeric ID
The function is mapping a nested structure of tensors (having shapes and types defined by dataset.output_shapes
and dataset.output_types) to a scalar tf.int32 tensor. Values should be in [0, num_classes).
"""
# For simplicity, trying to return the label itself as I assume its numeric...
return tf.cast(label, tf.int32) # <-- I guess this is the bug
迭代器不像单次迭代器那样接收张量形状。
例如。 通过 One Shot 迭代器运行,迭代器得到正确的形状:
Tensor("train_input_fn/IteratorGetNext:0", shape=(?, 100, 100, 3), dtype=float32, device=/device:CPU:0)
但是在使用可初始化迭代器时,它缺少张量形状信息:
Tensor("train_input_fn/IteratorGetNext:0", shape=(?,), dtype=int32, device=/device:CPU:0)
任何帮助将不胜感激!
[Edit #2 ]- 在@mrry 评论之后,它似乎是另一个数据集] 也许这里真正的问题不是迭代器的初始化序列,而是 tf.contrib.data.rejection_resample 使用的映射函数,它返回 tf.int32。但是后来我想知道应该如何定义映射函数?例如,将数据集形状保持为 (?,100,100,3)...
[Edit #3]:来自rejection_resample的实现
class_values_ds = dataset.map(class_func)
因此,class_func 将获取一个数据集并返回一个 tf.int32 的数据集是有意义的。
【问题讨论】:
-
你能分享代码吗?看起来这两个迭代器是从两个不同的
Dataset对象创建的,这可能解释了为什么它们具有不同的推断形状(和类型!)。 -
嗨@mrry。谢谢你这么快回复!我添加了所有信息,我认为您肯定发现了问题。我相信这不是初始化过程,而是滥用 tf.contrib.data.rejection_resample 中的映射函数。如果您同意这就是原因 - 如果您能评论应该如何定义映射函数,我将不胜感激,因为我找不到这方面的参考。谢谢 ! ——
标签: tensorflow iterator