TensorFlow 1 version View source on GitHub

A class to write records to a TFRecords file.

TFRecords tutorial

TFRecords is a binary format which is optimized for high throughput data retrieval, generally in conjunction with TFRecordWriter is used to write serialized examples to a file for later consumption. The key steps are:

Ahead of time:

A minimal example is given below:

import tempfile
example_path = os.path.join(tempfile.gettempdir(), "example.tfrecords")
# Write the records to a file.
with as file_writer:
  for _ in range(4):
    x, y = np.random.random(), np.random.random()

    record_bytes = tf.train.Example(features=tf.train.Features(feature={
        "x": tf.train.Feature(float_list=tf.train.FloatList(value=[x])),
        "y": tf.train.Feature(float_list=tf.train.FloatList(value=[y])),
# Read the data back out.
def decode_fn(record_bytes):
      # Data

      # Schema
      {"x":[], dtype=tf.float32),
       "y":[], dtype=tf.float32)}
for batch in[example_path]).map(decode_fn):
  print("x = {x:.4f},  y = {y:.4f}".format(**batch))
x = 0.5488,  y = 0.7152
x = 0.6028,  y = 0.5449
x = 0.4237,  y = 0.6459
x = 0.4376,  y = 0.8918

This class implements __enter__ and __exit__, and can be used in with blocks like a normal file. (See the usage example above.)

path The path to the TFRecords file.
options (optional) String specifying compression type, TFRecordCompressionType, or TFRecordOptions object.

IOError If path cannot be opened for writing.
ValueError If valid compression_type can't be determined from options.



View source

Close the file.


View source

Flush the file.


View source

Write a string record to the file.

record str


enter(self: object) -> object


exit(self: tensorflow.python._pywrap_record_io.RecordWriter, *args) -> None