2

生成 TFRecordDataset 时如何跳过 TFRecord 文件中的条目?

给定一个TFRecord文件和tf.contrib.data.TFRecordDataset对象,我通过mapprotobuf 定义创建一个新数据集。例如,

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指示无效条目,或者触发一些断言。

4

1 回答 1

2

(总结评论作为答案)

filter()方法Dataset可以根据谓词过滤条目。

于 2017-09-18T22:32:01.913 回答