> ## Content Index
> Fetch the complete content index at: https://yinguobing.com/blog/llms.txt
> Use this file to discover other available public pages before exploring further.

# 如何生成TFRecord文件
- URL: https://yinguobing.com/blog/how-to-generate-tfrecord-file/
- Published: 2019-03-26T02:35:02.000Z
- Updated: 2022-05-29T05:32:31.000Z
- Description: 使用Python生成TFRecord文件的最小示例代码。
- Author: 尹国冰
- Tags: TensorFlow

TFRecord文件是TensorFlow原生支持的数据格式，适合用来作为训练所需的数据的存储容器。配合Dataset API可以极大的提升数据I/O速度，简化代码。

TensorFlow官方提供了一份TFRecord文件的[使用教程](https://www.tensorflow.org/tutorials/load%5Fdata/tf%5Frecords?ref=yinguobing.com)。TFRecord看起来复杂，实际上使用起来可以归纳为几个要点：

1. 将需要存储的数据按照格式转换为 tf.train.Feature。
2. 将多个Feature组装为一个Python字典，再转换为tf.train.Example。
3. 将 tf.train.Example 序列化（SerializeToString），使用 TFRecordWriter 写入文件。

例如下方的示例代码，演示了如何将一张图片与label写成TFRecord文件。

---

1. [https://www.tensorflow.org/tutorials/load\_data/tf\_records](https://www.tensorflow.org/tutorials/load%5Fdata/tf%5Frecords?ref=yinguobing.com)