python - 如何引发 tensorflow 内存不足错误的异常?

标签 python python-3.x tensorflow exception

我正在循环中使用 sess.run() 运行多个 tensorflow 推理,并且某些推理对于我的 GPU 来说过于繁重。

我收到如下错误:

2019-05-23 15:37:49.582272: E tensorflow/core/common_runtime/executor.cc:623] 
Executor failed to create kernel. Resource exhausted: OOM when allocating tensor of shape [306] and type float

我希望能够捕获这些特定的 OutOfMemory 错误,但不能捕获其他错误(这可能是由于错误的输入格式或损坏的图表所致。)

显然,结构类似于:

try:
   sess.run(node_output, feed_dict={node_input : value_input})
except:
    do_outOfMemory_specific_stuff()

不起作用,因为其他类型的错误将导致调用 do_outOfMemory_specific_stuff 函数。

知道如何捕获这些 OutOfMemory 错误吗?

最佳答案

您应该能够通过以下方式捕获它:

...
except tf.errors.ResourceExhaustedError as e:
    ...

根据this documentation .

关于python - 如何引发 tensorflow 内存不足错误的异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56276627/

相关文章:

python - 在 Python 中使用正则表达式删除标点符号时出现错误转义错误

python - 从人类时间输入中解析时区

python - 在 Python 中将 HTML 子标签更改为同级标签

python - 防止字符串被打印python

python - 对 pandas 数据框应用跨行滚动中位数

regex - 获取一个xml文件中的多个值

python - 减少 Keras.models.model 内存消耗

python - tensorflow 中的线性模型

tensorflow - 由于 ImportError : cannot import name 'keras_export' from 'tensorflow.python.util.tf_export' 无法运行 anpose

Python:循环条件的一行