On this page
tf.nest.map_structure
Applies func to each entry in structure and returns a new structure.
tf.nest.map_structure(
    func, *structure, **kwargs
)
Applies func(x[0], x[1], ...) where x[i] is an entry in structure[i]. All structures in structure must have the same arity, and the return value will contain results with the same structure layout.
Examples:
- A single Python dict:
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}
| Args | |
|---|---|
| func | A callable that accepts as many arguments as there are structures. | 
| *structure | scalar, or tuple or dict or list of constructed scalars and/or other tuples/lists, or scalars. Note: numpy arrays are considered as scalars. | 
| **kwargs | Valid keyword args are: 
 | 
| Returns | |
|---|---|
| A new structure with the same arity as structure, whose values correspond tofunc(x[0], x[1], ...)wherex[i]is a value in the corresponding location instructure[i]. If there are different sequence types andcheck_typesisFalsethe sequence types of the first structure will be used. | 
| Raises | |
|---|---|
| TypeError | If funcis 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. | 
© 2020 The TensorFlow Authors. All rights reserved.
Licensed under the Creative Commons Attribution License 3.0.
Code samples licensed under the Apache 2.0 License.
 https://www.tensorflow.org/versions/r2.4/api_docs/python/tf/nest/map_structure