当你想要训练神经网络时,你需要设置一个批次大小。批次大小越大,GPU 内存消耗就越高。当 GPU 内存不足时,tensorflow 将会抛出以下类型的信息:
2021-03-29 15:45:04.185417: E tensorflow/stream_executor/cuda/cuda_driver.cc:825] failed to alloc 8589934592 bytes on host: CUDA_ERROR_OUT_OF_MEMORY: out of memory
2021-03-29 15:45:04.229570: E tensorflow/stream_executor/cuda/cuda_driver.cc:825] failed to alloc 7730940928 bytes on host: CUDA_ERROR_OUT_OF_MEMORY: out of memory
2021-03-29 15:45:10.776120: E tensorflow/stream_executor/cuda/cuda_driver.cc:825] failed to alloc 17179869184 bytes on host: CUDA_ERROR_OUT_OF_MEMORY: out of memory
...
解决方案是减小批次大小。当我收到此消息时,我希望能够捕获此异常,这样我就可以向视图发送消息,甚至自动减小批次大小以自动化学习行为。在我的情况下,内存不足来自数据集的加载:
try:
features, labels = iter(input_dataset).next()
except:
print("this is my exception")
raise
然而,CUDA错误OOM似乎无法像这样被捕获。实际上,我认为该错误已经在tf.Dataset类的下一个函数中被捕获。我看到的似乎实际上是由OOM错误捕获生成的日志。我不知道如何检测此日志以便对OOM事件作出反应。
except tf.errors.ResourceExhaustedError as e:
- Bijay Regmi