Stay organized with collections
Save and categorize content based on your preferences.
Functions that work with structures.
A structure is either:
one of the recognized Python collections, holding nested structures;
a value of any other type, typically a TensorFlow data type like Tensor,
Variable, or of compatible types such as int, float, ndarray, etc. these are
commonly referred to as atoms of the structure.
A structure of type T is a structure whose atomic items are of type T.
For example, a structure of tf.Tensor only contains tf.Tensor as its atoms.
Historically a nested structure was called a nested sequence in TensorFlow.
A nested structure is sometimes called a nest or a tree, but the formal
name nested structure is preferred.
Any other values are considered atoms. Not all collection types are
considered nested structures. For example, the following types are
considered atoms:
set; {"a", "b"} is an atom, while ["a", "b"] is a nested structure.
[[["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 2023-10-06 UTC."],[],[],null,["# Module: tf.nest\n\n\u003cbr /\u003e\n\nFunctions that work with structures.\n\n#### A structure is either:\n\n- one of the recognized Python collections, holding *nested structures*;\n- a value of any other type, typically a TensorFlow data type like Tensor, Variable, or of compatible types such as int, float, ndarray, etc. these are commonly referred to as *atoms* of the structure.\n\nA structure of type `T` is a structure whose atomic items are of type `T`.\nFor example, a structure of [`tf.Tensor`](../tf/Tensor) only contains [`tf.Tensor`](../tf/Tensor) as its atoms.\n\nHistorically a *nested structure* was called a *nested sequence* in TensorFlow.\nA nested structure is sometimes called a *nest* or a *tree* , but the formal\nname *nested structure* is preferred.\n\nRefer to [Nesting Data Structures](https://en.wikipedia.org/wiki/Nesting_(computing)#Data_structures).\n\nThe following collection types are recognized by [`tf.nest`](../tf/nest) as nested\nstructures:\n\n- `collections.abc.Sequence` (except `string` and `bytes`). This includes `list`, `tuple`, and `namedtuple`.\n- `collections.abc.Mapping` (with sortable keys). This includes `dict` and `collections.OrderedDict`.\n- `collections.abc.MappingView` (with sortable keys).\n- [`attr.s` classes](https://www.attrs.org/).\n\nAny other values are considered **atoms**. Not all collection types are\nconsidered nested structures. For example, the following types are\nconsidered atoms:\n\n- `set`; `{\"a\", \"b\"}` is an atom, while `[\"a\", \"b\"]` is a nested structure.\n- [`dataclass` classes](https://docs.python.org/library/dataclasses.html)\n- [`tf.Tensor`](../tf/Tensor)\n- `numpy.array`\n\n[`tf.nest.is_nested`](../tf/nest/is_nested) checks whether an object is a nested structure or an atom.\nFor example:\n\n\u003cbr /\u003e\n\n tf.nest.is_nested(\"1234\")\n False\n tf.nest.is_nested([1, 3, [4, 5]])\n True\n tf.nest.is_nested(((7, 8), (5, 6)))\n True\n tf.nest.is_nested([])\n True\n tf.nest.is_nested({\"a\": 1, \"b\": 2})\n True\n tf.nest.is_nested({\"a\": 1, \"b\": 2}.keys())\n True\n tf.nest.is_nested({\"a\": 1, \"b\": 2}.values())\n True\n tf.nest.is_nested({\"a\": 1, \"b\": 2}.items())\n True\n tf.nest.is_nested(set([1, 2]))\n False\n ones = tf.ones([2, 3])\n tf.nest.is_nested(ones)\n False\n \n \n\u003cbr /\u003e\n\n| **Note:** A proper structure shall form a tree. The user shall ensure there is no cyclic references within the items in the structure, i.e., no references in the structure of the input of these functions should be recursive. The behavior is undefined if there is a cycle.\n\nFunctions\n---------\n\n[`assert_same_structure(...)`](../tf/nest/assert_same_structure): Asserts that two structures are nested in the same way.\n\n[`flatten(...)`](../tf/nest/flatten): Returns a flat list from a given structure.\n\n[`is_nested(...)`](../tf/nest/is_nested): Returns true if its input is a nested structure.\n\n[`map_structure(...)`](../tf/nest/map_structure): Creates a new structure by applying `func` to each atom in `structure`.\n\n[`pack_sequence_as(...)`](../tf/nest/pack_sequence_as): Returns a given flattened sequence packed into a given structure."]]