Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"TypeError: can't pickle weakref objects" when pickling a deep learning model

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']) 
like image 220
Dibyaranjan Jena Avatar asked Nov 03 '20 15:11

Dibyaranjan Jena


1 Answers

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)
like image 189
Yuvraj verma Avatar answered Oct 19 '22 23:10

Yuvraj verma