如何通过Keras优化循环训练模型避免数据跑时内存泄漏问题?
- 内容介绍
- 文章标签
- 相关推荐
本文共计493个文字,预计阅读时间需要2分钟。
在完成完模型训练后,添加以下代码行即可释放model占用的内存:pythonimport tensorflow as tffrom keras import backend as KK.clear_session()tf.reset_default_graph()
在使用完模型之后,添加这两行代码即可清空之前model占用的内存:
import tensorflow as tf from keras import backend as K K.clear_session() tf.reset_default_graph()
补充知识:keras 多个模型测试阶段速度越来越慢问题的解决方法
问题描述
在实际应用或比赛中,经常会用到交叉验证(10倍或5倍)来提高泛化能力,这样在预测时需要加载多个模型。常用的方法为
mods = [] from keras.utils.generic_utils import CustomObjectScope with CustomObjectScope({}): for model_file in tqdm.tqdm(model_files): mod = keras.models.load_model(model_file) mods.append(mod) return mods
使用这种方式时会发现,刚开始模型加载速度很快,但随着加载的模型数量增多,加载速度越来越慢,甚至延长了3倍以上。
本文共计493个文字,预计阅读时间需要2分钟。
在完成完模型训练后,添加以下代码行即可释放model占用的内存:pythonimport tensorflow as tffrom keras import backend as KK.clear_session()tf.reset_default_graph()
在使用完模型之后,添加这两行代码即可清空之前model占用的内存:
import tensorflow as tf from keras import backend as K K.clear_session() tf.reset_default_graph()
补充知识:keras 多个模型测试阶段速度越来越慢问题的解决方法
问题描述
在实际应用或比赛中,经常会用到交叉验证(10倍或5倍)来提高泛化能力,这样在预测时需要加载多个模型。常用的方法为
mods = [] from keras.utils.generic_utils import CustomObjectScope with CustomObjectScope({}): for model_file in tqdm.tqdm(model_files): mod = keras.models.load_model(model_file) mods.append(mod) return mods
使用这种方式时会发现,刚开始模型加载速度很快,但随着加载的模型数量增多,加载速度越来越慢,甚至延长了3倍以上。

