ML Community Day is November 9! Join us for updates from TensorFlow, JAX, and more Learn more

tf.nest.map_structure

Applies func to each entry in structure and returns a new structure.

Used in the notebooks

Used in the tutorials

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}
  • A single Python list:
a = [24, 76, "ab"]
tf.nest.map_structure(lambda p: p * 2, a)
[48, 152, 'abab']
  • Scalars:
tf.nest.map_structure(lambda x, y: x + y, 3, 4)
7
  • Empty structures:
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))

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:

  • 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 al