Open TomsCodingCode opened 4 months ago
Hi @TomsCodingCode, thanks for using TFX and report the issue with a concrete standalone example!
The Evaluator standard component redirects its implementation into TFMA packages. Although, I am not an expert on TFMA, at my glance, the issue arises because the tf.keras.metrics.R2Score metric stores multiple internal variables (like sum of squares, sample count, etc.) However, TFMA usually expects metrics to have a simple, single-value state for serialization and aggregation. This mismatch causes the error you encountered.
To resolve this, you can use a custom metric wrapper called R2ScoreWrapper. This wrapper encapsulates the complex internal state of R2Score and exposes only the final value to TFMA, making it compatible with TFMA's serialization and aggregation mechanisms.
class R2ScoreWrapper(tf.keras.metrics.Metric):
def __init__(self, name="r2_score_wrapper", **kwargs):
super().__init__(name=name, **kwargs)
self.r2_score = tf.keras.metrics.R2Score()
def update_state(self, y_true, y_pred, sample_weight=None):
self.r2_score.update_state(y_true, y_pred, sample_weight)
def result(self):
return self.r2_score.result()
def reset_state(self):
self.r2_score.reset_state()
...
linear_model.compile(
optimizer=keras.optimizers.Adam(learning_rate=0.1),
loss='mean_absolute_error',
metrics=[R2ScoreWrapper()]
# metrics=[tf.keras.metrics.R2Score()]
)
I found the similar phenomenon appears to the metrics.F1Score
too. Hope that it works as you wish. Thanks!
That works perfectly fine as a workaround, thanks!
Is this a bug in tfma then or is this expected baviour?
If the bug is related to a specific library below, please raise an issue in the respective repo directly:
TensorFlow Data Validation Repo
TensorFlow Model Analysis Repo
TensorFlow Transform Repo
TensorFlow Serving Repo
System information
pip freeze
output):Describe the current behavior When I specify R2 Score as a Metric when compiling the model, the downstream component Evaluator throws an exception. This is not the case for any other Metrics I have tried
Describe the expected behavior The Evaluator evaluates the model with all stadard metrics
Standalone code to reproduce the issue
Providing a bare minimum test case or step(s) to reproduce the problem will greatly help us to debug the issue. If possible, please share a link to Colab/Jupyter/any notebook.
Name of your Organization (Optional)
Other info / logs I went down a debugging rabbithole myself and I think the issue is that the metrics container used by the model does not build the metrics it contains after being loaded from memory. For most metrics this is fine, but the R2 Score adds some weights during the build function and those are missing, as can be seen in the error message.
You called
set_weights(weights)on layer "r2_score" with a weight list of length 5, but the layer was expecting 1 weights.
Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached.