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']) 
Alex Waygood
  • 6,304
  • 3
  • 24
  • 46
Dibyaranjan Jena
  • 189
  • 1
  • 2
  • 10
  • 4
    Is there any reason you don't want to use tensorflow's built in model saving functionality? https://www.tensorflow.org/api_docs/python/tf/keras/Model#save – Bhaskar Nov 03 '20 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. – Dibyaranjan Jena Nov 04 '20 at 14:37
  • 1
    Maybe this solution could help https://stackoverflow.com/a/42763323/8196143 – youssef kamel Feb 13 '21 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! –  May 12 '21 at 11:35

1 Answers1

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)
Tyler Tian
  • 597
  • 1
  • 6
  • 22