TensorFlow中tf.metrics.mean_relative_error遇0除法的异常行为排查
tf.metrics.mean_relative_error Returns Finite Value While Manual MRE Shows inf With Zero Labels Hey, let's break down why you're seeing this discrepancy—it's not a TensorFlow bug, but an intentional robustness feature in the official metric.
The Core Difference: How Zero Values Are Handled
Your Manual Calculation
Your manual MRE code does a direct division by Y_test, which hits a division-by-zero when labels are 0:
MANUAL_MRE = tf.reduce_mean(tf.abs((Y_test - tf.transpose(predictions)) / Y_test))
Naturally, this produces inf because any number divided by 0 is undefined (represented as inf in TensorFlow).
TensorFlow's mean_relative_error Logic
The official tf.metrics.mean_relative_error is designed to handle edge cases like zero labels gracefully. Under the hood, it avoids division-by-zero by replacing the relative error calculation for zero-label samples with an absolute error instead.
To put it in simplified code terms (aligned with TensorFlow's actual implementation):
# Simplified logic for clarity relative_errors = tf.where( tf.equal(expectation, 0), tf.abs(prediction - expectation), # Use absolute error when label is 0 tf.abs((prediction - expectation) / expectation) # Normal relative error otherwise ) MRE = tf.reduce_mean(relative_errors)
This is why you get a finite value instead of inf—the metric skips the invalid division for zero labels and uses a fallback calculation.
How to Verify This
You can confirm this behavior with quick tests:
- Filter out all samples where
Y_test == 0and recalculate your manual MRE—it should match the official metric's result for non-zero data (which you already confirmed works). - For the zero-label samples, compute their absolute error instead of relative error, then average all errors (non-zero relative + zero absolute)—you'll get the exact same value as
tf.metrics.mean_relative_errorreturns.
Wrap-Up
This is a deliberate feature, not a bug. TensorFlow's metric is built to be robust for real-world datasets where zero labels might appear, so you don't end up with an unusable inf result. If your use case requires strict relative error calculation (even if that means inf with zeros), stick with your manual implementation. Otherwise, the official metric's handling is more practical for most evaluation scenarios.
内容的提问来源于stack exchange,提问作者Sim

