Need to inspect or transform nested model parameters without hard-coding their structure? jax.tree_util.tree_map() applies a function across corresponding leaves of pytree arguments and returns a new pytree. It works like Python’s map(), but understands containers such as lists, tuples, dictionaries, namedtuples, OrderedDicts, and None. section 10.1 section 10.4
Inspect neural-network parameter shapes
Given nested weights and biases in params, map a shape-extracting function over every leaf: shapes = jax.tree_util.tree_map(lambda p: p.shape, params) The resulting pytree preserves the parameter layout while replacing each array with its shape, producing entries such as ((2048, 120000), (2048,)) for one layer. This avoids manually unpacking each layer. section 10.1
Use tree_map() when you need to:
- Apply the same leaf-wise operation to nested parameter or data structures. section 10.1
- Preserve the original pytree structure while changing its leaves. section 10.1
- Pair it with JAX transformations and functions that accept or return pytrees of arrays. section 10.1
