Solve – Failed to get convolution algorithm. This is probably because cuDNN failed to initialize in Python

Solve – Failed to get convolution algorithm. This is probably because cuDNN failed to initialize in Python is an error which occurs when working with Tensorflow/ Keras and it happens for various reasons which have various fixes.

In this article I am going to solve the error while I do my best to explain why the error is occurring, I will also introduce some solutions which I tried and have worked for me and other fellow devs.

Explaining the Error : Failed to get convolution algorithm. This is probably because cuDNN failed to initialize in Python

The error can happen to anyone running Keras or Tensorflow, the error is very diverse it has many reasons to occur and the scenarios are many. So, reproducing the error is hard because of what I have just explained. But, the error messages are usually very similar, bellow is the standard error message for our error.

                                                                       #
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.
                                                                       #

To solve the problem above, I have multiple solutions which have worked for me, bellow is a detailed explanation of all of them.

Solution 1 : Restart the process and more

First of all, this is an error that has many causes as we have said before. In our first solution/situation we assume that we have a memory problem, this is very common for tensorflow users …

The error will happen when your RAM runs out for your graphics card. With Nvidia, please run nvidia-smi to check your usage of the Gpu RAM.

The solution is to frre up the Ram by restarting the process.

To prevent the problem in the first place, you can limit the amount of Ram tensorflow can use, you can limit it to 0.7 for example aka 70 percent. To do that, add this to your code.

                                                                       #
import keras
import tensorflow as tflow
config = tflow.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.7 
keras.backend.tensorflow_backend.set_session(tflow.Session(config=config))
                                                                       #

Solution 2 : Fix compatibility issues

If that does not work, please try uninstalling TensorFlow and Cuda.

The error could be caused by compatibility issues between NVIDIA drivers, TensorFlow and Cuda.

Reinstalling them can solve the problem, make sure you use the installation guide available in the TensorFlow website, do not use any other guide, website or Youtube tutorial, most likely that is why you have the error in the first place.

Solution 3 : An Amazing Fix

Another solution is to run the following command

                                                                       #
os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true'
                                                                       #

This is an amazing fix, since this command forces Tensorflow to limit its memory usage to the absolute minimum and then scale if needed, this will help with TensorFlow overwhelming your device’s memory.

If the solutions above helped you, consider supporting us on Kofi, any help is appreciated.

Summing-up

Here we are at the end of the road, at the end of this article, if you solved this error, congratulations. This was a confusing error for me the first time I encountered it.

Make sure to keep coding and keep learning, Python is my favourite programming language, it just needs some patience, cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/