Failed to get convolution algorithm. This is probably because cuDNN failed..(TensorFlow和keras显存不足报错)

在跑深度学习程序时,用到TensorFlow或者keras时候,经常会报一个错误:

tensorflow.python.framework.errors_impl.

UnknownError: Failed to get convolution algorithm.

This is probably because cuDNN failed to initialize,
so try looking to see if a warning log message was printed above.

我查了很多资料,发现主要原因是显卡加速的显存不够用。

[En]

I looked up a lot of information and found that the main reason is that the video memory accelerated by the video card is not enough.

报错分析

出现这种情况的原因是,tensorflow初始化时会默认占满全部显卡和全部剩余显存,这会导致显卡除了训练之外无法进行其他工作,包括显示,一定会报错。TensorFlow的官网这这样解释的:

By default, TensorFlow maps nearly all of the GPU memory of all GPUs (subject to CUDA_VISIBLE_DEVICES) visible to the process.

This is done to more efficiently use the relatively precious GPU memory resources on the devices by reducing memory fragmentation.

翻译:默认情况下,TensorFlow映射所有GPU的几乎所有GPU内存(仅限CUDA可视设备)对流程可见。这样做是为了通过减少内存碎片来更有效地使用设备上相对宝贵的GPU内存资源。

说白了,我们默认使用所有显存,是为了防止碎片化,更好地利用内存。然而,在这种情况下,图形卡将无法执行计算机的其他正常功能,并将报告错误。因此,为了防止节目的行为,我们应该采取相应的措施。

[En]

To put it bluntly, we use all video memory by default in order to prevent fragmentation and to make better use of memory. However, in this way, the graphics card will not be able to perform other normal functions of the computer and will report an error. Therefore, in order to prevent the behavior of the program, we should take corresponding measures.

解决方法:

对于TensorFlow程序

1.直接限制显存,在报错的程序开头加上下面这几句程序:

from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession
config = ConfigProto()
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)

在这种方案下,显存占用会随着epoch的增长而增长,也就是运行后面的eopch时,会去申请新的显存,前面已经完成的epoch所占用的显存并不会释放,原因也是为了防止碎片化。

2.直接限制显存的百分比,在报错程序的开头加上这几句程序:

from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession
config = ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.8
session = InteractiveSession(config=config)

这种方法就比较给力了,告诉tensorflow,我这块显卡给你80%的显存,其余的你给我放着不动。 需要注意的是,虽然代码或配置层面设置了对显存占用百分比阈值,但在实际运行中如果达到了这个阈值,程序有需要的话还是会突破这个阈值。换而言之如果跑在一个大数据集上还是会用到更多的显存。以上的显存限制仅仅为了在跑小数据集时避免对显存的浪费而已。如果还是报错的话,以我多次实验的经验可以对0.8进行调整,比如降低到0.7、0.6或者0.5,这样的话,即使显存使用占比超过设定的这个阈值,显卡还是能正常工作,不会报错。

对于Keras程序

由于keras是使用的tensorflow后端,所以需要加上额外的语句。为了限制程序百分百调用显存,在程序开头的地方加上以下几句程序:

import tensorflow as tf
import keras
config = tf.compat.v1.ConfigProto(allow_soft_placement=True)
config.gpu_options.per_process_gpu_memory_fraction = 0.8
tf.compat.v1.keras.backend.set_session(tf.compat.v1.Session(config=config))

和上面一样,虽然代码或配置层面设置了对显存占用百分比阈值,但在实际运行中如果达到了这个阈值,程序有需要的话还是会突破这个阈值。所以0.8这个数值可以调整,最好向下调整数值大小。

没办法的办法:

如果以上方法都不行,可以尝试减小程序中的batch_size的大小了,batch_size的大小一般是2的n次方,设置的时候也需要按照这个规律进行设置。

实验验证:

经过本人的实验,以上的三段程序都可以Keras程序中使用,TensorFlow程序最好只使用前两个程序段,读者可以依次尝试,直到选择一个可以正常运行的程序段。

参考文献

Original: https://blog.csdn.net/nohopenolove/article/details/121531633
Author: C++&&python
Title: Failed to get convolution algorithm. This is probably because cuDNN failed..(TensorFlow和keras显存不足报错)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/511316/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球