tff.tensorflow.computation
Stay organized with collections
Save and categorize content based on your preferences.
Decorates/wraps Python functions and defuns as TFF TensorFlow computations.
tff.tensorflow.computation(
*args, **kwargs
)
Used in the notebooks
This symbol can be used as either a decorator or a wrapper applied to a
function given to it as an argument. The supported patterns and examples of
usage are as follows:
Convert an existing function inline into a TFF computation. This is the
simplest mode of usage, and how one can embed existing non-TFF code for
use with the TFF framework. In this mode, one invokes
tff.tensorflow.computation
with a pair of arguments, the first being a
function/defun that contains the logic, and the second being the TFF type
of the parameter:
foo = tff.tensorflow.computation(lambda x: x > 10, tf.int32)
After executing the above code snippet, foo
becomes an instance of the
abstract base class Computation
. Like all computations, it has the
type_signature
property:
str(foo.type_signature) == '(int32 -> bool)'
The function passed as a parameter doesn't have to be a lambda, it can
also be an existing Python function or a defun. Here's how to construct
a computation from the standard TensorFlow operator tf.add
:
foo = tff.tensorflow.computation(tf.add, (tf.int32, tf.int32))
The resulting type signature is as expected:
str(foo.type_signature) == '(<int32,int32> -> int32)'
If one intends to create a computation that doesn't accept any arguments,
the type argument is simply omitted. The function must be a no-argument
function as well:
foo = tff.tensorflow.computation(lambda: tf.constant(10))
Decorate a Python function or a TensorFlow defun with a TFF type to wrap
it as a TFF computation. The only difference between this mode of usage
and the one mentioned above is that instead of passing the function/defun
as an argument, tff.tensorflow.computation
along with the optional type specifier
is written above the function/defun's body.
Here's an example of a computation that accepts a parameter:
@tff.tensorflow.computation(tf.int32)
def foo(x):
return x > 10
One can think of this mode of usage as merely a syntactic sugar for the
example already given earlier:
foo = tff.tensorflow.computation(lambda x: x > 10, tf.int32)
Here's an example of a no-parameter computation:
@tff.tensorflow.computation
def foo():
return tf.constant(10)
Again, this is merely syntactic sugar for the example given earlier:
foo = tff.tensorflow.computation(lambda: tf.constant(10))
If the Python function has multiple decorators,
tff.tensorflow.computation
should be the outermost one (the one that
appears first in the sequence).
Create a polymorphic callable to be instantiated based on arguments,
similarly to TensorFlow defuns that have been defined without an input
signature.
This mode of usage is symmetric to those above. One simply omits the type
specifier, and applies tff.tensorflow.computation
as a decorator or
wrapper to a function/defun that does expect parameters.
Here's an example of wrapping a lambda as a polymorphic callable:
foo = tff.tensorflow.computation(lambda x, y: x > y)
The resulting foo
can be used in the same ways as if it were had the
type been declared; the corresponding computation is simply created on
demand, in the same way as how polymorphic TensorFlow defuns create and
cache concrete function definitions for each combination of argument
types.
...foo(1, 2)...
...foo(0.5, 0.3)...
Here's an example of creating a polymorphic callable via decorator:
@tff.tensorflow.computation
def foo(x, y):
return x > y
The syntax is symmetric to all examples already shown.
Args |
*args
|
Either a function/defun, or TFF type spec, or both (function first),
or neither, as documented in the 3 patterns and examples of usage above.
|
Returns |
If invoked with a function as an argument, returns an instance of a TFF
computation constructed based on this function. If called without one, as
in the typical decorator style of usage, returns a callable that expects
to be called with the function definition supplied as a parameter; see the
patterns and examples of usage above.
|
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 2025-01-17 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 2025-01-17 UTC."],[],[],null,["# tff.tensorflow.computation\n\n\u003cbr /\u003e\n\nDecorates/wraps Python functions and defuns as TFF TensorFlow computations. \n\n tff.tensorflow.computation(\n *args, **kwargs\n )\n\n### Used in the notebooks\n\n| Used in the tutorials |\n|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Implementing Custom Aggregations](https://www.tensorflow.org/federated/tutorials/custom_aggregators) - [Sending Different Data To Particular Clients With federated_language.federated_select](https://www.tensorflow.org/federated/tutorials/federated_select) - [Random noise generation in TFF](https://www.tensorflow.org/federated/tutorials/random_noise_generation) - [Custom Federated Algorithms, Part 1: Introduction to the Federated Core](https://www.tensorflow.org/federated/tutorials/custom_federated_algorithms_1) - [Building Your Own Federated Learning Algorithm](https://www.tensorflow.org/federated/tutorials/building_your_own_federated_learning_algorithm) |\n\nThis symbol can be used as either a decorator or a wrapper applied to a\nfunction given to it as an argument. The supported patterns and examples of\nusage are as follows:\n\n1. Convert an existing function inline into a TFF computation. This is the\n simplest mode of usage, and how one can embed existing non-TFF code for\n use with the TFF framework. In this mode, one invokes\n [`tff.tensorflow.computation`](../../tff/tensorflow/computation) with a pair of arguments, the first being a\n function/defun that contains the logic, and the second being the TFF type\n of the parameter:\n\n foo = tff.tensorflow.computation(lambda x: x \u003e 10, tf.int32)\n\n After executing the above code snippet, `foo` becomes an instance of the\n abstract base class `Computation`. Like all computations, it has the\n `type_signature` property: \n\n str(foo.type_signature) == '(int32 -\u003e bool)'\n\n The function passed as a parameter doesn't have to be a lambda, it can\n also be an existing Python function or a defun. Here's how to construct\n a computation from the standard TensorFlow operator [`tf.add`](https://www.tensorflow.org/api_docs/python/tf/math/add): \n\n foo = tff.tensorflow.computation(tf.add, (tf.int32, tf.int32))\n\n The resulting type signature is as expected: \n\n str(foo.type_signature) == '(\u003cint32,int32\u003e -\u003e int32)'\n\n If one intends to create a computation that doesn't accept any arguments,\n the type argument is simply omitted. The function must be a no-argument\n function as well: \n\n foo = tff.tensorflow.computation(lambda: tf.constant(10))\n\n2. Decorate a Python function or a TensorFlow defun with a TFF type to wrap\n it as a TFF computation. The only difference between this mode of usage\n and the one mentioned above is that instead of passing the function/defun\n as an argument, [`tff.tensorflow.computation`](../../tff/tensorflow/computation) along with the optional type specifier\n is written above the function/defun's body.\n\n Here's an example of a computation that accepts a parameter: \n\n @tff.tensorflow.computation(tf.int32)\n def foo(x):\n return x \u003e 10\n\n One can think of this mode of usage as merely a syntactic sugar for the\n example already given earlier: \n\n foo = tff.tensorflow.computation(lambda x: x \u003e 10, tf.int32)\n\n Here's an example of a no-parameter computation: \n\n @tff.tensorflow.computation\n def foo():\n return tf.constant(10)\n\n Again, this is merely syntactic sugar for the example given earlier: \n\n foo = tff.tensorflow.computation(lambda: tf.constant(10))\n\n If the Python function has multiple decorators,\n [`tff.tensorflow.computation`](../../tff/tensorflow/computation) should be the outermost one (the one that\n appears first in the sequence).\n3. Create a polymorphic callable to be instantiated based on arguments,\n similarly to TensorFlow defuns that have been defined without an input\n signature.\n\n This mode of usage is symmetric to those above. One simply omits the type\n specifier, and applies [`tff.tensorflow.computation`](../../tff/tensorflow/computation) as a decorator or\n wrapper to a function/defun that does expect parameters.\n\n Here's an example of wrapping a lambda as a polymorphic callable: \n\n foo = tff.tensorflow.computation(lambda x, y: x \u003e y)\n\n The resulting `foo` can be used in the same ways as if it were had the\n type been declared; the corresponding computation is simply created on\n demand, in the same way as how polymorphic TensorFlow defuns create and\n cache concrete function definitions for each combination of argument\n types. \n\n ...foo(1, 2)...\n ...foo(0.5, 0.3)...\n\n Here's an example of creating a polymorphic callable via decorator: \n\n @tff.tensorflow.computation\n def foo(x, y):\n return x \u003e y\n\n The syntax is symmetric to all examples already shown.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------|-----------------------------------------------------------------------------------------------------------------------------------------------|\n| `*args` | Either a function/defun, or TFF type spec, or both (function first), or neither, as documented in the 3 patterns and examples of usage above. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| If invoked with a function as an argument, returns an instance of a TFF computation constructed based on this function. If called without one, as in the typical decorator style of usage, returns a callable that expects to be called with the function definition supplied as a parameter; see the patterns and examples of usage above. ||\n\n\u003cbr /\u003e"]]