Skip to content

Conversation

@Shi-pra-19
Copy link

Refactor loss function and tensor handling in metric learning for compatibility with Keras 3.

Refactor loss function and tensor handling in metric learning for compatibility with Keras 3.
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Shi-pra-19, 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 updates the metric learning example to be fully compatible with Keras 3. The changes involve adapting import statements, configuring the MultiSimilarityLoss with an explicit reduction strategy, enabling eager execution during model compilation, and modernizing tensor operations to align with the Keras 3 API for improved consistency and future-proofing.

Highlights

  • Keras 3 Migration: Updated the metric learning example to ensure full compatibility with Keras 3, addressing API changes in imports, loss functions, and tensor operations.
  • Loss Function Configuration: Modified the MultiSimilarityLoss initialization to explicitly set reduction='sum_over_batch_size', aligning with Keras 3's expected behavior for loss aggregation.
  • Eager Execution: Enabled eager execution for the model by adding run_eagerly=True to the model.compile method, which can be beneficial for debugging and dynamic graph construction.
  • Tensor Operations Refactoring: Refactored tensor handling for visualization and confusion matrix generation, specifically converting y_display to a NumPy array and using keras.ops.take for no_match_queries to maintain Keras 3 compatibility.
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 migrates the metric learning example to be compatible with Keras 3. The changes correctly update the Keras import statement and tensor handling logic. However, I have a couple of suggestions. I've raised a concern about enabling run_eagerly=True, which can hurt performance and should ideally be avoided. I've also pointed out a minor redundancy in a variable assignment that can be cleaned up. Addressing these points will improve the quality and performance of the example.

optimizer=keras.optimizers.Adam(learning_rate),
loss=loss,
steps_per_execution=10,
run_eagerly=True,
Copy link
Contributor

Choose a reason for hiding this comment

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

high

Enabling run_eagerly=True can significantly degrade training performance as it disables graph optimizations. While it can be useful for debugging, it's generally recommended to make the model and training loop compatible with graph execution (i.e., by setting run_eagerly=False or removing the argument). Was this added to work around a specific incompatibility with Keras 3's graph mode? If so, it would be better to address the underlying issue to ensure the example runs efficiently.

@Shi-pra-19
Copy link
Author

I added run_eagerly=True because, without it, the training raised an "OperatorNotAllowedInGraphError" caused by an internal operation in the TensorFlow Similarity library.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants