keras
keras copied to clipboard
Unexpected breaking change: Optimizer.get_weights() removed
It seems like Optimizer.get_weights() is being removed. SciKeras was using it to serialize optimizer weights since SavedModel silently fails to do so (see https://github.com/tensorflow/tensorflow/issues/44670 and other linked issues, this is a longstanding bug that hasn't been fixed). Could someone fill me in on what the plans are going forward? Pickling models is an essential part how Scikit-Learn operates and hence SciKeras gets completely broken if TensorFlow models can't be serialized.
@gowthamkpr, I was able to reproduce the issue on tensorflow v2.8, v2.9 and nightly. Kindly find the gist of it here.
@chenmoneygithub can you take a look here?
@adriangb Thanks for reporting the issue!
There has not been any change on get_weights() for months. For loading optimizer weights, please make sure you call load_weights() if you want to get the optimizer weights. For example:
def roundtrip(model: keras.Model) -> keras.Model:
save_dir = "/tmp/mymodel"
model.save(save_dir)
restored = keras.models.load_model(save_dir)
restored.load_weights(save_dir)
return restored
On tensorflow==2.8.0:
>> import tensorflow as tf
>> tf.keras.optimizers.get("rmsprop").get_weights
<bound method OptimizerV2.get_weights of <keras.optimizer_v2.rmsprop.RMSprop object at 0x7f9f580e9360>>
On tf-nightly (9/8/2022):
>> import tensorflow as tf
>> tf.keras.optimizers.get("rmsprop").get_weights
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'RMSprop' object has no attribute 'get_weights'
This is because the new keras.optimizers.optimizer_experimental.Optimizer does not have a get_weights() method while the old keras.optimizers.optimizer_v2.optimizer_v2.OptimizerV2 does.
@chenmoneygithub any updates on this? This is breaking SciKeras (and presumably other downstream things)
Here's notebooks proving this is broken:
- stable (2.9.2): https://colab.research.google.com/drive/1VtH0AyV5HVolRzSla4U6YCY1MusS6sN4#scrollTo=dZYmmrM3WwFA
- nightly: https://colab.research.google.com/drive/15LZMxfQiXixHNWtL-n573RJMdgWQbbfy#scrollTo=P14dQaVIXBU6