12

When I run

pickle.dump(model,open('modelDL.pkl','wb'))

I get

TypeError: can't pickle weakref objects

I have a created a deep learning model which I am trying to save. The model:

model = Sequential()

model.add( Dense(30,activation='relu') )
model.add( Dropout(0.5) ) 
model.add( Dense(20,activation='relu') )
model.add( Dropout(0.5) ) 
model.add( Dense(20,activation='relu') )
model.add( Dropout(0.5) )     
model.add( Dense(1,activation='sigmoid') )

model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy']) 
4
  • 4
    Is there any reason you don't want to use tensorflow's built in model saving functionality? tensorflow.org/api_docs/python/tf/keras/Model#save
    – Bhaskar
    Nov 3, 2020 at 17:37
  • 2
    Thank you Bhaskar! I am able to save and load the model using tensorflow. But I am not sure why I was unable to do it using the pickle, because I used to save machine learning models using the pickle.dump method. If you could answer what is the error means , that would be great. Nov 4, 2020 at 14:37
  • 1
    Maybe this solution could help stackoverflow.com/a/42763323/8196143 Feb 13, 2021 at 23:35
  • 1
    Currently tensorflow supports the model can be saved in two different file formats (SavedModel and HDF5). The TensorFlow SavedModel format is the default file format in TF2.x. However, models can be saved in HDF5 format. In TF1.x it defaults to HDF5.Thanks!
    – user11530462
    May 12, 2021 at 11:35

1 Answer 1

3

can't pickle weakref comes because Deep Learning models are too large and pickle only used for storing small models

Use this : HDF5 used for storing large data

from keras.models import load_model

model.save('my_model.h5')  # creates a HDF5 file 'my_model.h5'

returns a compiled model

identical to the previous one

model1 = load_model('my_model.h5')

y_pred = model1.predict(x_test)
1
  • Worked for me ! Kudos for the post-- put an end to my 3-day search !!!
    – Partha D.
    Apr 6, 2023 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.