# tff.learning.templates.build_apply_optimizer_finalizer

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) 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) |

Builds finalizer that applies a step of an optimizer.

    tff.learning.templates.build_apply_optimizer_finalizer(
        optimizer_fn: https://www.tensorflow.org/federated/api_docs/python/tff/learning/optimizers/Optimizer,
        model_weights_type: https://www.tensorflow.org/federated/api_docs/python/tff/types/StructType,
        should_reject_update: Callable[[Any, Any], tuple[Union[bool, tf.Tensor], Optional[_MeasurementsType]]
            ] = https://www.tensorflow.org/federated/api_docs/python/tff/learning/templates/reject_non_finite_update
    )

### Used in the notebooks

| Used in the tutorials |
|---|
| - [Composing Learning Algorithms](https://www.tensorflow.org/federated/tutorials/composing_learning_algorithms) |

The provided `model_weights_type` must be a non-federated [`tff.Type`](https://www.tensorflow.org/federated/api_docs/python/tff/types/Type) with the
[`tff.learning.models.ModelWeights`](https://www.tensorflow.org/federated/api_docs/python/tff/learning/models/ModelWeights) container.

The 2nd input argument of the created [`FinalizerProcess.next`](https://www.tensorflow.org/federated/api_docs/python/tff/templates/IterativeProcess#next) expects a value
matching `model_weights_type` and its 3rd argument expects value matching
`model_weights_type.trainable`. The `optimizer` will be applied to the
trainable model weights only, leaving non_trainable weights unmodified.

The state of the process is the state of the `optimizer` and the process
returns empty measurements.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `optimizer_fn` | A [`tff.learning.optimizers.Optimizer`](https://www.tensorflow.org/federated/api_docs/python/tff/learning/optimizers/Optimizer). This optimizer is used to apply client updates to the server model. |
| `model_weights_type` | A non-federated [`tff.Type`](https://www.tensorflow.org/federated/api_docs/python/tff/types/Type) of the model weights to be optimized, which must have a [`tff.learning.models.ModelWeights`](https://www.tensorflow.org/federated/api_docs/python/tff/learning/models/ModelWeights) container. |
| `should_reject_update` | A callable that takes the optimizer state and the model weights update, and returns a boolean or a bool tensor indicating if the model weights update should be rejected and an OrderedDict of measurements. If the model weights update is reject, we will fall back to the previous round's optimizer state and model weight, this is a no-op otherwise. The default function is `reject_non_finite_update` which checks if there is any non-finite value in the model update and returns the results. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| A `FinalizerProcess` that applies the `optimizer`. ||

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `TypeError` | If `value_type` does not have a `tff.learning.model.sModelWeights` Python container, or contains a [`tff.types.FederatedType`](https://www.tensorflow.org/federated/api_docs/python/tff/types/FederatedType). |

<br />