python - TypeErr:无法腌制“_thread.RLock”对象 - 目标:以 .joblib 格式保存模型以便在需要 .joblib 后重用
问题描述
目标是将我的模型保存为.joblib
文件。使用joblib.dump
方法我得到了这个问题末尾发现的错误。我确实找到了一些解决方案来以其他格式保存我的模型,例如.pb
with model.save
,但是我需要一个.joblib
文件以便以后能够在期望这种格式的代码中重用它。
我检查了不同的类似问题,但没有一个具有相同的根本原因,有些是由于 Lambda 表达式导致的序列化问题 - 显然不是我的情况 - 即this question以及this one。
我目前保存的代码是:
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')
和错误堆栈跟踪:
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
为什么会发生这种情况?预先感谢您的任何帮助 :)
解决方案
推荐阅读
- javascript - stack all divs to the top of container without leaving space below
- sql - How to insert records for next 6 days (Sun-Fri) when just date for (Sat) is given
- spring - Actuator not working with non spring boot application
- python-3.x - Market IDs for events cannot be found through their event IDs - Betfair
- python - Python unittest 更改变量看似超出范围
- python - Python: Complexity of functions with slicing tuples and strings
- python - tkinter 属性错误 - 对象没有属性。我不明白为什么它找不到这个对象的属性
- node.js - 处理这个 facebook 登录边缘案例的正确方法?
- r - Is it possible to use spacy without having administrator access?
- mysql - MySQL 结合两个查询得到两个计数