tff.learning.metrics.create_functional_metric_fns
Stay organized with collections
Save and categorize content based on your preferences.
Turn a Keras metric construction method into a tuple of pure functions.
tff.learning.metrics.create_functional_metric_fns(
metrics_constructor: Union[MetricConstructor, MetricsConstructor, MetricConstructors]
) -> tuple[Callable[[], StateVar], Callable[[StateVar, Any, Any, Any], StateVar],
Callable[[StateVar], Any]]
This can be used to convert Keras metrics for use in
tff.learning.models.FunctionalModel
. The method traces the metric logic into
three tf.function
with explicit state
parameters that replace the
closure over internal tf.Variable
of the tf.keras.metrics.Metric
.
Example |
>>> metric = tf.keras.metrics.Accuracy()
>>> metric.update_state([1.0, 1.0], [0.0, 1.0])
>>> metric.result() # == 0.5
>>>
>>> metric_fns = tff.learning.metrics.create_functional_metric_fns(
>>> tf.keras.metrics.Accuracy)
>>> initialize, update, finalize = metric_fns
>>> state = initialize()
>>> batch_output = tff.learning.models.BatchOutput(predictions=[0.0, 1.0])
>>> state = update(state, [1.0, 1.0], batch_output)
>>> finalize(state) # == 0.5
|
Returns |
A 3-tuple of tf.function s namely (initialize, update, finalize) .
initialize is a no-arg function used to create the algrebraic "zero"
before reducing the metric over batches of examples. update is a function
that takes three arguments, the state, labels, and the
tff.learning.models.BatchOutput structure from the model's forward pass,
and is used to add an observation to the metric. finalize only takes a
state argument and returns the final metric value based on observations
previously added.
|
Raises |
TypeError
|
If metrics_constructor is not a callable or OrderedDict , or
if metrics_constructor is a callable returning values of the wrong type.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-09-20 UTC.
[null,null,["Last updated 2024-09-20 UTC."],[],[],null,["# tff.learning.metrics.create_functional_metric_fns\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\nTurn a Keras metric construction method into a tuple of pure functions. \n\n tff.learning.metrics.create_functional_metric_fns(\n metrics_constructor: Union[MetricConstructor, MetricsConstructor, MetricConstructors]\n ) -\u003e tuple[Callable[[], StateVar], Callable[[StateVar, Any, Any, Any], StateVar],\n Callable[[StateVar], Any]]\n\nThis can be used to convert Keras metrics for use in\n[`tff.learning.models.FunctionalModel`](../../../tff/learning/models/FunctionalModel). The method traces the metric logic into\nthree [`tf.function`](https://www.tensorflow.org/api_docs/python/tf/function) with explicit `state` parameters that replace the\nclosure over internal [`tf.Variable`](https://www.tensorflow.org/api_docs/python/tf/Variable) of the [`tf.keras.metrics.Metric`](https://www.tensorflow.org/api_docs/python/tf/keras/Metric).\n| **Important:** Only metrics whose [`tf.keras.metrics.Metric.update_state`](https://www.tensorflow.org/api_docs/python/tf/keras/Metric#update_state) method take two arguments (`y_true` and `y_pred`) are supported.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Example ------- ||\n|---|---|\n| \u003cbr /\u003e \u003e\u003e\u003e metric = tf.keras.metrics.Accuracy() \u003e\u003e\u003e metric.update_state([1.0, 1.0], [0.0, 1.0]) \u003e\u003e\u003e metric.result() # == 0.5 \u003e\u003e\u003e \u003e\u003e\u003e metric_fns = tff.learning.metrics.create_functional_metric_fns( \u003e\u003e\u003e tf.keras.metrics.Accuracy) \u003e\u003e\u003e initialize, update, finalize = metric_fns \u003e\u003e\u003e state = initialize() \u003e\u003e\u003e batch_output = tff.learning.models.BatchOutput(predictions=[0.0, 1.0]) \u003e\u003e\u003e state = update(state, [1.0, 1.0], batch_output) \u003e\u003e\u003e finalize(state) # == 0.5 \u003cbr /\u003e ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-----------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `metrics_constructor` | Either a no-arg callable that returns a [`tf.keras.metrics.Metric`](https://www.tensorflow.org/api_docs/python/tf/keras/Metric) or an `OrderedDict` of `str` names to [`tf.keras.metrics.Metric`](https://www.tensorflow.org/api_docs/python/tf/keras/Metric), or `OrderedDict` of no-arg callables returning [`tf.keras.metrics.Metric`](https://www.tensorflow.org/api_docs/python/tf/keras/Metric) instances. The no-arg callables can be the metric class itself (e.g. [`tf.keras.metrics.Accuracy`](https://www.tensorflow.org/api_docs/python/tf/keras/metrics/Accuracy)) in which case the default metric configuration will be used. It also supports lambdas or `functools.partial` to provide alternate metric configurations. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A 3-tuple of [`tf.function`](https://www.tensorflow.org/api_docs/python/tf/function)s namely `(initialize, update, finalize)`. `initialize` is a no-arg function used to create the algrebraic \"zero\" before reducing the metric over batches of examples. `update` is a function that takes three arguments, the state, labels, and the [`tff.learning.models.BatchOutput`](../../../tff/learning/models/BatchOutput) structure from the model's forward pass, and is used to add an observation to the metric. `finalize` only takes a `state` argument and returns the final metric value based on observations previously added. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|-------------|--------------------------------------------------------------------------------------------------------------------------------------------|\n| `TypeError` | If `metrics_constructor` is not a callable or `OrderedDict`, or if `metrics_constructor` is a callable returning values of the wrong type. |\n\n\u003cbr /\u003e"]]