-
Notifications
You must be signed in to change notification settings - Fork 45
Description
System information.
- Have I written custom code (as opposed to using a stock example script provided in Keras):
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Mac arm M2 Ventura 13.3
- TensorFlow installed from (source or binary): binary
- TensorFlow version (use command below): 2.13.0
- Python version: 3.11.4
- Bazel version (if compiling from source):
- GPU model and memory: Apple M2 Pro 16 GB
- Exact command to reproduce:
Describe the problem.
Cannot deserialize a model compiled with the tf.keras.optimizers.Adam
optimizer on M1/M2 macs.
Describe the current behavior.
When creating a Keras model on a M1/M2 mac the following messages are displayed indicating that the default optimizer tf.keras.optimizers.Adam
runs slowly on M1/M2 macs. Keras then "falls back" to the legacy optimizer tf.keras.optimizers.legacy.Adam
.
WARNING:absl:At this time, the v2.11+ optimizer `tf.keras.optimizers.Adam` runs slowly on M1/M2 Macs, please use the legacy Keras optimizer instead, located at `tf.keras.optimizers.legacy.Adam`.
WARNING:absl:There is a known slowdown when using v2.11+ Keras optimizers on M1/M2 Macs. Falling back to the legacy Keras optimizer, i.e., `tf.keras.optimizers.legacy.Adam`.
Then, when serializing and trying to deserialize the model the following error is raised:
self = <keras.src.optimizers.legacy.adam.Adam object at 0x29b503a90>
name = 'build'
def __getattribute__(self, name):
"""Overridden to support hyperparameter access."""
try:
> return super().__getattribute__(name)
E AttributeError: 'Adam' object has no attribute 'build'
Indicating that the legacy optimizer cannot be deserialized because it does not implement the build
attribute. On all other platforms the model can be serialized and deserialized because the tf.keras.optimizers.Adam
is used rather than the legacy version.
Describe the expected behavior.
The expected behaviour is that the model can be deserialized without raising an attribute error.
Standalone code to reproduce the issue.
import tensorflow as tf
import pickle
model = tf.keras.Model(inputs={"a": tf.keras.Input(shape=(10,))}, outputs={"b": tf.keras.Input(shape=(10,))})
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), loss='mse')
with open('test.pkl', 'wb') as f:
pickle.dump(model, f)
with open('test.pkl', 'rb') as f:
opt = pickle.load(f)