基于Keras 循环训练模型跑数据时内存泄漏的解决方式
更多python视频教程请到菜鸟教程https://www.piaodoo.com/
在使用完模型之后,添加这两行代码即可清空之前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倍以上。那么为什么会出现这种现象呢?
原因
由于te