tf_agents.utils.nest_utils.prune_extra_keys
Stay organized with collections
Save and categorize content based on your preferences.
Recursively prunes keys from wide
if they don't appear in narrow
.
tf_agents.utils.nest_utils.prune_extra_keys(
narrow, wide
)
Often used as preprocessing prior to calling tf.nest.flatten
or tf.nest.map_structure
.
This function is more forgiving than the ones in nest
; if two substructures'
types or structures don't agree, we consider it invalid and prune_extra_keys
will return the wide
substructure as is. Typically, additional checking is
needed: you will also want to use
nest.assert_same_structure(narrow, prune_extra_keys(narrow, wide))
to ensure the result of pruning is still a correct structure.
Examples:
wide = [{"a": "a", "b": "b"}]
# Narrows 'wide'
assert prune_extra_keys([{"a": 1}], wide) == [{"a": "a"}]
# 'wide' lacks "c", is considered invalid.
assert prune_extra_keys([{"c": 1}], wide) == wide
# 'wide' contains a different type from 'narrow', is considered invalid
assert prune_extra_keys("scalar", wide) == wide
# 'wide' substructure for key "d" does not match the one in 'narrow' and
# therefore is returned unmodified.
assert (prune_extra_keys({"a": {"b": 1}, "d": None},
{"a": {"b": "b", "c": "c"}, "d": [1, 2]})
== {"a": {"b": "b"}, "d": [1, 2]})
# assert prune_extra_keys((), wide) == ()
# assert prune_extra_keys({"a": ()}, wide) == {"a": ()}
Args |
narrow
|
A nested structure.
|
wide
|
A nested structure that may contain dicts with more fields than
narrow .
|
Returns |
A structure with the same nested substructures as wide , but with
dicts whose entries are limited to the keys found in the associated
substructures of narrow .
In case of substructure or size mismatches, the returned substructures
will be returned as is. Note that ObjectProxy-wrapped objects are
considered equivalent to their non-ObjectProxy types.
|
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.
Last updated 2024-04-26 UTC.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf_agents.utils.nest_utils.prune_extra_keys\n\n\u003cbr /\u003e\n\n|--------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/agents/blob/v0.19.0/tf_agents/utils/nest_utils.py#L195-L311) |\n\nRecursively prunes keys from `wide` if they don't appear in `narrow`. \n\n tf_agents.utils.nest_utils.prune_extra_keys(\n narrow, wide\n )\n\nOften used as preprocessing prior to calling [`tf.nest.flatten`](https://www.tensorflow.org/api_docs/python/tf/nest/flatten)\nor [`tf.nest.map_structure`](https://www.tensorflow.org/api_docs/python/tf/nest/map_structure).\n\nThis function is more forgiving than the ones in `nest`; if two substructures'\ntypes or structures don't agree, we consider it invalid and `prune_extra_keys`\nwill return the `wide` substructure as is. Typically, additional checking is\nneeded: you will also want to use\n`nest.assert_same_structure(narrow, prune_extra_keys(narrow, wide))`\nto ensure the result of pruning is still a correct structure.\n\n#### Examples:\n\n wide = [{\"a\": \"a\", \"b\": \"b\"}]\n # Narrows 'wide'\n assert prune_extra_keys([{\"a\": 1}], wide) == [{\"a\": \"a\"}]\n # 'wide' lacks \"c\", is considered invalid.\n assert prune_extra_keys([{\"c\": 1}], wide) == wide\n # 'wide' contains a different type from 'narrow', is considered invalid\n assert prune_extra_keys(\"scalar\", wide) == wide\n # 'wide' substructure for key \"d\" does not match the one in 'narrow' and\n # therefore is returned unmodified.\n assert (prune_extra_keys({\"a\": {\"b\": 1}, \"d\": None},\n {\"a\": {\"b\": \"b\", \"c\": \"c\"}, \"d\": [1, 2]})\n == {\"a\": {\"b\": \"b\"}, \"d\": [1, 2]})\n # assert prune_extra_keys((), wide) == ()\n # assert prune_extra_keys({\"a\": ()}, wide) == {\"a\": ()}\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|----------|---------------------------------------------------------------------------|\n| `narrow` | A nested structure. |\n| `wide` | A nested structure that may contain dicts with more fields than `narrow`. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A structure with the same nested substructures as `wide`, but with dicts whose entries are limited to the keys found in the associated substructures of `narrow`. \u003cbr /\u003e In case of substructure or size mismatches, the returned substructures will be returned as is. Note that ObjectProxy-wrapped objects are considered equivalent to their non-ObjectProxy types. ||\n\n\u003cbr /\u003e"]]