tff.to_value
Stay organized with collections
Save and categorize content based on your preferences.
Converts the argument into an instance of the abstract class tff.Value
.
tff.to_value(
arg: object,
type_spec: Optional[tff.types.Type
],
*,
parameter_type_hint=None,
zip_if_needed: bool = False
) -> tff.Value
Instances of tff.Value
represent TFF values that appear internally in
federated computations. This helper function can be used to wrap a variety of
Python objects as tff.Value
instances to allow them to be passed as
arguments, used as functions, or otherwise manipulated within bodies of
federated computations.
At the moment, the supported types include:
Simple constants of str
, int
, float
, and bool
types, mapped to
values of a TFF tensor type.
Numpy arrays (np.ndarray
objects), also mapped to TFF tensors.
Dictionaries (collections.OrderedDict
and unordered dict
), list
s,
tuple
s, namedtuple
s, and Struct
s, all of which are mapped to
TFF tuple type.
Computations (constructed with either the tff.tensorflow.computation
or
with the tff.federated_computation
decorator), typically mapped to TFF
functions.
Placement literals (tff.CLIENTS
, tff.SERVER
), mapped to values of the
TFF placement type.
This function is also invoked when attempting to execute a TFF computation.
All arguments supplied in the invocation are converted into TFF values prior
to execution. The types of Python objects that can be passed as arguments to
computations thus matches the types listed here.
Args |
arg
|
An instance of one of the Python types that are convertible to TFF
values (instances of tff.Value ).
|
type_spec
|
An optional type specifier that allows for disambiguating the
target type (e.g., when two TFF types can be mapped to the same Python
representations). If not specified, TFF tried to determine the type of the
TFF value automatically.
|
parameter_type_hint
|
An optional tff.Type or value convertible to it by
tff.to_type() which specifies an argument type to use in the case that
arg is a polymorphic_computation.PolymorphicComputation .
|
zip_if_needed
|
If True , attempt to coerce the result of to_value to
match type_spec by applying intrinsics.federated_zip to appropriate
elements.
|
Returns |
An instance of tff.Value as described above.
|
Raises |
TypeError
|
if arg is of an unsupported type, or of a type that does not
match type_spec . Raises explicit error message if TensorFlow constructs
are encountered, as TensorFlow code should be sealed away from TFF
federated context.
|
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-09-20 UTC.
[null,null,["Last updated 2024-09-20 UTC."],[],[],null,["# tff.to_value\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/federated/blob/v0.87.0 Version 2.0, January 2004 Licensed under the Apache License, Version 2.0 (the) |\n\nConverts the argument into an instance of the abstract class [`tff.Value`](../tff/Value). \n\n tff.to_value(\n arg: object,\n type_spec: Optional[../tff/types/Type],\n *,\n parameter_type_hint=None,\n zip_if_needed: bool = False\n ) -\u003e ../tff/Value\n\nInstances of [`tff.Value`](../tff/Value) represent TFF values that appear internally in\nfederated computations. This helper function can be used to wrap a variety of\nPython objects as [`tff.Value`](../tff/Value) instances to allow them to be passed as\narguments, used as functions, or otherwise manipulated within bodies of\nfederated computations.\n\nAt the moment, the supported types include:\n\n- Simple constants of `str`, `int`, `float`, and `bool` types, mapped to\n values of a TFF tensor type.\n\n- Numpy arrays (`np.ndarray` objects), also mapped to TFF tensors.\n\n- Dictionaries (`collections.OrderedDict` and unordered `dict`), `list`s,\n `tuple`s, `namedtuple`s, and `Struct`s, all of which are mapped to\n TFF tuple type.\n\n- Computations (constructed with either the [`tff.tensorflow.computation`](../tff/tensorflow/computation) or\n with the [`tff.federated_computation`](../tff/federated_computation) decorator), typically mapped to TFF\n functions.\n\n- Placement literals ([`tff.CLIENTS`](../tff#CLIENTS), [`tff.SERVER`](../tff#SERVER)), mapped to values of the\n TFF placement type.\n\nThis function is also invoked when attempting to execute a TFF computation.\nAll arguments supplied in the invocation are converted into TFF values prior\nto execution. The types of Python objects that can be passed as arguments to\ncomputations thus matches the types listed here.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-----------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `arg` | An instance of one of the Python types that are convertible to TFF values (instances of [`tff.Value`](../tff/Value)). |\n| `type_spec` | An optional type specifier that allows for disambiguating the target type (e.g., when two TFF types can be mapped to the same Python representations). If not specified, TFF tried to determine the type of the TFF value automatically. |\n| `parameter_type_hint` | An optional [`tff.Type`](../tff/types/Type) or value convertible to it by [`tff.to_type()`](../tff/types/to_type) which specifies an argument type to use in the case that `arg` is a `polymorphic_computation.PolymorphicComputation`. |\n| `zip_if_needed` | If `True`, attempt to coerce the result of `to_value` to match `type_spec` by applying `intrinsics.federated_zip` to appropriate elements. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| An instance of [`tff.Value`](../tff/Value) as described above. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|-------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `TypeError` | if `arg` is of an unsupported type, or of a type that does not match `type_spec`. Raises explicit error message if TensorFlow constructs are encountered, as TensorFlow code should be sealed away from TFF federated context. |\n\n\u003cbr /\u003e"]]