tensorflow - 跳过 TFRecordDataset.map() 中的数据集条目

标签 tensorflow tensorflow-datasets

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

给定一个 TFRecord 文件和 tf.contrib.data.TFRecordDataset 对象,我通过 map 在 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 以指示无效条目,或跳闸一些断言。

最佳答案

(总结评论作为答案)

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

关于tensorflow - 跳过 TFRecordDataset.map() 中的数据集条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46254999/

相关文章:

audio - 将实时音频数据馈送到移动设备上的 tensorflow

Tensorflow - 无法将操作转换为张量

python - 使用 tensorflow 中的另一个索引列表访问张量的元素

python - Tensorflow train.py 抛出 Windows 致命异常

python - FailedPreconditionError(请参阅上面的回溯): GetNext() failed because the iterator has not been initialized

tensorflow - 无法理解 keras.datasets.imdb

tensorflow - 具有中间层输出的 Keras 多输出自定义损失

python - 通过 conda-forge 安装 tensorflow-gpu 导致使用 CPU-only tensorflow

tensorflow - tf.dataset、多个路径输入以及每批映射以加载图像

android - 值错误: Invalid tensors 'input' were found