tf.keras.tree.traverse
Stay organized with collections
Save and categorize content based on your preferences.
Traverses the given nested structure, applying the given function.
tf.keras.tree.traverse(
func, structure, top_down=True
)
The traversal is depth-first. If top_down
is True (default), parents
are returned before their children (giving the option to avoid traversing
into a sub-tree).
Examples:
v = []
keras.tree.traverse(v.append, [(1, 2), [3], {"a": 4}], top_down=True)
[(1, 2), [3], {'a': 4}]
v
[[(1, 2), [3], {'a': 4}], (1, 2), 1, 2, [3], 3, {'a': 4}, 4]
v = []
keras.tree.traverse(v.append, [(1, 2), [3], {"a": 4}], top_down=False)
[(1, 2), [3], {'a': 4}]
v
[1, 2, (1, 2), 3, [3], 4, {'a': 4}, [(1, 2), [3], {'a': 4}]]
Args |
func
|
The function to be applied to each sub-nest of the structure.
When traversing top-down:
If func(subtree) is None the traversal continues into the
sub-tree.
If func(subtree) is not None the traversal does not continue
into the sub-tree. The sub-tree will be replaced by func(subtree)
in the returned structure (to replace the sub-tree with None , use
the special value _MAP_TO_NONE ).
When traversing bottom-up:
If func(subtree) is None the traversed sub-tree is returned
unaltered.
If func(subtree) is not None the sub-tree will be replaced by
func(subtree) in the returned structure (to replace the sub-tree
with None, use the special value _MAP_TO_NONE ).
|
structure
|
The structure to traverse.
|
top_down
|
If True, parent structures will be visited before their
children.
|
Returns |
The structured output from the traversal.
|
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-06-07 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-06-07 UTC."],[],[],null,["# tf.keras.tree.traverse\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/tree/tree_api.py#L41-L88) |\n\nTraverses the given nested structure, applying the given function. \n\n tf.keras.tree.traverse(\n func, structure, top_down=True\n )\n\nThe traversal is depth-first. If `top_down` is True (default), parents\nare returned before their children (giving the option to avoid traversing\ninto a sub-tree).\n\n#### Examples:\n\n v = []\n keras.tree.traverse(v.append, [(1, 2), [3], {\"a\": 4}], top_down=True)\n [(1, 2), [3], {'a': 4}]\n v\n [[(1, 2), [3], {'a': 4}], (1, 2), 1, 2, [3], 3, {'a': 4}, 4]\n\n v = []\n keras.tree.traverse(v.append, [(1, 2), [3], {\"a\": 4}], top_down=False)\n [(1, 2), [3], {'a': 4}]\n v\n [1, 2, (1, 2), 3, [3], 4, {'a': 4}, [(1, 2), [3], {'a': 4}]]\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `func` | The function to be applied to each sub-nest of the structure. \u003cbr /\u003e When traversing top-down: If `func(subtree) is None` the traversal continues into the sub-tree. If `func(subtree) is not None` the traversal does not continue into the sub-tree. The sub-tree will be replaced by `func(subtree)` in the returned structure (to replace the sub-tree with `None`, use the special value `_MAP_TO_NONE`). When traversing bottom-up: If `func(subtree) is None` the traversed sub-tree is returned unaltered. If `func(subtree) is not None` the sub-tree will be replaced by `func(subtree)` in the returned structure (to replace the sub-tree with None, use the special value `_MAP_TO_NONE`). |\n| `structure` | The structure to traverse. |\n| `top_down` | If True, parent structures will be visited before their children. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| The structured output from the traversal. ||\n\n\u003cbr /\u003e"]]