我创建了keras模型,然后将该模型保存到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'))
运行代码时,出现以下错误:
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
怎么了? 当我想到它时,在变量中输入的值是正常的。
当我打印'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)}
答案 0 :(得分:0)
我找到了解决方法:
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()