tf.nest.flatten
Stay organized with collections
Save and categorize content based on your preferences.
Returns a flat list from a given nested structure.
tf.nest.flatten(
structure, expand_composites=False
)
If nest is not a structure , tuple (or a namedtuple), dict, or an attrs class,
then returns a single-element list:
[nest].
In the case of dict instances, the sequence consists of the values, sorted by
key to ensure deterministic behavior. This is true also for OrderedDict
instances: their sequence order is ignored, the sorting order of keys is used
instead. The same convention is followed in pack_sequence_as. This correctly
repacks dicts and OrderedDicts after they have been flattened, and also allows
flattening an OrderedDict and then repacking it back using a corresponding
plain dict, or vice-versa. Dictionaries with non-sortable keys cannot be
flattened.
Users must not modify any collections used in nest while this function is
running.
Examples:
- Python dict (ordered by key):
dict = { "key3": "value3", "key1": "value1", "key2": "value2" }
tf.nest.flatten(dict)
['value1', 'value2', 'value3']
- For a nested python tuple:
tuple = ((1.0, 2.0), (3.0, 4.0, 5.0), (6.0))
tf.nest.flatten(tuple)
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
- Numpy array (will not flatten):
array = np.array([[1, 2], [3, 4]])
tf.nest.flatten(array)
[array([[1, 2],
[3, 4]])]
tf.Tensor
(will not flatten):
tensor = tf.constant([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]])
tf.nest.flatten(tensor)
[<tf.Tensor: shape=(3, 3), dtype=float32, numpy=
array([[1., 2., 3.],
[4., 5., 6.],
[7., 8., 9.]], dtype=float32)>]
Args |
structure
|
an arbitrarily nested structure. Note, numpy arrays are
considered atoms and are not flattened.
|
expand_composites
|
If true, then composite tensors such as tf.SparseTensor
and tf.RaggedTensor are expanded into their component tensors.
|
Returns |
A Python list, the flattened version of the input.
|
Raises |
TypeError
|
The nest is or contains a dict with non-sortable keys.
|
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.
Last updated 2020-10-01 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2020-10-01 UTC."],[],[],null,["# tf.nest.flatten\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------|\n| [TensorFlow 1 version](/versions/r1.15/api_docs/python/tf/nest/flatten) | [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.2.0/tensorflow/python/util/nest.py#L259-L322) |\n\nReturns a flat list from a given nested structure.\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.nest.flatten`](/api_docs/python/tf/nest/flatten)\n\n\u003cbr /\u003e\n\n tf.nest.flatten(\n structure, expand_composites=False\n )\n\nIf nest is not a structure , tuple (or a namedtuple), dict, or an attrs class,\nthen returns a single-element list:\n\\[nest\\].\n\nIn the case of dict instances, the sequence consists of the values, sorted by\nkey to ensure deterministic behavior. This is true also for OrderedDict\ninstances: their sequence order is ignored, the sorting order of keys is used\ninstead. The same convention is followed in pack_sequence_as. This correctly\nrepacks dicts and OrderedDicts after they have been flattened, and also allows\nflattening an OrderedDict and then repacking it back using a corresponding\nplain dict, or vice-versa. Dictionaries with non-sortable keys cannot be\nflattened.\n\nUsers must not modify any collections used in nest while this function is\nrunning.\n\n#### Examples:\n\n1. Python dict (ordered by key):\n\n dict = { \"key3\": \"value3\", \"key1\": \"value1\", \"key2\": \"value2\" }\n tf.nest.flatten(dict)\n ['value1', 'value2', 'value3']\n\n1. For a nested python tuple:\n\n tuple = ((1.0, 2.0), (3.0, 4.0, 5.0), (6.0))\n tf.nest.flatten(tuple)\n [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]\n\n1. Numpy array (will not flatten):\n\n array = np.array([[1, 2], [3, 4]])\n tf.nest.flatten(array)\n [array([[1, 2],\n [3, 4]])]\n\n1. [`tf.Tensor`](../../tf/Tensor) (will not flatten):\n\n tensor = tf.constant([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]])\n tf.nest.flatten(tensor)\n [\u003ctf.Tensor: shape=(3, 3), dtype=float32, numpy=\n array([[1., 2., 3.],\n [4., 5., 6.],\n [7., 8., 9.]], dtype=float32)\u003e]\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------------------|------------------------------------------------------------------------------------------------------------------------|\n| `structure` | an arbitrarily nested structure. Note, numpy arrays are considered atoms and are not flattened. |\n| `expand_composites` | If true, then composite tensors such as tf.SparseTensor and tf.RaggedTensor are expanded into their component tensors. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A Python list, the flattened version of the input. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|-------------|--------------------------------------------------------|\n| `TypeError` | The nest is or contains a dict with non-sortable keys. |\n\n\u003cbr /\u003e"]]