Fix mixed precision compatibility issues in Bayesian Neural Networks example#2240
Fix mixed precision compatibility issues in Bayesian Neural Networks example#2240samudraneel05 wants to merge 3 commits intokeras-team:masterfrom
Conversation
Summary of ChangesHello @samudraneel05, 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 addresses and resolves critical data type compatibility issues within the Bayesian Neural Networks example, particularly when running with mixed precision. By explicitly enforcing Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request addresses mixed precision compatibility issues in the Bayesian Neural Networks example by explicitly setting dtype=tf.float32 in various TensorFlow Probability and Keras layers. The changes are generally correct and should resolve the reported failures when mixed precision is enabled. I've provided a few suggestions to improve the implementation by using the dtype parameter passed to functions instead of hardcoding tf.float32, which makes the code more robust and maintainable. I also have a question regarding the change of imports from tensorflow.keras to tf_keras, as it's not mentioned in the description and seems to deviate from the Keras 3 migration guide.
|
Hey @sachinprasadhs, if the scope of the fix appears small, I'm happy to also update the |
Fixes #1860
This PR fixes mixed precision/dtype compatibility issues in the Bayesian Neural Networks example -
examples/keras_recipes/bayesian_neural_networks.pythat caused the example to fail when mixed precision is enabled.Problem
The example was failing with dtype-related errors when using TensorFlow Probability layers. The layers were not explicitly specifying dtypes, leading to type mismatches, errors and other dtype incompatibility issues.
Changes Made
dtype=tf.float32for all TensorFlow Probability distribution parameters:prior(): Added dtype specification totf.zeros()andtf.ones()in MultivariateNormalDiagposterior(): Added dtype to VariableLayer and MultivariateNormalTriLcreate_bnn_model(): Added dtype to DenseVariational layercreate_probablistic_bnn_model(): Added dtype to DenseVariational and Dense layers, and IndependentNormal output layernegative_loglikelihood(): Added explicit casting of targets to tf.float32Testing
Ran tests on my local machine (Macbook M2 with Python 3.13), converted .py to ipynb and ran the full notebook without errors on mixed precision data.