tf.train.Example
Stay organized with collections Save and categorize content based on your preferences.
An Example
is a standard proto storing data for training and inference.
Used in the notebooks
Used in the guide | Used in the tutorials |
| |
An Example
proto is a representation of the following python type:
Dict[str, Union[List[bytes], List[int64], List[float]]]
It contains a key-value store Example.features
where each key (string) maps to a tf.train.Feature
message which contains a fixed-type list. This flexible and compact format allows the storage of large amounts of typed data, but requires that the data shape and use be determined by the configuration files and parsers that are used to read and write this format (refer to tf.io.parse_example
for details).
from google.protobuf import text_format
example = text_format.Parse('''
features {
feature {key: "my_feature"
value {int64_list {value: [1, 2, 3, 4]} } }
}''',
tf.train.Example())
Use tf.io.parse_example
to extract tensors from a serialized Example
proto:
tf.io.parse_example(
example.SerializeToString(),
features = {'my_feature': tf.io.RaggedFeature(dtype=tf.int64)})
{'my_feature': <tf.Tensor: shape=(4,), dtype=float32,
numpy=array([1, 2, 3, 4], dtype=int64)>}
While the list of keys, and the contents of each key could be different for every Example
, TensorFlow expects a fixed list of keys, each with a fixed tf.dtype
. A conformant Example
dataset obeys the following conventions:
- If a Feature
K
exists in one example with data type T
, it must be of type T
in all other examples when present. It may be omitted. - The number of instances of Feature
K
list data may vary across examples, depending on the requirements of the model. - If a Feature
K
doesn't exist in an example, a K
-specific default will be used, if configured. - If a Feature
K
exists in an example but contains no items, the intent is considered to be an empty tensor and no default will be used.
Attributes |
features | Features features |
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.train.Example\n\n\u003cbr /\u003e\n\n|----------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/core/example/example.proto) |\n\nAn `Example` is a standard proto storing data for training and inference.\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.train.Example`](https://www.tensorflow.org/api_docs/python/tf/train/Example)\n\n\u003cbr /\u003e\n\n### Used in the notebooks\n\n| Used in the guide | Used in the tutorials |\n|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [tf.data: Build TensorFlow input pipelines](https://www.tensorflow.org/guide/data) - [Estimators](https://www.tensorflow.org/guide/estimator) - [Ragged tensors](https://www.tensorflow.org/guide/ragged_tensor) | - [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) - [TensorFlow Ranking Keras pipeline for distributed training](https://www.tensorflow.org/ranking/tutorials/ranking_dnn_distributed) - [FaceSSD Fairness Indicators Example Colab](https://www.tensorflow.org/responsible_ai/fairness_indicators/tutorials/Facessd_Fairness_Indicators_Example_Colab) - [Graph regularization for sentiment classification using synthesized graphs](https://www.tensorflow.org/neural_structured_learning/tutorials/graph_keras_lstm_imdb) |\n\nAn `Example` proto is a representation of the following python type: \n\n Dict[str,\n Union[List[bytes],\n List[int64],\n List[float]]]\n\nIt contains a key-value store [`Example.features`](../../tf/train/Example#features) where each key (string) maps\nto a [`tf.train.Feature`](../../tf/train/Feature) message which contains a fixed-type list. This flexible\nand compact format allows the storage of large amounts of typed data, but\nrequires that the data shape and use be determined by the configuration files\nand parsers that are used to read and write this format (refer to\n[`tf.io.parse_example`](../../tf/io/parse_example) for details). \n\n from google.protobuf import text_format\n example = text_format.Parse('''\n features {\n feature {key: \"my_feature\"\n value {int64_list {value: [1, 2, 3, 4]} } }\n }''',\n tf.train.Example())\n\nUse [`tf.io.parse_example`](../../tf/io/parse_example) to extract tensors from a serialized `Example` proto: \n\n tf.io.parse_example(\n example.SerializeToString(),\n features = {'my_feature': tf.io.RaggedFeature(dtype=tf.int64)})\n {'my_feature': \u003ctf.Tensor: shape=(4,), dtype=float32,\n numpy=array([1, 2, 3, 4], dtype=int64)\u003e}\n\nWhile the list of keys, and the contents of each key *could* be different for\nevery `Example`, TensorFlow expects a fixed list of keys, each with a fixed\n`tf.dtype`. A conformant `Example` dataset obeys the following conventions:\n\n- If a Feature `K` exists in one example with data type `T`, it must be of type `T` in all other examples when present. It may be omitted.\n- The number of instances of Feature `K` list data may vary across examples, depending on the requirements of the model.\n- If a Feature `K` doesn't exist in an example, a `K`-specific default will be used, if configured.\n- If a Feature `K` exists in an example but contains no items, the intent is considered to be an empty tensor and no default will be used.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Attributes ---------- ||\n|------------|---------------------|\n| `features` | `Features features` |\n\n\u003cbr /\u003e"]]