Before you can start building deep learning models, you need to set up your development environment with PyTorch. This involves installing the core library and configuring your workspace. As this course assumes familiarity with Python, we'll focus on integrating PyTorch into a standard Python setup.
PyTorch can be installed using two primary package managers: Conda (part of the Anaconda or Miniconda distributions) and Pip (Python's default package installer).
venv
. You might need to handle the installation of NVIDIA drivers and the CUDA toolkit separately if you require GPU support.For reproducibility and avoiding conflicts with other projects, it's highly recommended to install PyTorch within a dedicated virtual environment, regardless of whether you choose Conda or Pip.
Installation decision flowchart summarizing the choice between Conda and Pip, considering GPU requirements, and ending with verification.
The exact installation command depends on your operating system (Linux, macOS, Windows), package manager (Conda, Pip), and whether you want CPU-only or GPU (CUDA-enabled) support.
The best practice is to always consult the official PyTorch website (pytorch.org) for the most up-to-date installation command generator. Select your preferences, and it will provide the precise command to run.
1. Using Conda
First, create and activate a new Conda environment (replace pytorch_env
with your preferred name and choose a suitable Python version):
# Create the environment
conda create -n pytorch_env python=3.9 # Or 3.8, 3.10, etc.
# Activate the environment
conda activate pytorch_env
Now, go to the PyTorch website, select your OS, Conda, Python version, and desired CUDA version (or CPU). Copy the generated command. It will look something like this (example only, get the current one from the site!):
# Example Conda command for CUDA 11.8
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
# Example Conda command for CPU only
conda install pytorch torchvision torchaudio cpuonly -c pytorch
Run the command provided by the website in your activated Conda environment.
2. Using Pip
First, create and activate a new virtual environment using venv
:
# Create the environment (in your project directory)
python -m venv pytorch_env
# Activate the environment
# Linux/macOS:
source pytorch_env/bin/activate
# Windows (Command Prompt):
.\pytorch_env\Scripts\activate
# Windows (PowerShell):
.\pytorch_env\Scripts\Activate.ps1
Next, go to the PyTorch website, select your OS, Pip, Python version, and desired CUDA version (or CPU). Copy the generated command. It will often involve specifying an index URL and potentially the CUDA version (example only, get the current one from the site!):
# Example Pip command for CUDA 11.8
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
# Example Pip command for CPU only
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
Run the command provided by the website in your activated virtual environment. Ensure your pip
is up-to-date (pip install --upgrade pip
).
Deep learning computations, particularly matrix multiplications, are significantly faster on NVIDIA GPUs using the CUDA parallel computing platform.
If you don't have a compatible GPU or don't need GPU acceleration initially, simply choose the "CPU" option during installation. You can perform all operations on the CPU, albeit potentially slower for large models.
Once the installation completes, you can verify it by opening a Python interpreter (within your activated virtual environment) and running:
import torch
# Check PyTorch version
print(f"PyTorch Version: {torch.__version__}")
# Check if CUDA (GPU support) is available
cuda_available = torch.cuda.is_available()
print(f"CUDA Available: {cuda_available}")
if cuda_available:
# Get the number of GPUs available
print(f"Number of GPUs: {torch.cuda.device_count()}")
# Get the name of the current GPU
print(f"Current GPU Name: {torch.cuda.get_device_name(torch.cuda.current_device())}")
else:
print("PyTorch is using CPU.")
# Create a simple tensor
x = torch.rand(2, 3)
print("Successfully created a tensor:")
print(x)
If these commands run without error and report the correct version and CUDA status (True if you installed the GPU version and have hardware/drivers set up, False otherwise), your installation is successful.
pytorch_env
(or whatever you named it) before working on your PyTorch projects or running scripts.conda install <package>
or pip install <package>
:
numpy
: For numerical operations and interfacing with other libraries.matplotlib
or seaborn
: For plotting and visualization.scikit-learn
: For traditional ML tasks and utilities.jupyterlab
or notebook
: For interactive development.With PyTorch installed and your environment configured, you are now ready to explore its core component: the Tensor.
© 2025 ApX Machine Learning