tf.nest.map_structure
Stay organized with collections
Save and categorize content based on your preferences.
Creates a new structure by applying func
to each atom in structure
.
tf.nest.map_structure(
func, *structure, **kwargs
)
Used in the notebooks
Used in the guide |
Used in the tutorials |
|
|
Refer to tf.nest
for the definition of a structure.
Applies func(x[0], x[1], ...)
where x[i] enumerates all atoms in
structure[i]
. All items in structure
must have the same arity,
and the return value will contain results with the same structure layout.
Examples:
a = {"hello": 24, "world": 76}
tf.nest.map_structure(lambda p: p * 2, a)
{'hello': 48, 'world': 152}
- Multiple Python dictionaries:
d1 = {"hello": 24, "world": 76}
d2 = {"hello": 36, "world": 14}
tf.nest.map_structure(lambda p1, p2: p1 + p2, d1, d2)
{'hello': 60, 'world': 90}
a = [24, 76, "ab"]
tf.nest.map_structure(lambda p: p * 2, a)
[48, 152, 'abab']
tf.nest.map_structure(lambda x, y: x + y, 3, 4)
7
tf.nest.map_structure(lambda x: x + 1, ())
()
- Check the types of iterables:
s1 = (((1, 2), 3), 4, (5, 6))
s1_list = [[[1, 2], 3], 4, [5, 6]]
tf.nest.map_structure(lambda x, y: None, s1, s1_list)
Traceback (most recent call last):
TypeError: The two structures don't have the same nested structure
- Type check is set to False:
s1 = (((1, 2), 3), 4, (5, 6))
s1_list = [[[1, 2], 3], 4, [5, 6]]
tf.nest.map_structure(lambda x, y: None, s1, s1_list, check_types=False)
(((None, None), None), None, (None, None))
Args |
func
|
A callable that accepts as many arguments as there are structures.
|
*structure
|
atom or nested structure.
|
**kwargs
|
Valid keyword args are:
check_types : If set to True (default) the types of iterables within
the structures have to be same (e.g. map_structure(func, [1], (1,))
raises a TypeError exception). To allow this set this argument to
False . Note that namedtuples with identical name and fields are always
considered to have the same shallow structure.
expand_composites : If set to True , then composite tensors such as
tf.sparse.SparseTensor and tf.RaggedTensor are expanded into their
component tensors. If False (the default), then composite tensors are
not expanded.
|
Returns |
A new structure with the same arity as structure[0] , whose atoms
correspond to func(x[0], x[1], ...) where x[i] is the atom in the
corresponding location in structure[i] . If there are different structure
types and check_types is False the structure types of the first
structure will be used.
|
Raises |
TypeError
|
If func is not callable or if the structures do not match
each other by depth tree.
|
ValueError
|
If no structure is provided or if the structures do not match
each other by type.
|
ValueError
|
If wrong keyword arguments are provided.
|
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.nest.map_structure\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/util/nest.py#L542-L630) |\n\nCreates a new structure by applying `func` to each atom in `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.map_structure`](https://www.tensorflow.org/api_docs/python/tf/nest/map_structure)\n\n\u003cbr /\u003e\n\n tf.nest.map_structure(\n func, *structure, **kwargs\n )\n\n### Used in the notebooks\n\n| Used in the guide | Used in the tutorials |\n|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Import a JAX model using JAX2TF](https://www.tensorflow.org/guide/jax2tf) - [Distributed training with Core APIs and DTensor](https://www.tensorflow.org/guide/core/distribution) | - [Distributed training with DTensors](https://www.tensorflow.org/tutorials/distribute/dtensor_ml_tutorial) - [Substantial Undocumented Infection Facilitates the Rapid Dissemination of Novel Coronavirus (SARS-CoV2)](https://www.tensorflow.org/probability/examples/Undocumented_Infection_and_the_Dissemination_of_SARS-CoV2) - [Use TFF optimizers in custom iterative process](https://www.tensorflow.org/federated/tutorials/custom_federated_algorithm_with_tff_optimizers) - [Variational Inference on Probabilistic Graphical Models with Joint Distributions](https://www.tensorflow.org/probability/examples/Variational_Inference_and_Joint_Distributions) - [Composing Learning Algorithms](https://www.tensorflow.org/federated/tutorials/composing_learning_algorithms) |\n\nRefer to [tf.nest](https://www.tensorflow.org/api_docs/python/tf/nest)\nfor the definition of a structure.\n\nApplies `func(x[0], x[1], ...)` where x\\[i\\] enumerates all atoms in\n`structure[i]`. All items in `structure` must have the same arity,\nand the return value will contain results with the same structure layout.\n\n#### Examples:\n\n- A single Python dict:\n\n a = {\"hello\": 24, \"world\": 76}\n tf.nest.map_structure(lambda p: p * 2, a)\n {'hello': 48, 'world': 152}\n\n- Multiple Python dictionaries:\n\n d1 = {\"hello\": 24, \"world\": 76}\n d2 = {\"hello\": 36, \"world\": 14}\n tf.nest.map_structure(lambda p1, p2: p1 + p2, d1, d2)\n {'hello': 60, 'world': 90}\n\n- A single Python list:\n\n a = [24, 76, \"ab\"]\n tf.nest.map_structure(lambda p: p * 2, a)\n [48, 152, 'abab']\n\n- Scalars:\n\n tf.nest.map_structure(lambda x, y: x + y, 3, 4)\n 7\n\n- Empty structures:\n\n tf.nest.map_structure(lambda x: x + 1, ())\n ()\n\n- Check the types of iterables:\n\n s1 = (((1, 2), 3), 4, (5, 6))\n s1_list = [[[1, 2], 3], 4, [5, 6]]\n tf.nest.map_structure(lambda x, y: None, s1, s1_list)\n Traceback (most recent call last):\n\n TypeError: The two structures don't have the same nested structure\n\n- Type check is set to False:\n\n s1 = (((1, 2), 3), 4, (5, 6))\n s1_list = [[[1, 2], 3], 4, [5, 6]]\n tf.nest.map_structure(lambda x, y: None, s1, s1_list, check_types=False)\n (((None, None), None), None, (None, None))\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|--------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `func` | A callable that accepts as many arguments as there are structures. |\n| `*structure` | atom or nested structure. |\n| `**kwargs` | Valid keyword args are: \u003cbr /\u003e - `check_types`: If set to `True` (default) the types of iterables within the structures have to be same (e.g. `map_structure(func, [1], (1,))` raises a `TypeError` exception). To allow this set this argument to `False`. Note that namedtuples with identical name and fields are always considered to have the same shallow structure. - `expand_composites`: If set to `True`, then composite tensors such as [`tf.sparse.SparseTensor`](../../tf/sparse/SparseTensor) and [`tf.RaggedTensor`](../../tf/RaggedTensor) are expanded into their component tensors. If `False` (the default), then composite tensors are not expanded. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A new structure with the same arity as `structure[0]`, whose atoms correspond to `func(x[0], x[1], ...)` where `x[i]` is the atom in the corresponding location in `structure[i]`. If there are different structure types and `check_types` is `False` the structure types of the first structure will be used. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|---------------------------------------------------------------------------------------|\n| `TypeError` | If `func` is not callable or if the structures do not match each other by depth tree. |\n| `ValueError` | If no structure is provided or if the structures do not match each other by type. |\n| `ValueError` | If wrong keyword arguments are provided. |\n\n\u003cbr /\u003e"]]