0

I am trying to use this code to fit an ANN using Keras and then pickle it:

early_stopping = EarlyStopping(monitor='val_loss', patience=4, mode='auto')
model = Sequential()
model.add(Dense(units=40, kernel_initializer='random_uniform', input_dim=x_train.shape[1], activation='relu'))
model.add(Dense(units=1, kernel_initializer='random_uniform', activation='sigmoid', W_regularizer=reg))
model.compile(loss='binary_crossentropy', optimizer='adam')
model.fit(x=x_train, y=y_train, epochs=1, validation_data=(x_val, y_val), callbacks=[early_stopping])

pickle_file_and_path = 'C:/Bla/DLModel20180816.sav'
pickle.dump(model, open(pickle_file_and_path, 'wb'))

Unfortunately, I get:

  pickle.dump(model, open(pickle_file_and_path, 'wb'))
TypeError: can't pickle _thread.RLock objects

Any ideas?

cs0815
  • 16,751
  • 45
  • 136
  • 299
  • 1
    Any particular reason why you want to pickle the model instead of using the built-in `model.save()` to store is as a `.h5`? – sdcbr Aug 16 '18 at 14:06
  • Thanks. TBH I was not aware of this. As long as I can persist it and then read in via another script. Could you please be so kind and suggest some code to write and read a 'Keras model' you way? Thanks! – cs0815 Aug 16 '18 at 14:10

1 Answers1

1

The canonical way of storing Keras models is to use the built-in model.save() method which will save the model into a HDF5 file.

Adapting your code:

model = Sequential()
model.add(Dense(units=40, kernel_initializer='random_uniform', input_dim=x_train.shape[1], activation='relu'))
model.add(Dense(units=1, kernel_initializer='random_uniform', activation='sigmoid', W_regularizer=reg))
model.compile(loss='binary_crossentropy', optimizer='adam')
model.fit(x=x_train, y=y_train, epochs=1, validation_data=(x_val, y_val), callbacks=[early_stopping])

# Save the model
model.save('./model.h5')

Afterwards, you can load it as follows:

from keras.models import load_model
model = load_model('./model.h5')

And start retraining or use the model for inference. Note that you can also store the only the weights with the save_weights() method. For the full documentation and more examples see the Keras website.

sdcbr
  • 7,021
  • 3
  • 27
  • 44
  • could it be that you miss load_model? – cs0815 Aug 16 '18 at 14:46
  • Also I am getting: Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 )-: – cs0815 Aug 16 '18 at 15:00
  • You can safely ignore that. [Here](https://stackoverflow.com/questions/47068709/your-cpu-supports-instructions-that-this-tensorflow-binary-was-not-compiled-to-u) is some explanation on what it means. – sdcbr Aug 16 '18 at 15:02
  • I also came across this: https://machinelearningmastery.com/save-load-keras-deep-learning-models/ It saves structure and weights separably though? – cs0815 Aug 16 '18 at 15:02