AI
PyTorch
What is PyTorch?
PyTorch is an open-source machine learning library developed by Facebook's AI Research lab. It is based on the Torch library and is used for applications such as natural language processing. PyTorch provides two high-level features:
- Tensor computation (like NumPy) with strong GPU acceleration
- Deep neural networks built on a tape-based autograd system
PyTorch is widely used in the research community and is known for its flexibility and ease of use.
Installation
Check out the latest version of PyTorch installation instructions on the official website.
It's recommended to create a virtual environment before installing PyTorch to avoid conflicts with other Python packages.
Linux
| Platform | CUDA Version | Command |
|---|---|---|
| Linux | None | pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu |
| Linux | CUDA 13.0 | pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu130 |
| Linux | CUDA 12.8 | pip3 install torch torchvision torchaudio |
| Linux | CUDA 12.6 | pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126 |
| MacOS | None | pip3 install torch torchvision torchaudio |
| Windows | None | pip3 install torch torchvision torchaudio |
| Windows | CUDA 12.6 | pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126 |
Verify Installation
To make sure PyTorch is installed correctly, you can run the following Python code:
import torch
x = torch.rand(5, 3)
print(x)
Output:
tensor([[0.3380, 0.3845, 0.3217],
[0.8337, 0.9050, 0.2650],
[0.2979, 0.7141, 0.9069],
[0.1449, 0.1132, 0.1375],
[0.4675, 0.3947, 0.1426]])
NVIDIA CUDA Toolkit
Learn more about CUDA Toolkit.
Checks in PyTorch to validate whether a GPU (CUDA-capable NVIDIA device) is available and usable.
import torch
gpu_count = torch.cuda.device_count()
print(f"Number of GPUs: {gpu_count}")
for i in range(gpu_count):
print(f"GPU {i}: {torch.cuda.get_device_name(i)}")