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

Model Accuracy Degradation by 6x when Switching TF_USE_LEGACY_KERAS from "1" (Keras 2) to "0" (Keras 3) #20320

Open
Lw-Cui opened this issue Oct 3, 2024 · 3 comments
Assignees
Labels
stat:awaiting response from contributor type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited.

Comments

@Lw-Cui
Copy link

Lw-Cui commented Oct 3, 2024

Summary

There is a significant degradation in model performance when changing the TF_USE_LEGACY_KERAS environment variable between Keras 2 and Keras 3 in an Encoder-Decoder Network for Neural Machine Translation. With os.environ["TF_USE_LEGACY_KERAS"] = "1" (Keras 2), the validation set accuracy is much higher (60% v.s. 10%) compared to when os.environ["TF_USE_LEGACY_KERAS"] = "0" (Keras 3), despite no changes in the model architecture or training procedure.

System Information:

  • Python version: 3.10.12 (main, Sep 11 2024, 15:47:36) [GCC 11.4.0]
  • TensorFlow version: 2.17.0
  • Keras version: 3.4.1
  • Environment: Google Colab

Steps to Reproduce:

  1. Set os.environ["TF_USE_LEGACY_KERAS"] = "1" to use Keras 2 and run the Encoder-Decoder model.
  2. Set os.environ["TF_USE_LEGACY_KERAS"] = "0" to use Keras 3 and run the same model.
  3. Compare the validation accuracy between the two setups.

Expected Results:

The validation accuracy should remain consistent between both runs, or at least be comparable.

Looking for guidance on the cause of this discrepancy and possible ways to resolve this performance issue.

@mehtamansi29
Copy link
Collaborator

Hi @Lw-Cui -

Thanks for reporting the issue. I am able to reproduce this issue. But here while using os.environ["TF_USE_LEGACY_KERAS"] = "1", it means you are trying to use Keras2 with Tensorflow2.16+ version. And there is some custom layers with Tensorflow API so it is usually easy to convert the code to be backend-agnostic and os.environ["TF_USE_LEGACY_KERAS"] = "0" it means work as keras3 version with tensorflow backend.

There is some legacy features changes while migrating from Keras2 (tf.keras) to Keras3 (multibackend- keras on the top of tensorflow,jax and pytorch backend).

Here you can find more details about legacy features and Transitioning to backend-agnostic Keras 3.

Because of legacy features and Transitioning to backend-agnostic, it will impact accuracy while for Keras3 os.environ["TF_USE_LEGACY_KERAS"] = "0". For Keras3 os.environ["TF_USE_LEGACY_KERAS"] = "0", you can train the model with more epochs will increase the accuracy.

Let me know if you required more details. Thanks..!!

@mehtamansi29 mehtamansi29 added type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited. stat:awaiting response from contributor labels Oct 7, 2024
@Lw-Cui
Copy link
Author

Lw-Cui commented Oct 7, 2024

Thank you for the explanation. I now understand that converting the code to backend-agnostic Keras 3 is the best approach. However, for learning purposes, could you explain why, despite the code remaining the same, there is such a significant drop in accuracy when transitioning from Keras 2 to Keras 3? What are the specific reasons behind this?

@mehtamansi29
Copy link
Collaborator

Hi @Lw-Cui -

Here you can find more details about legacy features and Transitioning to backend-agnostic Keras 3.

Because of this removal of features, changes in architecture, layers, metrics in keras3(high level API contains 3 backend-tensorflow, jax, torch) can impact on drop in accuracy. You can re-train the model with more epochs, changing hyperparamter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stat:awaiting response from contributor type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited.
Projects
None yet
Development

No branches or pull requests

2 participants