【问题标题】:Skip Dataset entries in TFRecordDataset.map()跳过 TFRecordDataset.map() 中的数据集条目
【发布时间】: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 来指示无效条目,或者触发一些断言。

【问题讨论】:

标签: tensorflow tensorflow-datasets


【解决方案1】:

(总结评论作为答案)

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

【讨论】:

  • 你能举一个filter()语法的例子吗?我不确定如何定义谓词。
  • 这是一个例子:x = x.filter(lambda data: tf.greater(data, 0))
猜你喜欢
  • 2020-03-19
  • 1970-01-01
  • 2012-10-14
  • 2011-05-08
  • 1970-01-01
  • 1970-01-01
  • 2012-06-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多