Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

GPU TensorFlow Training: Step-by-Step Setup

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Using a GPU with TensorFlow can cut training time from hours to minutes for many models. This guide walks you through the setup: checking your hardware, installing the right drivers and CUDA, installing TensorFlow with GPU support, and verifying it works. You'll need an NVIDIA GPU (TensorFlow does not support AMD or Intel GPUs officially), a 64-bit Linux or Windows system, and administrator access to install drivers.

What You'll Need

  • An NVIDIA GPU with a compute capability of 3.5 or higher (most GTX 9xx and newer, all RTX cards)
  • 64-bit Ubuntu 16.04 or newer, or Windows 10/11
  • NVIDIA GPU driver (version 450.x or newer for CUDA 11, 525.x or newer for CUDA 12)
  • CUDA Toolkit (version 11.8 or 12.x depending on your TensorFlow version)
  • cuDNN SDK (version 8.6 or 8.9, matching CUDA)
  • Python 3.8–3.12 (check TensorFlow's compatibility)
  • pip package manager

Step-by-Step

  1. Check your GPU. Open a terminal (Linux) or Command Prompt (Windows). Run nvidia-smi if you have a driver installed. If not, install the latest NVIDIA driver from the official site. The output shows your GPU model and driver version. If you see "NVIDIA-SMI has failed", install the driver first.
  2. Install Python and pip. If you don't have Python, download it from python.org. Ensure pip is available: run python -m pip --version. On Linux, you may need to install python3-pip via apt.
  3. Install CUDA Toolkit. Go to NVIDIA's CUDA download page and select your OS. Choose the version that matches your TensorFlow. For TensorFlow 2.10–2.15, CUDA 11.8 is common; for 2.16+, CUDA 12.x. Download and run the installer. On Linux, follow the instructions to add the repository and install cuda-toolkit-11-8 (or similar). After install, add the CUDA bin and lib paths to your PATH and LD_LIBRARY_PATH environment variables.
  4. Install cuDNN. Download cuDNN from NVIDIA (requires free account). Choose the version matching your CUDA. Extract the files and copy them into the CUDA directory. On Linux, copy libcudnn.so* to /usr/local/cuda/lib64 and cudnn.h to /usr/local/cuda/include. On Windows, copy the DLLs to C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\bin.
  5. Create a virtual environment (recommended). Run python -m venv tf-gpu and activate it: source tf-gpu/bin/activate (Linux) or tf-gpu\Scripts\activate (Windows). This isolates your TensorFlow installation.
  6. Install TensorFlow with GPU support. Run pip install tensorflow (for the latest stable). For older versions, pip install tensorflow==2.15.0. The pip package includes GPU support by default, but it will only use the GPU if CUDA and cuDNN are present.
  7. Verify the GPU is detected. In Python, run:
    import tensorflow as tf
    print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))
    If it prints 1 or more, you're set. If it prints 0, go to Troubleshooting.
  8. Run a quick test. Train a small model, e.g., a simple dense network on random data, and watch the GPU usage using nvidia-smi in another terminal. You should see a Python process using memory and GPU-Util.

Common Mistakes

  • Installing TensorFlow without CUDA/cuDNN – the pip package will still install, but it will silently fall back to CPU.
  • Mismatched CUDA and cuDNN versions – TensorFlow expects exact minor versions (e.g., CUDA 11.8, cuDNN 8.6). Using 11.7 or 8.7 may cause failures.
  • Forgetting to set PATH on Windows – the CUDA bin folder must be in your system PATH before starting Python.
  • Using a 32-bit Python – TensorFlow GPU only works with 64-bit Python.
  • Not restarting the terminal after installing drivers – environment variables may not refresh.

Tips

  • Use tf.config.set_visible_devices to limit which GPU TensorFlow uses if you have multiple.
  • For large models, increase batch size to fully utilize GPU memory – but watch for OOM errors.
  • Set tf.keras.mixed_precision.set_global_policy('mixed_float16') to speed up training on modern GPUs (Volta and later) with minimal accuracy loss.
  • Check TensorFlow's official GPU support matrix for your exact version – it lists tested CUDA and cuDNN versions.
  • If you use Docker, the tensorflow/tensorflow:latest-gpu image bundles CUDA and cuDNN, saving setup time.

Troubleshooting

  • TensorFlow doesn't see the GPU. Run python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))". If empty, check that nvidia-smi works. If not, reinstall the driver. Then verify CUDA is in PATH: nvcc --version should show the version. If not, add the CUDA bin to PATH. Also check that cuDNN files are in the right place – on Linux, run ldconfig -p | grep cudnn.
  • Error: "Could not load dynamic library 'libcudnn.so.8'" – This means cuDNN is missing or the version doesn't match. Reinstall cuDNN, ensuring the library file name matches the expected version (e.g., libcudnn.so.8 for CUDA 11).
  • Out of memory (OOM) during training. Reduce batch size, or use tf.keras.backend.clear_session() between experiments. Also check that other processes aren't using the GPU (run nvidia-smi to see).
  • Training is slow even with GPU. Ensure you're actually using the GPU – check task manager or nvidia-smi. If GPU utilization is low, your batch size might be too small, or the model is too small to benefit. Also, make sure you're not running on CPU because of a fallback.
  • Windows-specific: "CUDA_ERROR_NO_DEVICE" – This often happens when the GPU driver is too old. Update the driver. Also, ensure your GPU is not disabled in Device Manager.

Frequently Asked Questions

Do I need to install CUDA separately if I use pip?

Yes. The pip package does not bundle CUDA or cuDNN; they must be installed system-wide. However, using the TensorFlow Docker image avoids this entirely.

Can I use an AMD GPU with TensorFlow?

Officially, no. TensorFlow only supports NVIDIA GPUs with CUDA. There are community efforts like ROCm, but they are not stable for most users.

Does TensorFlow 2.x automatically use the GPU?

Yes, if CUDA and cuDNN are installed and the GPU is detected, TensorFlow will use it by default for all operations that support GPU execution.

How do I know which CUDA version to install?

Check the TensorFlow install page for your specific version. For TensorFlow 2.15, use CUDA 11.8; for 2.16+, use CUDA 12.2. The exact match matters.

Will using a GPU improve training for all models?

Mostly, but small models or very small datasets may not see a speedup because of the overhead of transferring data to the GPU. Larger models and bigger batches benefit most.

Can I use a GPU on a laptop?

Yes, if it has an NVIDIA GPU with compute capability 3.5 or higher. However, laptop GPUs may throttle due to heat, so performance may be lower than a desktop equivalent.