mlx.utils.tree_map_with_path#
- tree_map_with_path(fn: Callable, tree: Any, *rest: Any, is_leaf: Callable | None = None, path: Any | None = None) Any#
將
fn套用到 Python 樹tree的路徑與葉節點,並回傳包含結果的新集合。This function is the same
tree_map()but thefntakes the path as the first argument followed by the remaining tree nodes.- 參數:
fn (callable) -- The function that processes the leaves of the tree.
tree (Any) -- The main Python tree that will be iterated upon.
rest (tuple[Any]) -- Extra trees to be iterated together with
tree.is_leaf (Optional[Callable]) -- An optional callable that returns
Trueif the passed object is considered a leaf orFalseotherwise.path (Optional[Any]) -- Prefix will be added to the result.
- 回傳:
A Python tree with the new values returned by
fn.
範例
>>> from mlx.utils import tree_map_with_path >>> tree = {"model": [{"w": 0, "b": 1}, {"w": 0, "b": 1}]} >>> new_tree = tree_map_with_path(lambda path, _: print(path), tree) model.0.w model.0.b model.1.w model.1.b