The objective is to save my model as a .joblib
file. Using joblib.dump
method I get the error found in the end of this question. I have indeed found some solutions to save my model in other formats, as in .pb
with model.save
, but I need a .joblib
file to be able to reuse it later in a code expecting such format.
I've checked different similar issues, but none have the same root cause, some are about serialization problem due to Lambda expressions - not my case apparently - namely this question as well as this one.
My code for saving currently is:
RANDOM_SEED = 314
TEST_PCT = 0.3
df = pd.read_csv("C:/Users/terzan matthieu/Desktop/STAGE FDR/creditcard.csv")
df_norm = df.copy()
df_norm['Time'] = StandardScaler().fit_transform(df_norm['Time'].values.reshape(-1, 1))
df_norm['Amount'] = StandardScaler().fit_transform(df_norm['Amount'].values.reshape(-1, 1))
train_x, test_x = train_test_split(df_norm, test_size=TEST_PCT, random_state=RANDOM_SEED)
test_y = test_x['Class']
test_x = test_x.drop(['Class'], axis=1)
train_x = train_x[train_x.Class == 0]
train_x = train_x.drop(['Class'], axis=1)
train_x = train_x.values
test_x = test_x.values
nb_epoch = 50
batch_size = 128
input_dim = train_x.shape[1] #nb de colonnes, 30
encoding_dim = 18
hidden_dim1 = 10 #int(encoding_dim /
hidden_dim2 = 6
learning_rate = 1e-7
input_layer = Input(shape=(input_dim, ))
encoder = Dense(encoding_dim, activation="tanh",
activity_regularizer=regularizers.l1(learning_rate))(input_layer)
encoder = Dense(hidden_dim1, activation="elu")(encoder)
encoder = Dense(hidden_dim2, activation="tanh")(encoder)
decoder = Dense(hidden_dim2, activation='elu')(encoder)
decoder = Dense(hidden_dim1, activation='tanh')(decoder)
decoder = Dense(input_dim, activation='elu')(decoder)
autoencoder = Model(inputs=input_layer, outputs=decoder)
autoencoder.compile(optimizer='adam',
metrics=['accuracy'],
loss='mean_squared_error')
cp = ModelCheckpoint(filepath="model.h5",
save_best_only=True,
verbose=0)
tb = TensorBoard(log_dir='./logs',
histogram_freq=0,
write_graph=True,
write_images=True)
history = autoencoder.fit(x=train_x, y=train_x,
epochs=nb_epoch,
batch_size=batch_size,
shuffle=True,
validation_data=(test_x, test_x),
verbose=1,
callbacks=[cp, tb]).history
autoencoder = load_model('model.h5')
joblib.dump(autoencoder,'firstautoencoder')
And the error stacktrace:
File "C:\Users\terzan matthieu\Desktop\Algos_DL\Algo4\Autoencoder1FINAL.py", line 191, in <module>
joblib.dump(autoencoder,'firstautoencoder')
File "C:\Users\terzan matthieu\anaconda3\lib\site-packages\joblib\numpy_pickle.py", line 480, in dump
NumpyPickler(f, protocol=protocol).dump(value)
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 485, in dump
self.save(obj)
File "C:\Users\terzan matthieu\anaconda3\lib\site-packages\joblib\numpy_pickle.py", line 282, in save
return Pickler.save(self, obj)
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 601, in save
self.save_reduce(obj=obj, *rv)
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 715, in save_reduce
save(state)
File "C:\Users\terzan matthieu\anaconda3\lib\site-packages\joblib\numpy_pickle.py", line 282, in save
return Pickler.save(self, obj)
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 558, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 969, in save_dict
self._batch_setitems(obj.items())
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 995, in _batch_setitems
save(v)
File "C:\Users\terzan matthieu\anaconda3\lib\site-packages\joblib\numpy_pickle.py", line 282, in save
return Pickler.save(self, obj)
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 558, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 929, in save_list
self._batch_appends(obj)
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 953, in _batch_appends
save(x)
File "C:\Users\terzan matthieu\anaconda3\lib\site-packages\joblib\numpy_pickle.py", line 282, in save
return Pickler.save(self, obj)
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 601, in save
self.save_reduce(obj=obj, *rv)
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 715, in save_reduce
save(state)
File "C:\Users\terzan matthieu\anaconda3\lib\site-packages\joblib\numpy_pickle.py", line 282, in save
return Pickler.save(self, obj)
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 558, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 969, in save_dict
self._batch_setitems(obj.items())
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 995, in _batch_setitems
save(v)
File "C:\Users\terzan matthieu\anaconda3\lib\site-packages\joblib\numpy_pickle.py", line 282, in save
return Pickler.save(self, obj)
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 558, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 929, in save_list
self._batch_appends(obj)
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 956, in _batch_appends
save(tmp[0])
File "C:\Users\terzan matthieu\anaconda3\lib\site-packages\joblib\numpy_pickle.py", line 282, in save
return Pickler.save(self, obj)
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 601, in save
self.save_reduce(obj=obj, *rv)
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 715, in save_reduce
save(state)
File "C:\Users\terzan matthieu\anaconda3\lib\site-packages\joblib\numpy_pickle.py", line 282, in save
return Pickler.save(self, obj)
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 558, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 969, in save_dict
self._batch_setitems(obj.items())
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 995, in _batch_setitems
save(v)
File "C:\Users\terzan matthieu\anaconda3\lib\site-packages\joblib\numpy_pickle.py", line 282, in save
return Pickler.save(self, obj)
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 601, in save
self.save_reduce(obj=obj, *rv)
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 715, in save_reduce
save(state)
File "C:\Users\terzan matthieu\anaconda3\lib\site-packages\joblib\numpy_pickle.py", line 282, in save
return Pickler.save(self, obj)
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 558, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 969, in save_dict
self._batch_setitems(obj.items())
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 995, in _batch_setitems
save(v)
File "C:\Users\terzan matthieu\anaconda3\lib\site-packages\joblib\numpy_pickle.py", line 282, in save
return Pickler.save(self, obj)
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 601, in save
self.save_reduce(obj=obj, *rv)
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 715, in save_reduce
save(state)
File "C:\Users\terzan matthieu\anaconda3\lib\site-packages\joblib\numpy_pickle.py", line 282, in save
return Pickler.save(self, obj)
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 558, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 969, in save_dict
self._batch_setitems(obj.items())
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 995, in _batch_setitems
save(v)
File "C:\Users\terzan matthieu\anaconda3\lib\site-packages\joblib\numpy_pickle.py", line 282, in save
return Pickler.save(self, obj)
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 601, in save
self.save_reduce(obj=obj, *rv)
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 715, in save_reduce
save(state)
File "C:\Users\terzan matthieu\anaconda3\lib\site-packages\joblib\numpy_pickle.py", line 282, in save
return Pickler.save(self, obj)
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 558, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 969, in save_dict
self._batch_setitems(obj.items())
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 995, in _batch_setitems
save(v)
File "C:\Users\terzan matthieu\anaconda3\lib\site-packages\joblib\numpy_pickle.py", line 282, in save
return Pickler.save(self, obj)
File "C:\Users\terzan matthieu\anaconda3\lib\pickle.py", line 576, in save
rv = reduce(self.proto)
TypeError: cannot pickle '_thread.RLock' object
Why exactly is this happening? thank you in advance for any help :)