tff.templates.IterativeProcess
Stay organized with collections
Save and categorize content based on your preferences.
A process that includes an initialization and iterated computation.
tff.templates.IterativeProcess(
initialize_fn: tff.Computation
,
next_fn: tff.Computation
,
next_is_multi_arg: Optional[bool] = None
)
Used in the notebooks
An iterated process will usually be driven by a control loop like:
def initialize_fn():
...
def next_fn(state):
...
iterative_process = IterativeProcess(initialize_fn, next_fn)
state = iterative_process.initialize()
for round in range(num_rounds):
state = iterative_process.next(state)
The initialize_fn
function must return an object which is expected as input
to and returned by the next_fn
function. By convention, we refer to this
object as state
.
The iteration step (next_fn
function) can accept arguments in addition to
state
(which must be the first argument), and return additional arguments,
with state
being the first output argument:
def next_fn(state, round_num):
...
iterative_process = ...
state = iterative_process.initialize()
for round in range(num_rounds):
state, output = iterative_process.next(state, round)
Args |
initialize_fn
|
A no-arg tff.Computation that returns the initial state
of the iterative process. Let the type of this state be called S .
|
next_fn
|
A tff.Computation that represents the iterated function. The
first or only argument must be a type that is assignable from the state
type S (tff.types.Type.is_assignable_from must return True ). The
first or only return value must also be assignable to the first or only
argument, the same requirement as the S type.
|
next_is_multi_arg
|
An optional boolean indicating that next_fn will
receive more than just the state argument (if True ) or only the state
argument (if False ). This parameter is primarily used to provide
better error messages.
|
Raises |
TypeError
|
If initialize_fn and next_fn are not instances of
tff.Computation .
|
TemplateInitFnParamNotEmptyError
|
If initialize_fn has any input
arguments.
|
TemplateStateNotAssignableError
|
If the state returned by either
initialize_fn or next_fn is not assignable to the first input
argument of next_fn .
|
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.templates.IterativeProcess\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\nA process that includes an initialization and iterated computation. \n\n tff.templates.IterativeProcess(\n initialize_fn: ../../tff/Computation,\n next_fn: ../../tff/Computation,\n next_is_multi_arg: Optional[bool] = None\n )\n\n### Used in the notebooks\n\n| Used in the tutorials |\n|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Building Your Own Federated Learning Algorithm](https://www.tensorflow.org/federated/tutorials/building_your_own_federated_learning_algorithm) - [Working with tff's ClientData.](https://www.tensorflow.org/federated/tutorials/working_with_client_data) |\n\nAn iterated process will usually be driven by a control loop like: \n\n def initialize_fn():\n ...\n\n def next_fn(state):\n ...\n\n iterative_process = IterativeProcess(initialize_fn, next_fn)\n state = iterative_process.initialize()\n for round in range(num_rounds):\n state = iterative_process.next(state)\n\nThe `initialize_fn` function must return an object which is expected as input\nto and returned by the `next_fn` function. By convention, we refer to this\nobject as `state`.\n\nThe iteration step (`next_fn` function) can accept arguments in addition to\n`state` (which must be the first argument), and return additional arguments,\nwith `state` being the first output argument: \n\n def next_fn(state, round_num):\n ...\n\n iterative_process = ...\n state = iterative_process.initialize()\n for round in range(num_rounds):\n state, output = iterative_process.next(state, round)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `initialize_fn` | A no-arg [`tff.Computation`](../../tff/Computation) that returns the initial state of the iterative process. Let the type of this state be called `S`. |\n| `next_fn` | A [`tff.Computation`](../../tff/Computation) that represents the iterated function. The first or only argument must be a type that is assignable from the state type `S` ([`tff.types.Type.is_assignable_from`](../../tff/types/Type#is_assignable_from) must return `True`). The first or only return value must also be assignable to the first or only argument, the same requirement as the `S` type. |\n| `next_is_multi_arg` | An optional boolean indicating that `next_fn` will receive more than just the state argument (if `True`) or only the state argument (if `False`). This parameter is primarily used to provide better error messages. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|------------------------------------|----------------------------------------------------------------------------------------------------------------------------|\n| `TypeError` | If `initialize_fn` and `next_fn` are not instances of [`tff.Computation`](../../tff/Computation). |\n| `TemplateInitFnParamNotEmptyError` | If `initialize_fn` has any input arguments. |\n| `TemplateStateNotAssignableError` | If the `state` returned by either `initialize_fn` or `next_fn` is not assignable to the first input argument of `next_fn`. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Attributes ---------- ||\n|--------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `initialize` | A no-arg [`tff.Computation`](../../tff/Computation) that returns the initial state. |\n| `next` | A [`tff.Computation`](../../tff/Computation) that produces the next state. \u003cbr /\u003e Its first argument should always be the current state (originally produced by [`tff.templates.IterativeProcess.initialize`](../../tff/templates/IterativeProcess#initialize)), and the first (or only) returned value is the updated state. |\n| `state_type` | The [`tff.Type`](../../tff/types/Type) of the state of the process. |\n\n\u003cbr /\u003e"]]