Skip to content

Conversation

@divyashreepathihalli
Copy link
Collaborator

Reverts #2078

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.

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_loss method call within the train_step and test_step methods of the Transformer class in examples/audio/transformer_asr.py from self.compute_loss back to model.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

  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 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)
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

This change re-introduces a reference to a global variable model inside an instance method. This is problematic for a couple of reasons:

  • Scoping: model is not defined within the train_step method's scope. It relies on a global variable, which makes the Transformer class non-reusable and will likely cause a NameError if the class is used in a context where the global model variable is not present.
  • Object-Oriented Practice: Inside a class method, the instance should be referred to via self. Since train_step is a method of your Keras model, self is the model instance.

The correct and robust approach is to use self.compute_loss.

Suggested change
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)
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

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.

Suggested change
loss = model.compute_loss(None, one_hot, preds, sample_weight=mask)
loss = self.compute_loss(None, one_hot, preds, sample_weight=mask)

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.

3 participants