How to Run Jupyter Notebooks with PyTorch and TensorFlow in AI for Beginners
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 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 file that declaratively installs PyTorch, TensorFlow, Keras, OpenCV, and Jupyter utilities. This ensures version compatibility across the 24-lesson curriculum.
To build the environment:
conda env create -f environment.yml
The environment name defaults to ai4beg as specified in the YAML configuration. Once created, activate it:
conda activate ai4beg
According to the source code in 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:
# 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.ipynborIntroKeras.ipynb
Open either file and execute cells sequentially. The notebooks include import statements such as:
import torch
import torchvision
or
import tensorflow as tf
from tensorflow import keras
These imports resolve correctly because the 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.ipynbandConvNetsTF.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:
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, while excluding localized content.
Summary
- The AI-For-Beginners repository uses a single Conda environment defined in
environment.ymlto manage PyTorch, TensorFlow, and Jupyter dependencies. - Activate the environment with
conda activate ai4begbefore launching Jupyter to ensure kernel consistency. - Launch via
jupyter notebookorjupyter labto access the browser-based interface. - Each lesson provides parallel PyTorch and TensorFlow notebooks (e.g.,
IntroPyTorch.ipynbvs.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 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 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.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →