如何解决TypeErr:无法腌制“ _thread.RLock”对象-目标:以.joblib格式保存模型以在需要.joblib之后重用
目标是将模型保存为.joblib
文件。使用joblib.dump
方法,我得到在该问题结尾处发现的错误。我确实找到了一些解决方案,可以将模型保存为其他格式,例如将.pb
与model.save
一起保存,但是我需要一个.joblib
文件,以便以后可以在期望这样的代码中重用它格式。
我检查了不同的类似问题,但是没有一个根本原因,有一些是关于Lambda表达式引起的序列化问题-显然不是我的情况-this question和this one。 / p>
我当前的保存代码是:
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 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 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 956,in _batch_appends
save(tmp[0])
File "C:\Users\terzan matthieu\anaconda3\lib\site-packages\joblib\numpy_pickle.py",line 576,in save
rv = reduce(self.proto)
TypeError: cannot pickle '_thread.RLock' object
为什么会这样呢? 预先感谢您的帮助:)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。