无法使用 keras 腌制 _thread.RLock 对象 [英] can't pickle _thread.RLock objects using keras

查看:74
本文介绍了无法使用 keras 腌制 _thread.RLock 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了 keras 模型,然后将该模型保存到 pickle

I create keras model and then save that model to pickle

程序代码:

print(base_learners)
# 9) 앙상블 모델 저장하기
pickle.dump(meta_learner, open('./models/meta_learner.pkl', 'wb'))
pickle.dump(base_learners, open('./models/base_learners.pkl', 'wb'))
pickle.dump(models, open('./models/models.pkl', 'wb'))

当我运行代码时,出现以下错误:

When I run the code, I get the following error:

Traceback (most recent call last):
  File "MODEL02_ensemble.py", line 265, in <module>
    main()
  File "MODEL02_ensemble.py", line 246, in main
    pickle.dump(base_learners, open('./models/base_learners.pkl', 'wb'))
TypeError: can't pickle _thread.RLock objects

怎么了?想了想,变量中输入的值是正常的.

What is the matter? When I think of it, the value entered in the variable is normal.

当我打印base_learners"时:

when I print the 'base_learners' :

{'dnn': <keras.engine.sequential.Sequential object at 0x000001C43DDE8EF0>, 'random forest': RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
            max_depth=4, max_features='sqrt', max_leaf_nodes=None,
            min_impurity_decrease=0.0, min_impurity_split=None,
            min_samples_leaf=2, min_samples_split=2,
            min_weight_fraction_leaf=0.0, n_estimators=100, n_jobs=-1,
            oob_score=False, random_state=42, verbose=0, warm_start=False), 'extra trees': ExtraTreesClassifier(bootstrap=False, class_weight=None, criterion='gini',
           max_depth=4, max_features='auto', max_leaf_nodes=None,
           min_impurity_decrease=0.0, min_impurity_split=None,
           min_samples_leaf=2, min_samples_split=2,
           min_weight_fraction_leaf=0.0, n_estimators=100, n_jobs=-1,
           oob_score=False, random_state=42, verbose=0, warm_start=False)}

推荐答案

我找到了解决方案:

from threading import Thread 

# base_learners has 3 models. so filename1~3
def hanlder(filename1,filename2,filename3):
    with open('./models/base_learners.pkl', 'wb') as file:
        pickle.dump(filename1, file)
        pickle.dump(filename2, file)
        pickle.dump(filename3, file)


t = Thread(target=hanlder, args=(base_learners))
t.start()

这篇关于无法使用 keras 腌制 _thread.RLock 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆