tensorflow - Tensorflow:计算 TFRecord 文件中的示例数——不使用已弃用的 `tf.python_io.tf_record_iterator`
问题描述
请在标记重复之前阅读帖子:
我正在寻找一种有效的方法来计算 TFRecord 图像文件中的示例数量。由于 TFRecord 文件不保存有关文件本身的任何元数据,因此用户必须遍历文件才能计算此信息。
StackOverflow 上有几个不同的问题可以回答这个问题。问题是他们似乎都使用了 DEPRECATEDtf.python_io.tf_record_iterator
命令,所以这不是一个稳定的解决方案。以下是现有帖子的示例:
从 Tensorflow 中的 .tfrecords 文件中获取记录总数
所以我想知道是否有一种方法可以使用新的 Dataset API 来计算记录数。
解决方案
该类下列出了一个reduce
方法Dataset
。他们给出了一个使用该方法计数记录的示例:
# generate the dataset (batch size and repeat must be 1, maybe avoid dataset manipulation like map and shard)
ds = tf.data.Dataset.range(5)
# count the examples by reduce
cnt = ds.reduce(np.int64(0), lambda x, _: x + 1)
## produces 5
不知道这种方法是否比@krishnab 的 for 循环更快。
推荐阅读
- python-3.x - 我怎样才能用烧瓶和 Jinja2 做帖子的提要?
- r - R中的flexdashboard定期自动更新
- javascript - 从 jquery 调用 Mirth 'client api' 服务
- node.js - 检索
of a page with URL in NodeJS</h1> <div id="body"><p>I am trying to get <code><title></code> of the urls without using third party packages. Is it possible to get the title of a page from the url without using - sql - SQL聚合一列中的多个值,然后Pivot
- angular - 在嵌入式动态表单组件中更改表单控件的值
- java - 如何使用 Google 的 GeoCoder Java 客户端获取特定地点的经度和纬度?
- javascript - 有没有办法通过在 next.js 中单击按钮来重新加载页面?
- azure - 使用 azure API 管理根据 JWT 令牌声明限制 api 调用
- .net - 使用 .NET Core 在 Web API 中支持基于 WCF soap 的客户端