问题
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