# tff.learning.metrics.SumThenFinalizeFactory

<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) |

Aggregation Factory that sums and then finalizes the metrics.

Inherits From: [`UnweightedAggregationFactory`](https://www.tensorflow.org/federated/api_docs/python/tff/aggregators/UnweightedAggregationFactory)

    tff.learning.metrics.SumThenFinalizeFactory(
        metric_finalizers: https://www.tensorflow.org/federated/api_docs/python/tff/learning/metrics/MetricFinalizersType,
        initial_unfinalized_metrics: Optional[collections.OrderedDict[str, Any]] = None,
        inner_summation_factory: Optional[https://www.tensorflow.org/federated/api_docs/python/tff/aggregators/UnweightedAggregationFactory] = None
    )

The created [`tff.templates.AggregationProcess`](https://www.tensorflow.org/federated/api_docs/python/tff/templates/AggregationProcess) uses the inner summation
process created by the inner summation factory to sum unfinalized metrics
from [`tff.CLIENTS`](https://www.tensorflow.org/federated/api_docs/python/tff#CLIENTS) to [`tff.SERVER`](https://www.tensorflow.org/federated/api_docs/python/tff#SERVER), accumulates the summed unfinalized metrics
in the `state`, and then finalize the metrics for both current round and total
rounds. If the inner summation factory is not specified,
[`tff.aggregators.SumFactory`](https://www.tensorflow.org/federated/api_docs/python/tff/aggregators/SumFactory) is used by default. The inner summation factory
can also use SecAgg.

The accumulated unfinalized metrics across rounds are initialized to be the
intial value of the unfinalized metrics, if the inital value is not specified,
zero is used.

The `next` function of the created [`tff.templates.AggregationProcess`](https://www.tensorflow.org/federated/api_docs/python/tff/templates/AggregationProcess) takes
the `state` and local unfinalized metrics reported from [`tff.CLIENTS`](https://www.tensorflow.org/federated/api_docs/python/tff#CLIENTS), and
returns a [`tff.templates.MeasuredProcessOutput`](https://www.tensorflow.org/federated/api_docs/python/tff/templates/MeasuredProcessOutput) object with the following
properties:

- `state`: a tuple of the `state` of the inner summation process and the accumulated unfinalized metrics across rounds.
- `result`: a tuple of the finalized metrics of the current round and total rounds.
- `measurements`: the measurements of the inner summation process.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `metric_finalizers` | An `collections.OrderedDict` of metric names to finalizers, should have same keys as the unfinalized metrics. A finalizer is a function (typically a [`tf.function`](https://www.tensorflow.org/api_docs/python/tf/function) decorated callable or a [`tff.tensorflow.computation`](https://www.tensorflow.org/federated/api_docs/python/tff/tensorflow/computation) decorated TFF Computation) that takes in a metric's unfinalized values, and returns the finalized metric values. This can be obtained from [`tff.learning.models.VariableModel.metric_finalizers()`](https://www.tensorflow.org/federated/api_docs/python/tff/learning/models/VariableModel#metric_finalizers). |
| `initial_unfinalized_metrics` | Optional. An `collections.OrderedDict` of metric names to the initial values of local unfinalized metrics, its structure should match that of `local_unfinalized_metrics_type`. If not specified, defaults to zero. |
| `inner_summation_factory` | Optional. A [`tff.aggregators.UnweightedAggregationFactory`](https://www.tensorflow.org/federated/api_docs/python/tff/aggregators/UnweightedAggregationFactory) that creates a [`tff.templates.AggregationProcess`](https://www.tensorflow.org/federated/api_docs/python/tff/templates/AggregationProcess) to sum the metrics from clients to server. If not specified, [`tff.aggregators.SumFactory`](https://www.tensorflow.org/federated/api_docs/python/tff/aggregators/SumFactory) is used. If the metrics aggregation needs SecAgg, `aggregation_factory.SecureSumFactory` can be used as the inner summation factory. |

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `TypeError` | If any argument type mismatches. |

<br />

## Methods

### `create`

[View source](https://github.com/tensorflow/federated/blob/v0.87.0
Version 2.0, January 2004
Licensed under the Apache License, Version 2.0 (the)

    create(
        local_unfinalized_metrics_type: https://www.tensorflow.org/federated/api_docs/python/tff/types/StructWithPythonType
    ) -> https://www.tensorflow.org/federated/api_docs/python/tff/templates/AggregationProcess

Creates a [`tff.templates.AggregationProcess`](https://www.tensorflow.org/federated/api_docs/python/tff/templates/AggregationProcess) for metrics aggregation.

<br />

<br />

<br />

| Args ||
|---|---|
| `local_unfinalized_metrics_type` | A [`tff.types.StructWithPythonType`](https://www.tensorflow.org/federated/api_docs/python/tff/types/StructWithPythonType) (with `collections.OrderedDict` as the Python container) of a client's local unfinalized metrics. For example, `local_unfinalized_metrics` could represent the output type of [`tff.learning.models.VariableModel.report_local_unfinalized_metrics()`](https://www.tensorflow.org/federated/api_docs/python/tff/learning/models/VariableModel#report_local_unfinalized_metrics). |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| An instance of [`tff.templates.AggregationProcess`](https://www.tensorflow.org/federated/api_docs/python/tff/templates/AggregationProcess). ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `TypeError` | If any argument type mismatches; if the metric finalizers mismatch the type of local unfinalized metrics; if the initial unfinalized metrics mismatch the type of local unfinalized metrics. |

<br />