TensorFlow 2.16 / Keras 3 have undocumented breaking API changes #63792
Labels
comp:keras
Keras related issues
TF 2.16
type:docs-feature
Doc issues for new feature, or clarifications about functionality
type:feature
Feature requests
Issue type
Documentation Feature Request
Have you reproduced the bug with TensorFlow Nightly?
Yes
Source
source
TensorFlow version
2.16.1
Custom code
No
OS platform and distribution
No response
Mobile device
No response
Python version
No response
Bazel version
No response
GCC/compiler version
No response
CUDA/cuDNN version
No response
GPU model and memory
No response
Current behavior?
With the (documented) switch to Keras 3, TensorFlow 2.16 has introduced undocumented API-breaking changes that derogate to SemVer and results in unexpected code maintenance costs for depending projects.
While I understand the interest of switching to Keras 3 (and acknowledge the possibility to keep using Keras 2 as backend, albeit non-trivial to impose on end-users of a third-party solution that uses TensorFlow as dependency), I would have expected some kind of effort on documenting parts of the API that changed due to the switch. This Keras issue and this guide are presented as listing breaking changes, but seem not to be covering everything that changed, especially when calling keras from TensorFlow rather than as a primary endpoint for setting up models (agnostic to the computation backend).
As I put effort in updating custom code that calls TensorFlow / Keras code, I notably noticed that:
tf.keras.Model.weights
(and its variants) as well astf.keras.optimizers.Optimizer.variables
no longer return a list oftf.Variable
instances, but instead return a list oftf.keras.Variable
that themselves wraptf.Variable
instances.tf.keras.losses.deserialize("mse")
no longer works, but was to the best of my knowledge never reported to be deprecated in previous versions.tf.keras.optimizers.Optimizer.variables()
has been removed in favor oftf.keras.optimizers.Optimizer.variables
.These changes can be adjusted for, but require a lot of
if hasattr(tf.keras, "version") and tf.keras.version().startswith("3"):
branching to keep supporting not-so-old versions while adding support for newer TF versions, and, most importantly, require some tedious experiments to find out what has changed and how. As such, I categorize his issue as "Documentation Feature Request" as I obviously do not expect changes to be rolled back, but believe that it would be a sensible gesture towards the community to publish a clearer and more exhaustive guide as to what has changed with the introduction of Keras 3, and how to accommodate third-party code that does not want to impose on end-users the choice of a single TF version.Standalone code to reproduce the issue
As a single example of what I am talking about:
This works with TensorFlow 2.15:
But it fails for numerous reasons in TensorFlow 2.16.1 (with Keras 3 backend):
state = {v.name: v.value() for v in optim.variables()}
should be replaced withstate = {v.path: v.value.value() for v in optim.variables}
...Relevant log output
No response
The text was updated successfully, but these errors were encountered: