【发布时间】:2017-09-16 14:32:58
【问题描述】:
如何在生成 TFRecordDataset 时跳过 TFRecord 文件中的条目?
给定一个TFRecord 文件和tf.contrib.data.TFRecordDataset 对象,我通过maping 在protobuf 定义上创建一个新数据集。例如,
features = {'some_data': tf.FixedLenFeature([], tf.string)}
def parser(example_proto):
e = tf.parse_single_example(example_proto, features)
data = e['some_data']
# ...do a bunch of stuff to data...
return data
x = TFRecordDataset(filename)
x = x.map(parser)
x = x.cache(cache_filename)
x = x.repeat()
x = x.batch(batch_size)
这让我可以读取数据并进行一些预处理,然后缓存结果并为我的模型批量处理。
我的问题是,如果我想跳过 TFRecord 条目之一(例如,如果数据无效/错误)怎么办?例如,在parser() 中,也许我可以返回None,或者某种tf.cond 来指示无效条目,或者触发一些断言。
【问题讨论】:
-
使用 x.filter() 或 FilterDataset 怎么样? github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/…
-
这就是我最终使用的,
filter()。由于我的代码架构,我担心我不得不在map()中执行此操作,但结果证明使用filter和适当的谓词就可以了。谢谢。
标签: tensorflow tensorflow-datasets