Skip to content
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

Fixes lower train metrics when using Keras Masking (SequenceMaskRandom, SequenceMaskLast) #983

Merged
merged 3 commits into from
Feb 15, 2023

Conversation

gabrielspmoreira
Copy link
Member

@gabrielspmoreira gabrielspmoreira commented Feb 14, 2023

Fixes #961

Goals ⚽

This PR fix an issue that caused metrics obtained with model.fit() being much lower than the ones obtained with model.evaluate() when Keras Masking is used.
This bug was observed when comparing training and evaluation metrics of a Transformer example (as described in #961 ), which makes usage of Keras Masking (SequenceMaskRandom, SequenceMaskLast) to select items of the sequence for training / eval.

Implementation Details 🚧

  • After investigation, I found out that the issue was been caused by the @tf.function decorator we had in model.train_compute_metrics(). After replacing a condition inside that function by tf.cond(), it was possible to remove the @tf.function decorator and fix the error when using Keras Masking (i.e., setting predictions._keras_mask).

Testing Details 🔍

  • Included tests in graph mode for test_train_metrics_steps, to double check that the logic inside model.train_compute_metrics() that skips steps for computing metrics continue to working in eager and graph mode.

@gabrielspmoreira gabrielspmoreira self-assigned this Feb 14, 2023
@gabrielspmoreira gabrielspmoreira added the bug Something isn't working label Feb 14, 2023
@gabrielspmoreira gabrielspmoreira changed the title Fixes lower train metrics when using Keras Masking Fixes lower train metrics when using Keras Masking (SequenceMaskRandom, SequenceMaskLast) Feb 14, 2023
@github-actions
Copy link

Documentation preview

https://nvidia-merlin.github.io/models/review/pr-983

@gabrielspmoreira
Copy link
Member Author

rerun tests

@gabrielspmoreira gabrielspmoreira force-pushed the tf/fix_training_smaller_accuracy branch from 0cc59fe to 08ff219 Compare February 14, 2023 19:51
@edknv
Copy link
Contributor

edknv commented Feb 14, 2023

The decorator was added to fix that dataloader issue. There is unsuitability with list columns in the dataloader and adding the decorator fixed it. Do we still have issues with metrics if we use both tf.cond and tf.function?

@rnyak rnyak requested a review from sararb February 15, 2023 15:25
@rnyak rnyak added this to the Merlin 23.02 milestone Feb 15, 2023
@rnyak
Copy link
Contributor

rnyak commented Feb 15, 2023

@gabrielspmoreira I tested the PR and now I am getting more consistent results between model.fit() and model.evaluate().

@rnyak
Copy link
Contributor

rnyak commented Feb 15, 2023

rerun tests

@gabrielspmoreira
Copy link
Member Author

The decorator was added to fix that dataloader issue. There is unsuitability with list columns in the dataloader and adding the decorator fixed it. Do we still have issues with metrics if we use both tf.cond and tf.function?

Hi Edward. I remember you have added some @tf.function decorator to deal with list features.
For this fix, I removed the @tf.function only from the train_compute_metrics(), which according to git blame was added by myself a while ago to be able to compute train metrics each N steps, in order to speed up training. So I think that @tf.function decorator is not related to you additions to fix the dataloader issue.

Copy link
Contributor

@edknv edknv left a comment

Choose a reason for hiding this comment

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

Based on the discussion offline, it sounds like the CI failure is unrelated to tf.function. Please ignore my previous comment.

@rnyak rnyak merged commit c5c0f03 into main Feb 15, 2023
sararb pushed a commit that referenced this pull request Feb 28, 2023
…m, SequenceMaskLast) (#983)

* Removed @tf.function from train_compute_metrics, as it is not needed and is causing a lower than real accuracy in model.fit() when using preds._keras_mask

* Turning if condition into tf.cond to remove tf.function decorator

* Making the should_compute_train_metrics_for_batch variable True by default
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Inconsistent results between training and eval metrics when training a session-based model
4 participants