Ensure Correct TensorFlow Version
- Verify that you are using a version of TensorFlow that supports GPU. For example, you need TensorFlow version > 2.x for most modern applications.
- Use the command below to install or ensure you have the correct version:
pip install tensorflow-gpu
Install NVIDIA Drivers
- Ensure that you have the appropriate NVIDIA drivers installed for your GPU that corresponds with the CUDA version.
- Visit the NVIDIA website to download and install the most recent and compatible drivers.
Install CUDA Toolkit
- Download and install the CUDA toolkit from the NVIDIA Developer website. Ensure that its version matches the requirements for the version of TensorFlow you are using.
- Set the environment path for CUDA by adding CUDA's `bin` directory to your PATH environment variable:
export PATH=/usr/local/cuda/bin${PATH:+:${PATH}}
Install cuDNN
- Download the NVIDIA cuDNN library from the NVIDIA Developer website and follow the installation instructions for placing the included files in the appropriate directories.
- Make sure that the cuDNN version matches the version of the CUDA Toolkit installed.
Verify GPU Installation
- Confirm the GPU installation using the NVIDIA System Management Interface:
nvidia-smi
- Check for any error messages and ensure the GPU is detected and available.
Modify TensorFlow Device Placement
- Specify explicitly to use the GPU device in your TensorFlow script if necessary:
import tensorflow as tf
# Set GPU as the device
with tf.device('/GPU:0'):
# Your model code here
pass
Check TensorFlow GPU Availability
- Use the following command to check if TensorFlow recognizes your GPU:
import tensorflow as tf
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
- Ensure that the output indicates a GPU is available. If not, check configurations and installations.
Set Environment Variables
- Sometimes it's necessary to set or modify environment variables for CUDA and cuDNN:
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
export CUDA_HOME=/usr/local/cuda
Upgrade TensorFlow and Dependencies
- If you continue experiencing issues, consider upgrading TensorFlow and its dependencies:
pip install --upgrade tensorflow-gpu
- This ensures you have the latest enhancements and bug fixes related to GPU support.