生成TFRecordDataset时,如何跳过TFRecord文件中的条目?跳过TFRecordDataset.map()中的数据集条目
给定一个TFRecord
文件和对象,我创建了一个新的数据集map
ing 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? https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/data/python/ops/dataset_ops.py#L1296 –
这就是我最终使用的'filter()'。由于我的代码架构,我担心我必须在'map()'中执行它,但事实证明,使用'filter'和适当的谓词就可以。谢谢。 – cacti