TypeErr:无法腌制“ _thread.RLock”对象-目标:以.joblib格式保存模型以在需要.joblib之后重用

问题描述

目标是将模型保存为.joblib文件。使用joblib.dump方法,我得到在该问题结尾处发现的错误。我确实找到了一些解决方案,可以将模型保存为其他格式,例如将.pbmodel.save一起保存,但是我需要一个.joblib文件,以便以后可以在期望这样的代码中重用它格式。

我检查了不同的类似问题,但是没有一个根本原因,有一些是关于Lambda表达式引起的序列化问题-显然不是我的情况-this questionthis 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 (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...