This section mainly teaches how to use GPU with TensorFlow. There are many installation tutorials available online, but if you followed the official tutorial step by step and still didn’t succeed, but ended up with the following setup:
You find that when you execute the following code, it prints 0. If so, this article is what you need.
import tensorflow as tfThe main reasons for encountering this issue are usually:
Since the version may change, this article will focus on teaching you how to install CUDA in line with the latest updates. If you go to the TensorFlow version confirmation page now, you’ll see the recommended version is CUDA 11.2, but since 11.2 only supports Windows 10, I downloaded the latest version, and it works fine here.
Please go to the official CUDA Toolkit installation
![]()
After installation, open GeForce Experience and select the Studio Driver , ensuring that your GPU can function properly. Then install the NVIDIA Studio Driver .
According to the official instructions, it is required to set the environment variables for CUDA and cuDNN installation directories to the system environment variables. This ensures TensorFlow can correctly utilize the GPU. However, I haven’t set up cuDNN-related environment variables here, which we’ll do through conda installation later.
The version v11.0 below should be adjusted according to your installed version. Here, v11.0 is used as an example.
SET PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\bin;%PATH%Open System Properties -> Environment Variables -> System variables -> Path -> Edit -> New -> Ensure the following content is present:
Usually, if CUDA is properly installed, it will automatically set up for you. If you notice it’s not done, remember to set it up manually!
Next, open your Anaconda Prompt. It’s* recommended not to install too many packages in the base environment*. Therefore, it’s suggested to create a new environment, and install cuDNN. When should you install packages in the base environment? Only when you’re sure that this package will be used in every project.
According to the official documentation, you can see that the latest version of TensorFlow that supports GPU on Windows is 3.10 (Always refer to the official English documentation, as sometimes the Chinese version isn’t updated):
Let’s create a new environment first, setting the Python version to 3.10, and then install the necessary packages.
conda create -n py310 python=3.10After creating and activating the newly built environment, proceed to install cuDNN. We’ll use conda to install to ensure version consistency. Also, make sure to install according to the (https://www.tensorflow.org/install/source_windows#gpu, otherwise, the GPU won’t be usable. You can see that cuDNN 8.1 is recommended here.
So, in the py310 environment, install cuDNN version 8.1:
conda install -c conda-forge cudatoolkit=11.2 cudnn=8.1.0If you see the following output, it means it’s successful:
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.