# tff.aggregators.UnweightedReservoirSamplingFactory

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

An `UnweightedAggregationFactory` for reservoir sampling values.

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

    tff.aggregators.UnweightedReservoirSamplingFactory(
        sample_size: int, return_sampling_metadata: bool = False
    )

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

The process has empty `state`. The `measurements` of this factory counts the
number of non-finite (`NaN` or `Inf` values) leaves in the client values
*before* sampling. Specifically, the returned `measurements` has the same
structure as the client value, and every leaf node is a [`tf.int64`](https://www.tensorflow.org/api_docs/python/tf#int64) scalar
tensor counting the number of clients having non-finite value in that leaf.

For example, suppose we are aggregating from three clients:

      client_value_1 = collections.OrderedDict(a=[1.0, 2.0], b=[1.0, np.nan])
      client_value_2 = collections.OrderedDict(a=[np.nan, np.inf], b=[1.0, 2.0])
      client_value_3 = collections.OrderedDict(a=[1.0, 2.0], b=[np.inf, np.nan])

Then `measurements` will be:

      collections.OrderedDict(a=tf.constant(1, dtype=int64),
                              b=tf.constant(2, dtype=int64)

For more about reservoir sampling see
<https://en.wikipedia.org/wiki/Reservoir_sampling>

<br />

<br />

<br />

| ## Args ||
|---|---|
| `sample_size` | An integer specifying the number of clients sampled (by reservoir sampling algorithm). Values from the sampled clients are collected at the server (see the class documentation for details). |
| `return_sampling_metadata` | If True, the `result` property in the returned [`tff.templates.MeasuredProcessOutput`](https://www.tensorflow.org/federated/api_docs/python/tff/templates/MeasuredProcessOutput) object contains a dictionary of sampled values and other sampling metadata (such as random values generated during reservoir sampling). Otherwise, it only contains the sampled values. |

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `TypeError` | If any argument type mismatches. |
| `ValueError` | If `sample_size` is not positive. |

<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: https://www.tensorflow.org/federated/api_docs/python/tff/types/Type
    ) -> https://www.tensorflow.org/federated/api_docs/python/tff/templates/AggregationProcess

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

The provided `value_type` is a non-federated [`tff.Type`](https://www.tensorflow.org/federated/api_docs/python/tff/types/Type), that is, not 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 values matching `value_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>`, 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). |

<br />

<br />

<br />

<br />

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

<br />