How to Configure TensorFlow and PyTorch in the Same Environment for AI Lessons

You can install TensorFlow and PyTorch side-by-side in a single Conda environment using the environment.yml file from the Microsoft AI-For-Beginners repository, allowing you to import either framework in any notebook without dependency conflicts.

The Microsoft AI-For-Beginners curriculum is designed to teach deep learning concepts using both major frameworks interchangeably. To configure TensorFlow and PyTorch in the same environment, the repository provides a unified Conda environment definition that installs both libraries in parallel, eliminating the need to switch between virtual environments during lessons.

Why Configure TensorFlow and PyTorch Together?

The curriculum intentionally covers both frameworks to give learners flexibility in implementation. By configuring TensorFlow and PyTorch in the same environment, you avoid the disk space overhead and activation confusion of maintaining separate installations. Both frameworks share common dependencies like NumPy and OpenCV, making a unified environment both efficient and practical for the learning workflow.

The Environment Configuration Files

The repository uses two key files to define the unified environment according to the source code in microsoft/AI-For-Beginners:

environment.yml

The environment.yml file at the repository root declares the combined Conda environment. It pulls the Conda-packaged PyTorch suite from the pytorch channel on lines 19-22, including pytorch, torchvision, torchtext, and torchdata. Line 16 specifies tensorflow==2.17.0 from the standard channels. Line 24 invokes pip to install additional requirements from requirements.txt.

requirements.txt

The requirements.txt file lists Python-only libraries used throughout the curriculum, including Keras and TensorBoard. This file is referenced within the pip section of environment.yml, ensuring all high-level APIs are available immediately after environment creation.

Step-by-Step Setup Instructions

Create the unified environment by running these commands from the repository root:

conda env create --name ai4beg --file environment.yml
conda activate ai4beg

This single activation step prepares your system for any lesson in the curriculum, whether it uses TensorFlow, PyTorch, or the Keras API.

Verifying Your Installation

After activation, validate that both frameworks are accessible in the same Python interpreter:


# Verify TensorFlow

import tensorflow as tf
print("TensorFlow:", tf.__version__)

# Verify PyTorch

import torch
print("PyTorch:", torch.__version__, "CUDA:", torch.cuda.is_available())

Both import statements should execute without ModuleNotFoundError or CUDA library conflicts.

Using Both Frameworks in Practice

The unified environment allows you to compare implementations directly in the same Jupyter notebook. Here is how you define equivalent neural network architectures in both frameworks:


# TensorFlow example – simple dense layer

import tensorflow as tf
tf_model = tf.keras.Sequential([
    tf.keras.layers.Dense(32, activation='relu', input_shape=(784,)),
    tf.keras.layers.Dense(10, activation='softmax')
])
tf_model.summary()

# PyTorch example – equivalent module

import torch.nn as nn
torch_model = nn.Sequential(
    nn.Linear(784, 32),
    nn.ReLU(),
    nn.Linear(32, 10),
    nn.Softmax(dim=1)
)
print(torch_model)

Summary

  • The environment.yml in microsoft/AI-For-Beginners installs TensorFlow 2.17.0 and PyTorch simultaneously via Conda channels.
  • The requirements.txt handles additional Python dependencies like Keras and TensorBoard through the pip section on line 24.
  • A single conda activate ai4beg prepares your system for all lessons regardless of which framework the notebook targets.
  • Both frameworks share the same Python interpreter and system libraries, reducing disk usage and configuration complexity.

Frequently Asked Questions

Can TensorFlow and PyTorch coexist in the same Python environment without conflicts?

Yes. The environment.yml file specifies compatible versions of both frameworks and their shared dependencies. Because the repository uses Conda's dependency solver, it resolves potential conflicts between CUDA libraries and NumPy versions automatically, allowing both import tensorflow and import torch to work in the same session.

What specific versions of TensorFlow and PyTorch are specified?

The environment installs tensorflow==2.17.0 on line 16 of environment.yml, while PyTorch and its ecosystem packages (torchvision, torchtext, torchdata) come from the pytorch channel on lines 19-22. These versions are tested for compatibility within the AI-For-Beginners curriculum.

Do I need to create separate Conda environments for TensorFlow and PyTorch lessons?

No. The curriculum is explicitly designed to use a single environment named ai4beg as defined in environment.yml. This design eliminates the need to switch environments when moving between TensorFlow and PyTorch lessons, streamlining the learning workflow.

Where can I find detailed troubleshooting for the setup process?

The lessons/0-course-setup/setup.md file contains detailed instructions for creating the unified environment and troubleshooting common issues across Windows, macOS, and Linux systems. This file walks new users through the exact steps to configure TensorFlow and PyTorch together.

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:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →