The returned federated TFF computation is a polymorphic computation that
accepts unfinalized client metrics, and returns finalized, summed metrics
placed at the server. Invoking the polymorphic computation will initiate
tracing on the argument and will raise a ValueError if the keys (i.e.,
metric names) in metric_finalizers are not the same as those of the argument
the polymorphic method is invoked on.
The returned computation is intended to be invoked on the output of
tff.learning.models.VariableModel.report_local_unfinalized_metrics() when
placed at CLIENTS. The output is computed by first finalizing each client's
metrics locally, and then collecting metrics from at most sample_size
clients at the SERVER. If more than sample_size clients participating,
then sample_size clients are sampled (by reservoir sampling algorithm);
otherwise, all clients' metrics are collected. Sampling is done in a
"per-client" manner, i.e., a client, once sampled, will contribute all its
metrics to the final result.
The collected metrics samples at SERVER has the same structure (i.e., same
keys in a dictionary) as the client's local metrics, except that each leaf
node contains a list of scalar metric values, where each value comes from a
sampled client, e.g.,
Unused, will be removed from the API in the
future.
sample_size
An integer specifying the number of clients sampled by the
reservoir sampling algorithm. Metrics from the sampled clients are
collected at the server. If the total number of participating clients are
smaller than this value, then all clients' metrics are collected. Default
value is 100.
Returns
A federated TFF computation that finalizes the unfinalized metrics from
CLIENTS, samples the clients, and returns the sampled metrics at SERVER.
[null,null,["Last updated 2024-09-20 UTC."],[],[],null,["# tff.learning.metrics.finalize_then_sample\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [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) |\n\nCreates a TFF computation to aggregate metrics via `finalize_then_sample`. \n\n tff.learning.metrics.finalize_then_sample(\n metric_finalizers: Union[../../../tff/learning/metrics/MetricFinalizersType, ../../../tff/learning/metrics/FunctionalMetricFinalizersType],\n local_unfinalized_metrics_type: Optional[../../../tff/types/StructWithPythonType] = None,\n sample_size: int = 100\n ) -\u003e ../../../tff/Computation\n\nThe returned federated TFF computation is a polymorphic computation that\naccepts unfinalized client metrics, and returns finalized, summed metrics\nplaced at the server. Invoking the polymorphic computation will initiate\ntracing on the argument and will raise a `ValueError` if the keys (i.e.,\nmetric names) in `metric_finalizers` are not the same as those of the argument\nthe polymorphic method is invoked on.\n| **Note:** invoking this computation outside of a federated context (a method decoratedc with [`tff.federated_computation`](../../../tff/federated_computation)) will require first wrapping it in a concrete, non-polymorphic [`tff.Computation`](../../../tff/Computation) with appropriate federated types.\n\nThe returned computation is intended to be invoked on the output of\n[`tff.learning.models.VariableModel.report_local_unfinalized_metrics()`](../../../tff/learning/models/VariableModel#report_local_unfinalized_metrics) when\nplaced at `CLIENTS`. The output is computed by first finalizing each client's\nmetrics locally, and then collecting metrics from at most `sample_size`\nclients at the `SERVER`. If more than `sample_size` clients participating,\nthen `sample_size` clients are sampled (by reservoir sampling algorithm);\notherwise, all clients' metrics are collected. Sampling is done in a\n\"per-client\" manner, i.e., a client, once sampled, will contribute all its\nmetrics to the final result.\n\nThe collected metrics samples at `SERVER` has the same structure (i.e., same\nkeys in a dictionary) as the client's local metrics, except that each leaf\nnode contains a list of scalar metric values, where each value comes from a\nsampled client, e.g., \n\n sampled_metrics_at_server = {\n 'metric_a': [a1, a2, ...],\n 'metric_b': [b1, b2, ...],\n ...\n }\n\nwhere \"a1\" and \"b1\" are from the same client (similary for \"a2\" and \"b2\" etc).\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Example usage ------------- ||\n|---|---|\n| \u003cbr /\u003e training_process = tff.learning.algorithms.build_weighted_fed_avg( model_fn=..., ..., metrics_aggregator=finalize_then_sample, ) state = training_process.initialize() for i in range(num_rounds): output = training_process.next(state, client_data_at_round_i) state = output.state sampled_client_metrics = output.metrics['client_work'] \u003cbr /\u003e ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|----------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `metric_finalizers` | Either the result of [`tff.learning.models.VariableModel.metric_finalizers`](../../../tff/learning/models/VariableModel#metric_finalizers) (an `OrderedDict` of callables) or the [`tff.learning.models.FunctionalModel.finalize_metrics`](../../../tff/learning/models/FunctionalModel#finalize_metrics) method (a callable that takes an `OrderedDict` argument). If the former, the keys must be the same as the `OrderedDict` returned by [`tff.learning.models.VariableModel.report_local_unfinalized_metrics`](../../../tff/learning/models/VariableModel#report_local_unfinalized_metrics). If the later, the callable must compute over the same keyspace of the result returned by [`tff.learning.models.FunctionalModel.update_metrics_state`](../../../tff/learning/models/FunctionalModel#update_metrics_state). |\n| `local_unfinalized_metrics_type` | Unused, will be removed from the API in the future. |\n| `sample_size` | An integer specifying the number of clients sampled by the reservoir sampling algorithm. Metrics from the sampled clients are collected at the server. If the total number of participating clients are smaller than this value, then all clients' metrics are collected. Default value is 100. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A federated TFF computation that finalizes the unfinalized metrics from `CLIENTS`, samples the clients, and returns the sampled metrics at `SERVER`. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|---------------------------------------|\n| `TypeError` | If the inputs are of the wrong types. |\n| `ValueError` | If `sample_size` is not positive. |\n\n\u003cbr /\u003e"]]