# How to Configure GPU Acceleration for PyTorch and TensorFlow in the ai4beg Conda Environment

> Learn to configure GPU acceleration for PyTorch and TensorFlow in the ai4beg conda environment. Install drivers, add cudatoolkit, and verify GPU availability for faster AI development.

- Repository: [Microsoft/AI-For-Beginners](https://github.com/microsoft/AI-For-Beginners)
- Tags: how-to-guide
- Published: 2026-08-27

---

**To enable GPU acceleration in the ai4beg environment, install a CUDA-compatible NVIDIA driver (≥ 515 for CUDA 11.8), add the matching cudatoolkit version via Conda, and verify availability using `torch.cuda.is_available()` for PyTorch or `tf.config.list_physical_devices('GPU')` for TensorFlow.**

The microsoft/AI-For-Beginners repository provides the `ai4beg` conda environment defined in [`environment.yml`](https://github.com/microsoft/AI-For-Beginners/blob/main/environment.yml) to help beginners run machine learning code. While the base configuration includes PyTorch and TensorFlow packages, accessing NVIDIA GPU acceleration requires specific CUDA toolkit installation and driver compatibility steps beyond the default setup.

## Prerequisites for CUDA Compatibility

Before modifying the Conda environment, ensure your host machine meets the hardware and driver requirements.

- **NVIDIA GPU Driver**: Install a CUDA-compatible driver (e.g., NVIDIA driver ≥ 515 for CUDA 11.8).
- **Hardware**: An NVIDIA GPU with compute capability supported by the CUDA version you intend to install.

Without the correct driver, `torch.cuda.is_available()` will return `False` and TensorFlow will default to CPU execution even after installing the CUDA toolkit.

## Installing the CUDA Toolkit in the ai4beg Environment

The `ai4beg` environment defined in [`environment.yml`](https://github.com/microsoft/AI-For-Beginners/blob/main/environment.yml) already pulls in core PyTorch packages (`pytorch`, `torchvision`, `torchtext`, `torchdata`) and installs the full Python requirement set from [`requirements.txt`](https://github.com/microsoft/AI-For-Beginners/blob/main/requirements.txt). However, you must explicitly install a CUDA toolkit version that matches your driver.

Run the following command after activating the environment to install CUDA 11.8 support:

```bash
conda activate ai4beg
conda install -c pytorch -c nvidia \
    pytorch torchvision torchaudio \
    cudatoolkit=11.8

```

The `cudatoolkit` package provides the low-level CUDA libraries that PyTorch links against, allowing the framework to detect and utilize your GPU hardware.

## Configuring PyTorch for GPU Support

Once the CUDA toolkit is installed, verify PyTorch GPU access using the detection logic found in `lessons/3-NeuralNetworks/05-Frameworks/IntroPyTorch.ipynb`:

```python
import torch

if torch.cuda.is_available():
    print(f"✅ CUDA is available – {torch.cuda.device_count()} GPU(s) detected")
    print("GPU name:", torch.cuda.get_device_name(0))
else:
    print("❌ No CUDA-compatible GPU detected")

```

If this script prints your GPU name, PyTorch is correctly configured to use hardware acceleration for tensor operations and model training.

## Configuring TensorFlow for GPU Support

Starting with TensorFlow 2.10, GPU support is included in the standard `tensorflow` pip package listed in [`requirements.txt`](https://github.com/microsoft/AI-For-Beginners/blob/main/requirements.txt). No separate `tensorflow-gpu` installation is required.

However, TensorFlow defaults to allocating all available GPU memory upfront, which can cause out-of-memory errors when running multiple notebooks. Configure memory growth using the pattern found in `lessons/5-NLP/16-RNN/RNNTF.ipynb` (line 54) and `lessons/4-ComputerVision/08-TransferLearning/TransferLearningTF.ipynb` (line 632):

```python
import tensorflow as tf

gpus = tf.config.list_physical_devices('GPU')
if gpus:
    try:
        for gpu in gpus:
            tf.config.experimental.set_memory_growth(gpu, True)
        print("✅ Memory growth enabled for TensorFlow GPUs")
    except RuntimeError as e:
        print(e)

```

This configuration allows TensorFlow to allocate GPU memory incrementally rather than reserving the entire device memory at startup.

## Complete Environment Setup Commands

To create a fresh `ai4beg` environment with GPU support from scratch, execute the following sequence:

```bash

# 1. Remove any previous environment (optional)

conda env remove -n ai4beg

# 2. Create the base environment with Python 3.11

conda create -n ai4beg python=3.11 -y
conda activate ai4beg

# 3. Install core packages from the repository's environment file

conda env update -f https://raw.githubusercontent.com/microsoft/AI-For-Beginners/main/environment.yml

# 4. Add the CUDA toolkit matching your driver (example: 11.8)

conda install -c pytorch -c nvidia cudatoolkit=11.8 -y

# 5. Install TensorFlow with GPU support included

pip install tensorflow==2.17.0

```

This workflow ensures all dependencies from both [`environment.yml`](https://github.com/microsoft/AI-For-Beginners/blob/main/environment.yml) and [`requirements.txt`](https://github.com/microsoft/AI-For-Beginners/blob/main/requirements.txt) are present while adding the necessary CUDA libraries for GPU acceleration.

## Verifying GPU Availability

After completing the installation, run this combined verification script inside your activated `ai4beg` environment:

```python

# PyTorch verification

import torch
print("PyTorch CUDA available:", torch.cuda.is_available())
if torch.cuda.is_available():
    print("PyTorch GPU:", torch.cuda.get_device_name(0))

# TensorFlow verification

import tensorflow as tf
print("TensorFlow GPUs detected:", tf.config.list_physical_devices('GPU'))

```

Both frameworks should list at least one GPU device if the setup is correct.

## Summary

- The `ai4beg` environment installs PyTorch and TensorFlow via [`environment.yml`](https://github.com/microsoft/AI-For-Beginners/blob/main/environment.yml) and [`requirements.txt`](https://github.com/microsoft/AI-For-Beginners/blob/main/requirements.txt), but requires manual CUDA toolkit installation for GPU access.
- Install `cudatoolkit=11.8` (or match your driver version) from the `pytorch` and `nvidia` Conda channels to enable PyTorch GPU support.
- TensorFlow ≥ 2.10 includes GPU capabilities automatically; configure `tf.config.experimental.set_memory_growth` to prevent memory allocation issues.
- Verify configuration using `torch.cuda.is_available()` and `tf.config.list_physical_devices('GPU')` as implemented in the course notebooks.

## Frequently Asked Questions

### Do I need to install a separate tensorflow-gpu package?

No. According to the source requirements in [`requirements.txt`](https://github.com/microsoft/AI-For-Beginners/blob/main/requirements.txt), TensorFlow ≥ 2.10 includes GPU support by default. Simply `pip install tensorflow` and the framework will detect CUDA libraries automatically. Separate `tensorflow-gpu` packages are deprecated after TensorFlow 2.0.

### What NVIDIA driver version is required for the ai4beg environment?

For CUDA 11.8 (the version referenced in the setup commands), you need NVIDIA driver version ≥ 515. Always verify that your installed driver version meets the minimum requirements for whichever `cudatoolkit` version you install via Conda.

### Why does TensorFlow allocate all my GPU memory immediately?

By default, TensorFlow maps nearly all GPU memory into its address space at startup to ensure performance. This behavior is documented in the RNN and Transfer Learning notebooks (`RNNTF.ipynb` and `TransferLearningTF.ipynb`). To enable dynamic memory growth, iterate over physical GPUs and call `tf.config.experimental.set_memory_growth(gpu, True)` before allocating tensors.

### How can I check if PyTorch is using my GPU?

Import `torch` and call `torch.cuda.is_available()`. If this returns `True`, PyTorch can access the GPU. For detailed information, use `torch.cuda.get_device_name(0)` to retrieve the GPU model name, as demonstrated in `lessons/3-NeuralNetworks/05-Frameworks/IntroPyTorch.ipynb`.