python-3.x - tf.keras.Model.load_weights() 捕获 ResourceExhaustedError
问题描述
我有两个 ipynb 文件:train.ipynb
和predict.ipynb
. 我已经训练了一个带有拟合生成器(批量大小为 64)的模型,并在我尝试加载权重时train.ipynb
捕获了
我在 tensorflow v1.9 和 tensorflow docker 图像中使用 keras。ResourceExhaustedError
predict.ipynb
# train.ipynb
def network():
#[ A normal model]
return model
model = network()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit_generator(seq,shuffle=True,
epochs = 10, verbose=1
)
# save the model and weight after training
with open('model.json','w') as json_file:
json_file.write(model.to_json())
model.save_weights('model.h5')
clear_session() # tried to clear the session here
# saved both successfully
# model.h5(131MB)
成功保存后,我可以将其加载回里面train.ipynb
但是,当我在 中执行相同的操作时predict.ipynb
,会发现错误。
# train.ipynb
with open('model.json','r') as json_file:
test_model = model_from_json(json_file.read())
test_model.load_weights('model.h5')
# No error here
# predict.ipynb
with open('model.json','r') as json_file:
test_model = model_from_json(json_file.read())
test_model.load_weights('model.h5')
# Got the following error
ResourceExhaustedError: OOM when allocating tensor with shape[28224,1024] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc
任何帮助表示赞赏!
解决方案
您是否同时运行两个笔记本?您的 GPU 内存不足。尝试nvidia-smi
在命令行中检查 GPU 的资源使用情况,但请注意默认情况下 TensorFlow 会占用所有可用的 GPU 内存。keras.backend.clear_session()
也可以提供帮助。
推荐阅读
- java - 从 Minecraft Mod 中的外部文件读取
- javascript - 通过单击按钮获取 DIV 行的输入
- three.js - webgl / Three.js - 减去体积
- python - 相关图误差除以零 matplot pandas numpy
- bash - 后台进程完成后,如何在 bash shell 中干净地打印消息?
- regex - 查找并替换不止一次出现的正则表达式模式
- html - 为什么border-radius在tcpdf中不起作用以及如何在一行中显示文本?
- mysql - MySQL - 慢查询
- spring - SpringData Couchbase 是否提供任何获取实时事件的方法?
- node.js - 无法从数据库中获取云功能的任何数据