<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 for weighted mean.

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

    tff.aggregators.MeanFactory(
        value_sum_factory: Optional[https://www.tensorflow.org/federated/api_docs/python/tff/aggregators/UnweightedAggregationFactory] = None,
        weight_sum_factory: Optional[https://www.tensorflow.org/federated/api_docs/python/tff/aggregators/UnweightedAggregationFactory] = None,
        no_nan_division: bool = False
    )

### Used in the notebooks

| Used in the tutorials |
|---|
| - [Tuning recommended aggregations for learning](https://www.tensorflow.org/federated/tutorials/tuning_recommended_aggregators) - [Composing Learning Algorithms](https://www.tensorflow.org/federated/tutorials/composing_learning_algorithms) |

The created [`tff.templates.AggregationProcess`](https://www.tensorflow.org/federated/api_docs/python/tff/templates/AggregationProcess) computes the weighted mean of
values placed at `CLIENTS`, and outputs the mean placed at `SERVER`.

The input arguments of the `next` attribute of the process returned by
`create` are `<state, value, weight>`, where `weight` is a scalar broadcasted
to the structure of `value`, and the weighted mean refers to the expression
`sum(value * weight) / sum(weight)`.

The implementation is parameterized by two inner aggregation factories
responsible for the summations above, with the following high-level steps.

- Multiplication of `value` and `weight` at `CLIENTS`.
- Delegation to inner `value_sum_factory` and `weight_sum_factory` to realize the sum of weighted values and weights.
- Division of summed weighted values and summed weights at `SERVER`.

Note that the the division at `SERVER` can protect against division by 0, as
specified by `no_nan_division` constructor argument.

The `state` is the composed `state` of the aggregation processes created by
the two inner aggregation factories. The same holds for `measurements`.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `value_sum_factory` | An optional [`tff.aggregators.UnweightedAggregationFactory`](https://www.tensorflow.org/federated/api_docs/python/tff/aggregators/UnweightedAggregationFactory) responsible for summation of weighted values. If not specified, [`tff.aggregators.SumFactory`](https://www.tensorflow.org/federated/api_docs/python/tff/aggregators/SumFactory) is used. |
| `weight_sum_factory` | An optional [`tff.aggregators.UnweightedAggregationFactory`](https://www.tensorflow.org/federated/api_docs/python/tff/aggregators/UnweightedAggregationFactory) responsible for summation of weights. If not specified, [`tff.aggregators.SumFactory`](https://www.tensorflow.org/federated/api_docs/python/tff/aggregators/SumFactory) is used. |
| `no_nan_division` | A bool. If True, the computed mean is 0 if sum of weights is equal to 0. |

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `TypeError` | If provided `value_sum_factory` or `weight_sum_factory` is not an instance of [`tff.aggregators.UnweightedAggregationFactory`](https://www.tensorflow.org/federated/api_docs/python/tff/aggregators/UnweightedAggregationFactory). |

<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(
        value_type: factory.ValueType, weight_type: factory.ValueType
    ) -> https://www.tensorflow.org/federated/api_docs/python/tff/templates/AggregationProcess

Creates a `tff.aggregators.AggregationProcess` with weights.

The provided `value_type` and `weight_type` are non-federated [`tff.Type`](https://www.tensorflow.org/federated/api_docs/python/tff/types/Type)s.
That is, neither is a [`tff.FederatedType`](https://www.tensorflow.org/federated/api_docs/python/tff/types/FederatedType).

The returned `tff.aggregators.AggregationProcess` will be created
for aggregation of pairs of values matching `value_type` and `weight_type`
placed at [`tff.CLIENTS`](https://www.tensorflow.org/federated/api_docs/python/tff#CLIENTS). That is, its `next` method will expect type
`<S@SERVER, {value_type}@CLIENTS, {weight_type}@CLIENTS>`, where `S` is the
unplaced return type of its `initialize` method.

<br />

<br />

<br />

| Args ||
|---|---|
| `value_type` | A non-federated [`tff.Type`](https://www.tensorflow.org/federated/api_docs/python/tff/types/Type). |
| `weight_type` | A non-federated [`tff.Type`](https://www.tensorflow.org/federated/api_docs/python/tff/types/Type). |

<br />

<br />

<br />

<br />

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

<br />