# How to Run Jupyter Notebooks with PyTorch and TensorFlow in AI for Beginners

> Easily run Jupyter notebooks with PyTorch and TensorFlow using the AI For Beginners unified Conda environment. Activate ai4beg and start coding immediately. Perfect for AI beginners.

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

---

**The AI-For-Beginners repository provides a unified Conda environment that includes both PyTorch and TensorFlow, allowing you to launch Jupyter and immediately execute any curriculum notebook by activating the `ai4beg` environment and running `jupyter notebook` or `jupyter lab`.**

The Microsoft **AI-For-Beginners** curriculum ships with dual-framework support, giving learners the flexibility to study neural networks and deep learning using either PyTorch or TensorFlow/Keras. By configuring a single Python environment from the provided [`environment.yml`](https://github.com/microsoft/AI-For-Beginners/blob/main/environment.yml) file, you can run Jupyter notebooks with PyTorch and TensorFlow in AI for Beginners without managing separate installations or dependency conflicts.

## Configure the Conda Environment

The repository root contains an [`environment.yml`](https://github.com/microsoft/AI-For-Beginners/blob/main/environment.yml) file that declaratively installs **PyTorch**, **TensorFlow**, **Keras**, **OpenCV**, and Jupyter utilities. This ensures version compatibility across the 24-lesson curriculum.

To build the environment:

```bash
conda env create -f environment.yml

```

The environment name defaults to `ai4beg` as specified in the YAML configuration. Once created, activate it:

```bash
conda activate ai4beg

```

According to the source code in [`lessons/0-course-setup/how-to-run.md`](https://github.com/microsoft/AI-For-Beginners/blob/main/lessons/0-course-setup/how-to-run.md), this activation step is required before launching Jupyter to ensure the kernel has access to `torch` and `tensorflow` packages.

## Launch Jupyter Notebook or JupyterLab

With the Conda environment active, start the Jupyter server using either the classic interface or JupyterLab:

```bash

# Classic notebook interface

jupyter notebook

# Or JupyterLab

jupyter lab

```

The command automatically binds to the active `ai4beg` Python interpreter, making PyTorch and TensorFlow available in every notebook kernel. The browser will open to the local server (typically `http://localhost:8888`), displaying the repository file tree.

## Navigate and Execute Framework-Specific Notebooks

Each lesson directory contains parallel implementations. For example, in `lessons/3-NeuralNetworks/05-Frameworks/`, you will find:

- **PyTorch version**: `IntroPyTorch.ipynb`
- **TensorFlow/Keras version**: `IntroKerasTF.ipynb` or `IntroKeras.ipynb`

Open either file and execute cells sequentially. The notebooks include import statements such as:

```python
import torch
import torchvision

```

or

```python
import tensorflow as tf
from tensorflow import keras

```

These imports resolve correctly because the [`environment.yml`](https://github.com/microsoft/AI-For-Beginners/blob/main/environment.yml) explicitly lists `torch`, `torchvision`, and `tensorflow` as dependencies.

### Computer Vision and NLP Examples

The dual-framework pattern continues throughout the curriculum:

- **Convolutional Networks**: `lessons/4-ComputerVision/07-ConvNets/ConvNetsPyTorch.ipynb` and `ConvNetsTF.ipynb`
- **Text Representation**: `lessons/5-NLP/13-TextRep/TextRepresentationTF.ipynb`

Each notebook is self-contained and includes dataset download commands, requiring no additional configuration beyond the initial Conda setup.

## Optimized Repository Cloning with Sparse Checkout

If you want to skip translation files and reduce download size, use Git's sparse-checkout feature before creating the environment:

```bash
git clone --filter=blob:none --sparse https://github.com/microsoft/AI-For-Beginners.git
cd AI-For-Beginners
git sparse-checkout set --no-cone '/*' '!translations' '!translated_images'

```

This command retrieves only the core curriculum files, including all `*.ipynb` notebooks and the [`environment.yml`](https://github.com/microsoft/AI-For-Beginners/blob/main/environment.yml), while excluding localized content.

## Summary

- The **AI-For-Beginners** repository uses a single Conda environment defined in [`environment.yml`](https://github.com/microsoft/AI-For-Beginners/blob/main/environment.yml) to manage PyTorch, TensorFlow, and Jupyter dependencies.
- Activate the environment with `conda activate ai4beg` before launching Jupyter to ensure kernel consistency.
- Launch via `jupyter notebook` or `jupyter lab` to access the browser-based interface.
- Each lesson provides parallel PyTorch and TensorFlow notebooks (e.g., `IntroPyTorch.ipynb` vs. `IntroKerasTF.ipynb`) located in framework-specific subdirectories.
- Use sparse-checkout to clone efficiently without translation bloat.

## Frequently Asked Questions

### Do I need separate virtual environments for PyTorch and TensorFlow?

No. The [`environment.yml`](https://github.com/microsoft/AI-For-Beginners/blob/main/environment.yml) file in the repository root installs both frameworks into a single Conda environment named `ai4beg`. This unified approach prevents version conflicts and simplifies the workflow, as confirmed by the dependency list in the source code.

### Can I run these notebooks in Google Colab instead of local Jupyter?

While the curriculum is optimized for local execution via the provided Conda environment, you can manually upload the `.ipynb` files to Google Colab. However, you must install the required packages (`torch`, `tensorflow`, `opencv-python`) via `!pip` commands at the start of the session, as the notebooks assume the pre-configured `ai4beg` kernel.

### How do I switch between PyTorch and TensorFlow notebooks?

Simply open the corresponding file in the Jupyter file browser. For example, in `lessons/3-NeuralNetworks/05-Frameworks/`, open `IntroPyTorch.ipynb` for PyTorch code or `IntroKerasTF.ipynb` for TensorFlow/Keras implementations. Both kernels run under the same active Conda environment.

### What if I encounter CUDA/GPU errors when importing TensorFlow or PyTorch?

The [`environment.yml`](https://github.com/microsoft/AI-For-Beginners/blob/main/environment.yml) installs CPU-compatible versions by default. If you require GPU acceleration, modify the environment file to specify CUDA-enabled builds of `pytorch` and `tensorflow` before running `conda env create`, or install the appropriate CUDA toolkit separately after environment creation.