How to use Tensorflow addons' metrics correctly in functional API?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
TensorFlow Addons metrics work with the Functional API the same way built-in Keras metrics do: you create metric objects and pass them to model.compile. The most common mistakes are treating the metric like a layer, using the wrong output shape, or forgetting to configure arguments such as num_classes, average, or threshold.
Compile With Metric Instances, Not Tensor Calls
In the Functional API, metrics are not connected into the model graph manually. You build the model graph with layers, then attach metrics during compile.
A correct example with F1Score looks like this:
The important detail is that tfa.metrics.F1Score(...) is passed as a metric object. You do not call it on outputs yourself during model construction.
Match The Metric To The Output Format
Many Addons metrics are stricter than built-in accuracy metrics because they depend on class layout. That means your labels and output tensor must match the metric's expectations.
For example:
- softmax with one-hot labels pairs well with
categorical_crossentropy - sigmoid with multi-label targets often needs a thresholded metric
- sparse integer labels do not fit every Addons metric directly
Here is a full categorical example:
If the labels were sparse integers instead of one-hot vectors, you would need to either convert them or choose a metric compatible with that representation.
Configure Thresholds For Binary And Multi-Label Problems
Metrics such as F1 need a decision rule. For sigmoid outputs, that usually means a threshold.
Without a threshold, the metric may not behave the way you expect because sigmoid outputs are probabilities, not hard class assignments.
Multi-Output Models Need Metric Mapping
If your Functional API model has more than one output, metrics should usually be passed as a dictionary keyed by output layer name.
This avoids accidentally applying a classification metric to a regression head.
Remember That Metrics Are Stateful
Keras metrics accumulate state across batches during an epoch. TensorFlow handles resetting them at the right time during fit, but this matters when you evaluate them manually.
If you use a metric outside model.compile, call update_state, inspect result, and reset with reset_state when needed.
That is another reason metric objects belong in the compile step unless you have a specific custom training loop.
Common Pitfalls
The biggest mistake is calling the metric on tensors inside the model graph as if it were a layer. Metrics belong in compile, not in the network architecture.
Another mistake is mismatching label format and output format. Many Addons metrics expect one-hot or multi-label tensors with a known class dimension.
People also forget parameters such as num_classes and threshold, then wonder why the metric shape or score looks wrong.
Finally, in multi-output models, passing one flat metric list can attach metrics to the wrong output. Use a metrics dictionary when the heads represent different tasks.
Summary
- Create TensorFlow Addons metric instances and pass them to
model.compile. - Match the metric to the model output shape and label encoding.
- Configure
num_classes,average, andthresholdexplicitly when needed. - Use per-output metric dictionaries for multi-head Functional API models.
- Do not treat metrics like layers inside the model graph.
Related reading
- How to use tensorflow feature_columns as input to a keras model
- How to use TensorFlow metrics in Keras
- How to use tensorflow to implement deconvolution?
- how to use tf operations in keras models
- How to use Tensorflow dataset API with training and validation sets
- How to use tensorflow debugging tool tfdbg on tf.estimator in Tensorflow?
- How to use TensorFlow in OOP style?
- How to use tensorflow on spyder?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.