tf_agents.distributions.utils.parameters_to_dict
Stay organized with collections
Save and categorize content based on your preferences.
Converts value
to a nested dict
(excluding all type_
info).
tf_agents.distributions.utils.parameters_to_dict(
value: tf_agents.distributions.utils.Params
,
tensors_only: bool = False
) -> Mapping[Text, Any]
Sub-dicts represent Params
objects; keys represent flattened nest structures
in value.params
.
Example:
scale_matrix = tf.Variable([[1.0, 2.0], [-1.0, 0.0]])
d = tfp.distributions.MultivariateNormalDiag(
loc=[1.0, 1.0], scale_diag=[2.0, 3.0], validate_args=True)
b = tfp.bijectors.ScaleMatvecLinearOperator(
scale=tf.linalg.LinearOperatorFullMatrix(matrix=scale_matrix),
adjoint=True)
b_d = b(d)
p = utils.get_parameters(b_d)
params_dict = utils.parameters_to_dict(p, tensors_only)
results in the nested dictionary, if tensors_only=False
:
{
"bijector": {"adjoint": True,
"scale": {"matrix": scale_matrix} },
"distribution": {"validate_args": True,
# These are deeply nested because we passed lists
# intead of numpy arrays for `loc` and `scale_diag`.
"scale_diag:0": 2.0,
"scale_diag:1": 3.0,
"loc:0": 1.0,
"loc:1": 1.0}
}
results if tensors_only=True
:
{
"bijector": {"scale": {"matrix": scale_matrix} },
"distribution": {},
}
The dictionary may then be modified or updated (e.g., in place), and converted
back to a Params
object using merge_to_parameters_from_dict
.
Args |
value
|
The (possibly recursively defined) Params .
|
tensors_only
|
Whether to include all parameters or only tensors and
TypeSpecs. If True , then only tf.Tensor and tf.TypeSpec leaf
parameters are emitted.
|
Returns |
A dict mapping value.params to flattened key/value pairs. Any
sub-Params objects become nested dicts.
|
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.distributions.utils.parameters_to_dict\n\n\u003cbr /\u003e\n\n|-----------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/agents/blob/v0.19.0/tf_agents/distributions/utils.py#L379-L459) |\n\nConverts `value` to a nested `dict` (excluding all `type_` info). \n\n tf_agents.distributions.utils.parameters_to_dict(\n value: ../../../tf_agents/distributions/utils/Params,\n tensors_only: bool = False\n ) -\u003e Mapping[Text, Any]\n\nSub-dicts represent `Params` objects; keys represent flattened nest structures\nin `value.params`.\n\n#### Example:\n\n scale_matrix = tf.Variable([[1.0, 2.0], [-1.0, 0.0]])\n d = tfp.distributions.MultivariateNormalDiag(\n loc=[1.0, 1.0], scale_diag=[2.0, 3.0], validate_args=True)\n b = tfp.bijectors.ScaleMatvecLinearOperator(\n scale=tf.linalg.LinearOperatorFullMatrix(matrix=scale_matrix),\n adjoint=True)\n b_d = b(d)\n p = utils.get_parameters(b_d)\n params_dict = utils.parameters_to_dict(p, tensors_only)\n\nresults in the nested dictionary, if `tensors_only=False`: \n\n {\n \"bijector\": {\"adjoint\": True,\n \"scale\": {\"matrix\": scale_matrix} },\n \"distribution\": {\"validate_args\": True,\n # These are deeply nested because we passed lists\n # intead of numpy arrays for `loc` and `scale_diag`.\n \"scale_diag:0\": 2.0,\n \"scale_diag:1\": 3.0,\n \"loc:0\": 1.0,\n \"loc:1\": 1.0}\n }\n\nresults if `tensors_only=True`: \n\n {\n \"bijector\": {\"scale\": {\"matrix\": scale_matrix} },\n \"distribution\": {},\n }\n\nThe dictionary may then be modified or updated (e.g., in place), and converted\nback to a `Params` object using `merge_to_parameters_from_dict`.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|----------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `value` | The (possibly recursively defined) `Params`. |\n| `tensors_only` | Whether to include all parameters or only tensors and TypeSpecs. If `True`, then only [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor) and [`tf.TypeSpec`](https://www.tensorflow.org/api_docs/python/tf/TypeSpec) leaf parameters are emitted. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A `dict` mapping `value.params` to flattened key/value pairs. Any sub-`Params` objects become nested dicts. ||\n\n\u003cbr /\u003e"]]