can't pickle _thread.RLock objects using keras

我的梦境 提交于 2019-12-25 02:57:08

问题


I create keras model and then save that model to pickle

program code :

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.

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)}

回答1:


I found solution:

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()


来源:https://stackoverflow.com/questions/54421480/cant-pickle-thread-rlock-objects-using-keras

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!