tf.io.TFRecordWriter
Stay organized with collections
Save and categorize content based on your preferences.
A class to write records to a TFRecords file.
tf.io.TFRecordWriter(
path, options=None
)
Used in the notebooks
TFRecords tutorial
TFRecords is a binary format which is optimized for high throughput data
retrieval, generally in conjunction with tf.data
. 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")
np.random.seed(0)
# Write the records to a file.
with tf.io.TFRecordWriter(example_path) 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])),
})).SerializeToString()
file_writer.write(record_bytes)
# Read the data back out.
def decode_fn(record_bytes):
return tf.io.parse_single_example(
# Data
record_bytes,
# Schema
{"x": tf.io.FixedLenFeature([], dtype=tf.float32),
"y": tf.io.FixedLenFeature([], dtype=tf.float32)}
)
for batch in tf.data.TFRecordDataset([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.)
Args |
path
|
The path to the TFRecords file.
|
options
|
(optional) String specifying compression type,
TFRecordCompressionType , or TFRecordOptions object.
|
Raises |
IOError
|
If path cannot be opened for writing.
|
ValueError
|
If valid compression_type can't be determined from options .
|
Methods
close
View source
close()
Close the file.
flush
View source
flush()
Flush the file.
write
View source
write(
record
)
Write a string record to the file.
__enter__
__enter__()
enter(self: object) -> object
__exit__
__exit__()
exit(self: tensorflow.python.lib.io._pywrap_record_io.RecordWriter, *args) -> None
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf.io.TFRecordWriter\n\n\u003cbr /\u003e\n\n|--------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/lib/io/tf_record.py#L211-L318) |\n\nA class to write records to a TFRecords file.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.io.TFRecordWriter`](https://www.tensorflow.org/api_docs/python/tf/io/TFRecordWriter), [`tf.compat.v1.python_io.TFRecordWriter`](https://www.tensorflow.org/api_docs/python/tf/io/TFRecordWriter)\n\n\u003cbr /\u003e\n\n tf.io.TFRecordWriter(\n path, options=None\n )\n\n### Used in the notebooks\n\n| Used in the tutorials |\n|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [TFRecord and tf.train.Example](https://www.tensorflow.org/tutorials/load_data/tfrecord) - [Graph-based Neural Structured Learning in TFX](https://www.tensorflow.org/tfx/tutorials/tfx/neural_structured_learning) - [Graph regularization for sentiment classification using synthesized graphs](https://www.tensorflow.org/neural_structured_learning/tutorials/graph_keras_lstm_imdb) - [Instance Segmentation with Model Garden](https://www.tensorflow.org/tfmodels/vision/instance_segmentation) - [Semantic Segmentation with Model Garden](https://www.tensorflow.org/tfmodels/vision/semantic_segmentation) |\n\n[TFRecords tutorial](https://www.tensorflow.org/tutorials/load_data/tfrecord)\n\nTFRecords is a binary format which is optimized for high throughput data\nretrieval, generally in conjunction with [`tf.data`](../../tf/data). `TFRecordWriter` is used\nto write serialized examples to a file for later consumption. The key steps\nare:\n\nAhead of time:\n\n- [Convert data into a serialized format](https://www.tensorflow.org/tutorials/load_data/tfrecord#tfexample)\n- [Write the serialized data to one or more files](https://www.tensorflow.org/tutorials/load_data/tfrecord#tfrecord_files_in_python)\n\n During training or evaluation:\n- [Read serialized examples into memory](https://www.tensorflow.org/tutorials/load_data/tfrecord#reading_a_tfrecord_file)\n\n- [Parse (deserialize) examples](https://www.tensorflow.org/tutorials/load_data/tfrecord#reading_a_tfrecord_file)\n\nA minimal example is given below: \n\n import tempfile\n example_path = os.path.join(tempfile.gettempdir(), \"example.tfrecords\")\n np.random.seed(0)\n\n # Write the records to a file.\n with tf.io.TFRecordWriter(example_path) as file_writer:\n for _ in range(4):\n x, y = np.random.random(), np.random.random()\n\n record_bytes = tf.train.Example(features=tf.train.Features(feature={\n \"x\": tf.train.Feature(float_list=tf.train.FloatList(value=[x])),\n \"y\": tf.train.Feature(float_list=tf.train.FloatList(value=[y])),\n })).SerializeToString()\n file_writer.write(record_bytes)\n\n # Read the data back out.\n def decode_fn(record_bytes):\n return tf.io.parse_single_example(\n # Data\n record_bytes,\n\n # Schema\n {\"x\": tf.io.FixedLenFeature([], dtype=tf.float32),\n \"y\": tf.io.FixedLenFeature([], dtype=tf.float32)}\n )\n\n for batch in tf.data.TFRecordDataset([example_path]).map(decode_fn):\n print(\"x = {x:.4f}, y = {y:.4f}\".format(**batch))\n x = 0.5488, y = 0.7152\n x = 0.6028, y = 0.5449\n x = 0.4237, y = 0.6459\n x = 0.4376, y = 0.8918\n\nThis class implements `__enter__` and `__exit__`, and can be used\nin `with` blocks like a normal file. (See the usage example above.)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-----------|--------------------------------------------------------------------------------------------------------|\n| `path` | The path to the TFRecords file. |\n| `options` | (optional) String specifying compression type, `TFRecordCompressionType`, or `TFRecordOptions` object. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|---------------------------------------------------------------|\n| `IOError` | If `path` cannot be opened for writing. |\n| `ValueError` | If valid compression_type can't be determined from `options`. |\n\n\u003cbr /\u003e\n\nMethods\n-------\n\n### `close`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/lib/io/tf_record.py#L315-L317) \n\n close()\n\nClose the file.\n\n### `flush`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/lib/io/tf_record.py#L311-L313) \n\n flush()\n\nFlush the file.\n\n### `write`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/lib/io/tf_record.py#L303-L309) \n\n write(\n record\n )\n\nWrite a string record to the file.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|----------|-----|\n| `record` | str |\n\n\u003cbr /\u003e\n\n### `__enter__`\n\n __enter__()\n\n**enter**(self: object) -\\\u003e object\n\n### `__exit__`\n\n __exit__()\n\n**exit**(self: tensorflow.python.lib.io._pywrap_record_io.RecordWriter, \\*args) -\\\u003e None"]]