RSVP for your your local TensorFlow Everywhere event today!

Writes a dataset to a TFRecord file.

Used in the notebooks

Used in the tutorials

The elements of the dataset must be scalar strings. To serialize dataset elements as strings, you can use the function.

dataset =
dataset =
writer ="/path/to/file.tfrecord")

To read back the elements, use TFRecordDataset.

dataset ="/path/to/file.tfrecord")
dataset = x:, tf.int64))

To shard a dataset across multiple TFRecord files:

dataset = ... # dataset to be written

def reduce_func(key, dataset):
  filename = tf.strings.join([PATH_PREFIX, tf.strings.as_string(key)])
  writer =
  writer.write( _, x: x))

dataset = dataset.enumerate()
dataset = dataset.apply(
  lambda i, _: i % NUM_SHARDS, reduce_func, tf.int64.max

filename a string path indicating where to write the TFRecord data.
compression_type (Optional.) a string indicating what type of compression to use when writing the file. See for what types of compression are available. Defaults to None.



View source

Writes a dataset to a TFRecord file.

An operation that writes the content of the specified dataset to the file specified in the constructor.

If the file exists, it will be overwritten.

dataset a whose elements are to be written to a file

In graph mode, this returns an operation which when executed performs the write. In eager mode, the write is performed by the method itself and there is no return value.

Raises TypeError: if dataset is not a TypeError: if the elements produced by the dataset are not scalar strings.