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.
[{
"type": "thumb-down",
"id": "missingTheInformationINeed",
"label":"Missing the information I need"
},{
"type": "thumb-down",
"id": "tooComplicatedTooManySteps",
"label":"Too complicated / too many steps"
},{
"type": "thumb-down",
"id": "outOfDate",
"label":"Out of date"
},{
"type": "thumb-down",
"id": "samplesCodeIssue",
"label":"Samples / code issue"
},{
"type": "thumb-down",
"id": "otherDown",
"label":"Other"
}]
[{
"type": "thumb-up",
"id": "easyToUnderstand",
"label":"Easy to understand"
},{
"type": "thumb-up",
"id": "solvedMyProblem",
"label":"Solved my problem"
},{
"type": "thumb-up",
"id": "otherUp",
"label":"Other"
}]
{"lastModified": "Last updated 2024-09-20 UTC."}
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-09-20 UTC."],[],[]]