# tff.learning.algorithms.build_fed_recon

<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 the IterativeProcess for optimization using FedRecon.

    tff.learning.algorithms.build_fed_recon(
        model_fn: Callable[[], https://www.tensorflow.org/federated/api_docs/python/tff/learning/models/ReconstructionModel],
        *,
        loss_fn: LossFn,
        metrics_fn: Optional[MetricsFn] = None,
        server_optimizer_fn: https://www.tensorflow.org/federated/api_docs/python/tff/learning/optimizers/Optimizer = sgdm.build_sgdm(learning_rate=1.0),
        client_optimizer_fn: https://www.tensorflow.org/federated/api_docs/python/tff/learning/optimizers/Optimizer = sgdm.build_sgdm(learning_rate=0.1),
        reconstruction_optimizer_fn: https://www.tensorflow.org/federated/api_docs/python/tff/learning/optimizers/Optimizer = sgdm.build_sgdm(learning_rate=0.1),
        dataset_split_fn: Optional[https://www.tensorflow.org/federated/api_docs/python/tff/learning/models/ReconstructionDatasetSplitFn] = None,
        client_weighting: Optional[client_weight_lib.ClientWeightType] = None,
        model_distributor: Optional[https://www.tensorflow.org/federated/api_docs/python/tff/learning/templates/DistributionProcess] = None,
        model_aggregator_factory: Optional[AggregationFactory] = None,
        metrics_aggregator: Optional[Callable[[MetricFinalizersType, computation_types.
            StructWithPythonType], computation_base.Computation]] = https://www.tensorflow.org/federated/api_docs/python/tff/learning/metrics/sum_then_finalize
    ) -> https://www.tensorflow.org/federated/api_docs/python/tff/learning/templates/LearningProcess

### Used in the notebooks

| Used in the tutorials |
|---|
| - [Federated Reconstruction for Matrix Factorization](https://www.tensorflow.org/federated/tutorials/federated_reconstruction_for_matrix_factorization) |

Returns a [`tff.templates.IterativeProcess`](https://www.tensorflow.org/federated/api_docs/python/tff/templates/IterativeProcess) for Federated Reconstruction. On
the client, computation can be divided into two stages: (1) reconstruction of
local variables and (2) training of global variables.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `model_fn` | A no-arg function that returns a `tff.learning.reconstruction.Model`. This method must *not* capture Tensorflow tensors or variables and use them. must be constructed entirely from scratch on each invocation, returning the same pre-constructed model each call will result in an error. |
| `loss_fn` | A no-arg function returning a [`tf.keras.losses.Loss`](https://www.tensorflow.org/api_docs/python/tf/keras/Loss) to use to compute local model updates during reconstruction and post-reconstruction and evaluate the model during training. The final loss metric is the example-weighted mean loss across batches and across clients. The loss metric does not include reconstruction batches in the loss. |
| `metrics_fn` | A no-arg function returning a list of [`tf.keras.metrics.Metric`](https://www.tensorflow.org/api_docs/python/tf/keras/Metric)s to evaluate the model. Metrics results are computed locally as described by the metric, and are aggregated across clients as in `federated_aggregate_keras_metric`. If None, no metrics are applied. Metrics are not computed on reconstruction batches. |
| `server_optimizer_fn` | A [`tff.learning.optimizers.Optimizer`](https://www.tensorflow.org/federated/api_docs/python/tff/learning/optimizers/Optimizer)for applying updates to the global model on the server. |
| `client_optimizer_fn` | A [`tff.learning.optimizers.Optimizer`](https://www.tensorflow.org/federated/api_docs/python/tff/learning/optimizers/Optimizer) for local client training after reconstruction. |
| `reconstruction_optimizer_fn` | A [`tff.learning.optimizers.Optimizer`](https://www.tensorflow.org/federated/api_docs/python/tff/learning/optimizers/Optimizer) used to reconstruct the local variables, with the global ones frozen, or the first stage described above. |
| `dataset_split_fn` | A [`tff.learning.models.ReconstructionDatasetSplitFn`](https://www.tensorflow.org/federated/api_docs/python/tff/learning/models/ReconstructionDatasetSplitFn) taking in a single TF dataset and producing two TF datasets. The first is iterated over during reconstruction, and the second is iterated over post-reconstruction. This can be used to preprocess datasets to e.g. iterate over them for multiple epochs or use disjoint data for reconstruction and post-reconstruction. If None, split client data in half for each user, using one half for reconstruction and the other for evaluation. See [`tff.learning.models.ReconstructionModel.build_dataset_split_fn`](https://www.tensorflow.org/federated/api_docs/python/tff/learning/models/ReconstructionModel#build_dataset_split_fn) for options. |
| `client_weighting` | A value of [`tff.learning.ClientWeighting`](https://www.tensorflow.org/federated/api_docs/python/tff/learning/ClientWeighting) that specifies a built-in weighting method, or a callable that takes the local metrics of the model and returns a tensor that provides the weight in the federated average of model deltas. If None, defaults to weighting by number of examples. |
| `model_distributor` | An optional `DistributionProcess` that distributes the model weights on the server to the clients. If set to `None`, the distributor is constructed via [`tff.learning.templates.build_broadcast_process`](https://www.tensorflow.org/federated/api_docs/python/tff/learning/templates/build_broadcast_process). |
| `model_aggregator_factory` | An optional instance of [`tff.aggregators.WeightedAggregationFactory`](https://www.tensorflow.org/federated/api_docs/python/tff/aggregators/WeightedAggregationFactory) or [`tff.aggregators.UnweightedAggregationFactory`](https://www.tensorflow.org/federated/api_docs/python/tff/aggregators/UnweightedAggregationFactory) determining the method of aggregation to perform. If unspecified, uses a default [`tff.aggregators.MeanFactory`](https://www.tensorflow.org/federated/api_docs/python/tff/aggregators/MeanFactory) which computes a stateless mean across clients (weighted depending on `client_weighting`). |
| `metrics_aggregator` | A function that takes in the metric finalizers (i.e., `tff.learning.Model.metric_finalizers()`) and a [`tff.types.StructWithPythonType`](https://www.tensorflow.org/federated/api_docs/python/tff/types/StructWithPythonType) of the unfinalized metrics (i.e., the TFF type of `tff.learning.Model.report_local_unfinalized_metrics()`), and returns a [`tff.Computation`](https://www.tensorflow.org/federated/api_docs/python/tff/Computation) for aggregating the unfinalized metrics. If `None`, this is set to [`tff.learning.metrics.sum_then_finalize`](https://www.tensorflow.org/federated/api_docs/python/tff/learning/metrics/sum_then_finalize). |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| A [`tff.learning.templates.LearningProcess`](https://www.tensorflow.org/federated/api_docs/python/tff/learning/templates/LearningProcess). ||

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `TypeError` | If `model_fn` does not return instances of [`tff.learning.models.ReconstructionModel`](https://www.tensorflow.org/federated/api_docs/python/tff/learning/models/ReconstructionModel). |
| `ValueError` | If `model_aggregator_factory` is a [`tff.aggregators.UnweightedAggregationFactory`](https://www.tensorflow.org/federated/api_docs/python/tff/aggregators/UnweightedAggregationFactory) and `client_weighting` is any value other than [`tff.learning.ClientWeighting.UNIFORM`](https://www.tensorflow.org/federated/api_docs/python/tff/learning/ClientWeighting#UNIFORM). |

<br />