求助:tflearn中try/except加载预训练模型失败后训练报会话已关闭错误
Tflearn实现聊天机器人模型自动加载逻辑时触发闭会话报错
开发场景说明
- 开发身份:编程初学者
- 运行环境:配置了tflearn库的Anaconda虚拟环境,开发目标为搭建AI聊天机器人
- 代码设计目标:通过try/except逻辑实现模型加载流程自动适配:优先读取本地已存储的预训练模型
model.tflearn,若模型文件不存在则自动启动模型训练流程,训练完成后将模型持久化保存,避免重复训练占用资源 - 核心逻辑代码片段:
try: model.load("model.tflearn") except: model.fit(training, output, n_epoch=300, batch_size=8, show_metric=True) model.save("model.tflearn")
异常表现说明
- 异常触发条件:本地不存在可正常加载的
model.tflearn预训练模型文件时,程序运行中断抛出错误 - 无异常对照场景:
- 本地存在完整、可正常加载的预训练模型文件时,程序可走完所有流程正常运行
- 移除try/except异常捕获逻辑,每次运行直接执行模型训练流程时,程序可正常运行完成训练与保存
- 完整报错栈信息:
程序最终抛出的核心错误为RuntimeError: Attempted to use a closed Session.,完整栈跟踪内容如下:
Traceback (most recent call last): File "C:/Users/Eric/PycharmProjects/ai_chatbot/nlp.py", line 82, in <module> model.fit(training, output, n_epoch=300, batch_size=8, show_metric=True) File "C:\Users\Eric\anaconda3\envs\chatbot\lib\site-packages\tflearn\models\dnn.py", line 206, in fit callbacks=callbacks) File "C:\Users\Eric\anaconda3\envs\chatbot\lib\site-packages\tflearn\helpers\trainer.py", line 344, in fit show_metric) File "C:\Users\Eric\anaconda3\envs\chatbot\lib\site-packages\tflearn\helpers\trainer.py", line 826, in _train tflearn.is_training(True, session=self.session) File "C:\Users\Eric\anaconda3\envs\chatbot\lib\site-packages\tflearn\config.py", line 95, in is_training tf.get_collection('is_training_ops')[0].eval(session=session) File "C:\Users\Eric\anaconda3\envs\chatbot\lib\site-packages\tensorflow\python\framework\ops.py", line 921, in eval return _eval_using_default_session(self, feed_dict, self.graph, session) File "C:\Users\Eric\anaconda3\envs\chatbot\lib\site-packages\tensorflow\python\framework\ops.py", line 5512, in _eval_using_default_session return session.run(tensors, feed_dict) File "C:\Users\Eric\anaconda3\envs\chatbot\lib\site-packages\tensorflow\python\client\session.py", line 968, in run run_metadata_ptr) File "C:\Users\Eric\anaconda3\envs\chatbot\lib\site-packages\tensorflow\python\client\session.py", line 1114, in _run raise RuntimeError('Attempted to use a closed Session.') RuntimeError: Attempted to use a closed Session.
内容的提问来源于stack exchange,提问作者Eric
相关产品推荐
相关产品推荐

