-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Revert "Fix incorrect reference to model in Transformer class" #2138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This reverts commit 9f8949e.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @divyashreepathihalli, 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 serves to undo a prior modification that altered how the loss was calculated within the Transformer model's training and testing routines. By reverting to the previous implementation, it addresses potential issues or unintended side effects that may have arisen from the original change.
Highlights
- Revert Previous Change: This pull request fully reverts the changes introduced in
keras-team/keras-io#2078, which aimed to 'Fix incorrect reference to model in Transformer class'. - Loss Calculation Correction: Specifically, it changes the
compute_lossmethod call within thetrain_stepandtest_stepmethods of theTransformerclass inexamples/audio/transformer_asr.pyfromself.compute_lossback tomodel.compute_loss.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| 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 issue 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 is currently in preview and 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 to provide feedback.
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
-
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. ↩
There was a problem hiding this 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 reverts a previous fix, reintroducing a problematic reference to a global model variable within the Transformer class's train_step and test_step methods. This change is incorrect as it makes the class non-portable and reliant on global state, which can lead to NameError exceptions. The review comments suggest changing the calls back to self.compute_loss to ensure the code is robust and follows standard object-oriented practices.
| one_hot = tf.one_hot(dec_target, depth=self.num_classes) | ||
| mask = tf.math.logical_not(tf.math.equal(dec_target, 0)) | ||
| loss = self.compute_loss(None, one_hot, preds, sample_weight=mask) | ||
| loss = model.compute_loss(None, one_hot, preds, sample_weight=mask) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change re-introduces a reference to a global variable model inside an instance method. This is problematic for a couple of reasons:
- Scoping:
modelis not defined within thetrain_stepmethod's scope. It relies on a global variable, which makes theTransformerclass non-reusable and will likely cause aNameErrorif the class is used in a context where the globalmodelvariable is not present. - Object-Oriented Practice: Inside a class method, the instance should be referred to via
self. Sincetrain_stepis a method of your Keras model,selfis the model instance.
The correct and robust approach is to use self.compute_loss.
| loss = model.compute_loss(None, one_hot, preds, sample_weight=mask) | |
| loss = self.compute_loss(None, one_hot, preds, sample_weight=mask) |
| one_hot = tf.one_hot(dec_target, depth=self.num_classes) | ||
| mask = tf.math.logical_not(tf.math.equal(dec_target, 0)) | ||
| loss = self.compute_loss(None, one_hot, preds, sample_weight=mask) | ||
| loss = model.compute_loss(None, one_hot, preds, sample_weight=mask) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to the issue in train_step, this change incorrectly uses a global model variable within the test_step instance method. To ensure the Transformer class is robust, reusable, and follows standard object-oriented principles, the model's own methods should be accessed via self.
Please use self.compute_loss here as well.
| loss = model.compute_loss(None, one_hot, preds, sample_weight=mask) | |
| loss = self.compute_loss(None, one_hot, preds, sample_weight=mask) |
Reverts #2078