Skip to content

Align metrics.get() metric name with identifier alias (tf.keras parity)#22121

Open
MalyalaKarthik66 wants to merge 2 commits intokeras-team:masterfrom
MalyalaKarthik66:fix-metrics-get-name-alias
Open

Align metrics.get() metric name with identifier alias (tf.keras parity)#22121
MalyalaKarthik66 wants to merge 2 commits intokeras-team:masterfrom
MalyalaKarthik66:fix-metrics-get-name-alias

Conversation

@MalyalaKarthik66
Copy link
Contributor

@MalyalaKarthik66 MalyalaKarthik66 commented Feb 6, 2026

  • Ensures metrics created via metrics.get("alias") use the alias as the default metric name when the alias resolves to a Metric class instance.
  • This is a Keras 3–specific behavior: since aliases like "mse" resolve to Metric objects, using the alias as the name (when no explicit name is provided) improves clarity and avoids default class-name-based naming.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @MalyalaKarthik66, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request aims to enhance the consistency and predictability of metric naming within Keras. By modifying the metrics.get() function, it ensures that when a metric is retrieved using a string alias (e.g., "bce"), the resulting metric object is initialized with that alias as its name. This aligns Keras's behavior with tf.keras, providing a more intuitive and standardized experience for developers.

Highlights

  • Metric Naming Consistency: When using metrics.get("alias"), the metric will now be instantiated with the provided alias as its name, rather than its default class name.
  • tf.keras Parity: This change brings the behavior of metric instantiation closer to that of tf.keras, improving consistency across Keras implementations.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • keras/src/metrics/init.py
    • Removed a TODO comment that highlighted the previous discrepancy in metric naming with tf.keras.
    • Updated the get function to conditionally pass the identifier string as the name argument to the metric class constructor. This is done when the identifier is a string and the instantiated class is a callable, ensuring the alias is used as the metric's name if the constructor supports it, otherwise falling back to default instantiation.
Activity
  • No human activity has been recorded on this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request aligns metrics.get() behavior with tf.keras by using the string identifier as the metric's name, which is a good improvement for consistency. The implementation uses a try-except block to handle metric instantiation, which I've provided a suggestion to make more robust against masking unrelated TypeError exceptions.

Comment on lines 207 to 210
try:
obj = obj(name=identifier)
except TypeError:
obj = obj()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The use of a broad except TypeError can mask legitimate TypeError exceptions raised from within the metric's __init__ method. For instance, if a metric's constructor that accepts a name argument raises a TypeError for an internal reason, this exception will be silently caught, and the code will fall back to instantiating the metric without arguments. This could lead to unexpected behavior and hide bugs.

A more robust approach is to inspect the constructor's signature to determine if it can be called with just the name argument, avoiding the try-except block. This ensures that only argument-related instantiation issues are handled gracefully, while other TypeErrors are allowed to propagate.

Suggested change
try:
obj = obj(name=identifier)
except TypeError:
obj = obj()
sig = inspect.signature(obj.__init__)
params = sig.parameters
has_other_req_args = any(
p.name not in ("self", "name")
and p.default is inspect.Parameter.empty
and p.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD
for p in params.values()
)
if "name" in params and not has_other_req_args:
obj = obj(name=identifier)
else:
obj = obj()

@codecov-commenter
Copy link

codecov-commenter commented Feb 6, 2026

Codecov Report

❌ Patch coverage is 50.00000% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.79%. Comparing base (d72e81d) to head (8e0dd7d).

Files with missing lines Patch % Lines
keras/src/metrics/__init__.py 50.00% 2 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #22121      +/-   ##
==========================================
- Coverage   82.80%   82.79%   -0.01%     
==========================================
  Files         592      592              
  Lines       63146    63153       +7     
  Branches     9920     9922       +2     
==========================================
+ Hits        52287    52290       +3     
- Misses       8311     8313       +2     
- Partials     2548     2550       +2     
Flag Coverage Δ
keras 82.61% <50.00%> (-0.01%) ⬇️
keras-jax 62.45% <50.00%> (-0.01%) ⬇️
keras-numpy 56.60% <50.00%> (-0.01%) ⬇️
keras-openvino 37.47% <0.00%> (-0.01%) ⬇️
keras-tensorflow 63.68% <50.00%> (-0.01%) ⬇️
keras-torch 62.49% <50.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Collaborator

@hertschuh hertschuh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for addressing this.

Can you provide an example of when tf_keras and keras 3 differ? In fact it should be part of a unit test.

@MalyalaKarthik66
Copy link
Contributor Author

Thanks for addressing this.

Can you provide an example of when tf_keras and keras 3 differ? In fact it should be part of a unit test.

Here is a concrete example:

Before this change:
keras.metrics.get("mse").name == "mean_squared_error"

After this change (matching tf.keras behavior):
keras.metrics.get("mse").name == "mse"

Example unit test:

def test_get_mse_name(self):
    m = metrics_module.get("mse")
    assert m.name == "mse", f"Expected name='mse', got name='{m.name}'"

@hertschuh
Copy link
Collaborator

keras.metrics.get("mse").name

So, I tested with Keras 2:

import os
os.environ["TF_USE_LEGACY_KERAS"] = "True"
import tensorflow as tf
from tensorflow import keras

print(keras.metrics.get("mse").name)
AttributeError: 'function' object has no attribute 'name'

In this example, it's not a name difference. Keras 2 resolves "mse" to the mean_square_error function, whereas Keras 3 resolves it to a MeanSquareError class instance.

@MalyalaKarthik66
Copy link
Contributor Author

@hertschuh You’re absolutely right — thanks for checking this in Keras 2.

This is not strict tf.keras parity. In Keras 2, "mse" resolves to a function, so there is no .name attribute to compare.

This PR targets Keras 3 specifically. In Keras 3, aliases like "mse" resolve to Metric class instances, so using the alias as the default metric name (when no explicit name is provided) is clearer and matches what users expect.

I’ve updated the PR description to remove the tf.keras parity claim.

@hertschuh
Copy link
Collaborator

This PR targets Keras 3 specifically. In Keras 3, aliases like "mse" resolve to Metric class instances, so using the alias as the default metric name (when no explicit name is provided) is clearer and matches what users expect.

But this already works without your change:

import keras

model = keras.Sequential([keras.layers.Dense(1)])
model.compile(optimizer="adam", loss="mse", metrics=["mae"])
history = model.fit(np.random.rand(10, 10), np.random.rand(10, 1), epochs=1)
print(history.history)
{'loss': [1.173693299293518], 'mae': [0.9709270596504211]}

The metric in the output is name "mae" not "mean_absolute_error".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants