Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'Adam' object has no attribute 'build' during unpickling

I'm training a Keras model and saving it for later use using pickle.

When I unpickle I get this error:

AttributeError: 'Adam' object has no attribute 'build'

Here's the code:

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
import pickle

model = Sequential()
model.add(Dense(32, activation='relu', input_shape=(3,)))
model.add(Dense(1, activation='linear'))  # Use linear activation for regression
model.compile(loss='mean_squared_error', optimizer='adam')

pickle.dump(model, open("m.pkl", 'wb'))
loadedModel = pickle.load(open("m.pkl", 'rb'))

I get this error with TensorFlow 2.11.x and 2.13.0-rc0 on MacOS M1

like image 880
ColBeseder Avatar asked Aug 31 '25 17:08

ColBeseder


1 Answers

Actually, the same error happens using the keras save and load_model. I found this entry on the keras GitHub page:

https://github.com/keras-team/keras/issues/18278

This indicates that on M1/M2 Macs, keras reverts to the legacy optimizers, and that version of Adam does not have a build() function. I've tried other optimizers, but they appear to have this issue as well.

I have not found a solution yet...

like image 96
user2824214 Avatar answered Sep 02 '25 07:09

user2824214