PyTorch

An open-source deep learning framework based on dynamic computation graphs and automatic differentiation, and the most used in scientific research.

ActiveMediumv2.15.0Apache-2.0 AND Apache-2.0 WITH LLVM-exc…Deep Learning Frameworks

Official documentationTry the examples

Basic information

NamePyTorch
Full namePyTorch
CategoryDeep Learning Frameworks
DeveloperThe PyTorch Foundation (under the Linux Foundation), launched by Meta AI
Release year2016
Official websitepytorch.org
GitHub repositorypytorch/pytorch
Official documentationpytorch.org/docs
Package on PyPItorch
LicenseApache-2.0 AND Apache-2.0 WITH LLVM-exception AND BSD-2-Clause AND BSD-3-Clause AND BSL-1.0 AND ISC AND MIT AND Zlib (PyPI license_expression)
Project statusActive Had a release in the last 12 months
Latest version28 October 2026 (version 2.15.0)
Programming languagePython, C++, CUDA
Operating systemsNot specified
Required Python version>=3.11
Difficulty levelMedium

Data last checked: 21 November 2026 — source: PyPI JSON API (2026-11-21)

Overview

PyTorch provides tensors that run on CPUs and GPUs, automatic differentiation (Autograd) that builds the computation graph during execution, the torch.nn module for building layers, and optimizers and data loaders. Researchers write the model in plain Python, which makes it easy to trace and modify.

It supports distributed training (DistributedDataParallel and FSDP), compiling to faster code with torch.compile, and exporting to ONNX and ExecuTorch. A large ecosystem is built on it: TorchVision, timm, MONAI, Hugging Face Transformers and PyTorch Geometric.

The actual install command depends on the operating system and CUDA version; use the selector on the official installation page to get the right command for your machine.

When to choose it? Choose it for scientific research, new architectures and the largest ecosystem of open models.

Uses

Main uses

  • Building and training neural networks of every kind
  • Building and training neural networks of every kind
  • Research on new architectures
  • Research on new architectures
  • Fine-tuning pretrained models
  • Fine-tuning pretrained models
  • Moving models to production through TorchScript/ONNX/ExecuTorch
  • Moving models to production through TorchScript/ONNX/ExecuTorch

Scientific fields

  • Computer vision
  • Computer vision
  • Language processing
  • Language processing
  • Medical imaging
  • Medical imaging
  • Reinforcement Learning
  • Reinforcement Learning
  • Science
  • Science

Suitable project types

  • Medical image classification
  • Medical image classification
  • Tumour segmentation
  • Tumour segmentation
  • Language models
  • Language models
  • Research hybrid networks
  • Research hybrid networks

Research applications

  • Implementing architectures proposed in research papers and reproducing results
  • Implementing architectures proposed in research papers and reproducing results
  • Experiments with hybrid networks (CNN-Transformer, CapsNet) and their interpretation
  • Experiments with hybrid networks (CNN-Transformer, CapsNet) and their interpretation

Deep learning and building neural networksConvolutional networksImage classificationImage segmentationMedical image analysisLarge language modelsGPU accelerationDistributed computing and training

Technical features

A feature is shown as "Supported" only if it is documented in the library's official documentation; anything we have not verified is shown as "Not specified".

CPU supportSupported
GPU supportSupportedCUDA on NVIDIA, ROCm on AMD, and MPS on Apple devices according to the documentation.
CUDA compatibilitySupported
Distributed training or computingSupported
Automatic differentiation (Autodiff)Supported
Model deploymentSupportedtorch.export, ONNX and ExecuTorch.
Pre-trained modelsPartial or through an add-onThrough ecosystem libraries such as TorchVision, timm and the Hugging Face Hub.
Supported data typesfloat16/bfloat16/float32/float64, integers, booleans, complex numbers, sparse tensors
Integration with other librariesTorchVision, timm, MONAI, Hugging Face Transformers, PyTorch Geometric, Captum, ONNX Runtime
Flexibility (editorial estimate)Very high
Performance (editorial estimate)High, with torch.compile
Community (editorial estimate)The largest in academic research
Documentation quality (editorial estimate)Extensive official documentation and tutorials

Installation and running

Shell
pip install torch torchvision

The actual command depends on the operating system and the CUDA version; choose your settings on the official installation page: https://pytorch.org/get-started/locally/

Compatibility: Python >=3.11 — according to the package data on PyPI, checked on 21 November 2026

Compatibility: Python >=3.11 — according to the package data on PyPI, checked on 21 November 2026

Installing inside a virtual environment (venv or conda) is recommended. If you need compatibility with a specific CUDA version or operating system, refer to the official documentation, because this information changes with every release.

Code examples

Examples marked "Run and tested" were actually run, with their output, test date and library versions recorded; the others need a GPU or model downloads, so they were not run in the test environment.

BeginnerTensors and automatic differentiationRun and tested

Goal: Understanding how PyTorch computes derivatives automatically.

Python — example.py
import torch

x = torch.tensor(2.0, requires_grad=True)
y = x ** 2 + 3 * x
y.backward()
print("y =", y.item())
print("dy/dx =", x.grad.item())

a = torch.arange(6, dtype=torch.float32).reshape(2, 3)
print("Tensor shape:", tuple(a.shape), "| sum:", a.sum().item())
print("Available device:", "cuda" if torch.cuda.is_available() else "cpu")

Code explanation

With requires_grad=True PyTorch records operations in a computation graph, and after backward() the derivative is in .grad. Here y = x² + 3x, so the derivative is 2x + 3, which equals 7 at x=2.

Requirements

Shell
pip install torch

How to run

Save the code in a file example.py then run it:

Shell
python example.py

Output recorded during testing

y = 10.0
dy/dx = 7.0
Tensor shape: (2, 3) | sum: 15.0
Available device: cpu

This example was actually run and tested on 21 November 2026 on a CPU — torch 2.15.0, python 3.13.18

Example page
MediumA complete training loop for a multilayer networkRun and tested

Goal: Writing the training and evaluation loop by hand on synthetic data.

Python — example.py
import torch
from torch import nn

torch.manual_seed(0)
X = torch.rand(1200, 2) * 2 - 1
y = ((X ** 2).sum(1) < 0.5).long()
X_tr, y_tr, X_te, y_te = X[:1000], y[:1000], X[1000:], y[1000:]

model = nn.Sequential(nn.Linear(2, 32), nn.ReLU(), nn.Linear(32, 32), nn.ReLU(), nn.Linear(32, 2))
opt = torch.optim.Adam(model.parameters(), lr=0.01)
loss_fn = nn.CrossEntropyLoss()

for epoch in range(1, 201):
    model.train()
    loss = loss_fn(model(X_tr), y_tr)
    opt.zero_grad(); loss.backward(); opt.step()
    if epoch % 50 == 0:
        model.eval()
        with torch.no_grad():
            acc = (model(X_te).argmax(1) == y_te).float().mean().item()
        print(f"epoch {epoch:3d}  loss={loss.item():.4f}  test_acc={acc:.3f}")

Code explanation

We generate data with a non-linear rule (points inside a circle), build a network with two hidden layers, then repeat: forward pass, loss computation, zeroing gradients, backward, optimizer step. torch.no_grad() during evaluation saves memory.

Requirements

Shell
pip install torch

How to run

Save the code in a file example.py then run it:

Shell
python example.py

Output recorded during testing

epoch  50  loss=0.1008  test_acc=1.000
epoch 100  loss=0.0197  test_acc=0.995
epoch 150  loss=0.0111  test_acc=0.995
epoch 200  loss=0.0077  test_acc=0.995

This example was actually run and tested on 21 November 2026 on a CPU — torch 2.15.0, python 3.13.18

Example page
AdvancedA CNN with a custom Dataset and DataLoaderRun and tested

Goal: Building a data pipeline in the standard PyTorch style and training a convolutional network.

Python — example.py
import torch
from torch import nn
from torch.utils.data import DataLoader, Dataset

class Lines(Dataset):
    def __init__(self, n, seed):
        g = torch.Generator().manual_seed(seed)
        self.y = torch.randint(0, 2, (n,), generator=g)
        self.x = torch.rand(n, 1, 16, 16, generator=g) * 0.3
        pos = torch.randint(2, 14, (n,), generator=g)
        for i in range(n):
            if self.y[i] == 0: self.x[i, 0, pos[i], :] += 1   # horizontal
            else:              self.x[i, 0, :, pos[i]] += 1   # vertical
    def __len__(self): return len(self.y)
    def __getitem__(self, i): return self.x[i], self.y[i]

torch.manual_seed(0)
train = DataLoader(Lines(800, 1), batch_size=32, shuffle=True)
test = DataLoader(Lines(200, 2), batch_size=100)
net = nn.Sequential(nn.Conv2d(1, 8, 3, padding=1), nn.ReLU(), nn.MaxPool2d(2),
                    nn.Conv2d(8, 16, 3, padding=1), nn.ReLU(), nn.AdaptiveAvgPool2d(1),
                    nn.Flatten(), nn.Linear(16, 2))
opt = torch.optim.Adam(net.parameters(), 1e-2)
for epoch in range(3):
    for xb, yb in train:
        loss = nn.functional.cross_entropy(net(xb), yb)
        opt.zero_grad(); loss.backward(); opt.step()
    with torch.no_grad():
        acc = sum((net(xb).argmax(1) == yb).sum().item() for xb, yb in test) / 200
    print(f"epoch {epoch + 1}: loss={loss.item():.4f} test_acc={acc:.3f}")
print("Number of parameters:", sum(p.numel() for p in net.parameters()))

Code explanation

We define a Dataset that generates 16×16 images containing a horizontal or vertical line, and use a DataLoader for batching and shuffling. The network has two Conv2d layers with pooling, then a linear layer. The same structure is used with real images by changing __getitem__ only.

Requirements

Shell
pip install torch

How to run

Save the code in a file example.py then run it:

Shell
python example.py

Output recorded during testing

epoch 1: loss=0.3398 test_acc=1.000
epoch 2: loss=0.0026 test_acc=1.000
epoch 3: loss=0.0005 test_acc=1.000
Number of parameters: 1282

This example was actually run and tested on 21 November 2026 on a CPU — torch 2.15.0, python 3.13.18

Example page
ResearchA capsule layer with dynamic routingRun and tested

Goal: Implementing the squash function and the routing-by-agreement algorithm between capsules as in the paper by Sabour et al. (2017).

Python — example.py
import torch
from torch import nn

def squash(s, dim=-1, eps=1e-8):
    n2 = (s ** 2).sum(dim, keepdim=True)
    return n2 / (1 + n2) * s / torch.sqrt(n2 + eps)

class CapsuleLayer(nn.Module):
    def __init__(self, in_caps, in_dim, out_caps, out_dim, iters=3):
        super().__init__()
        self.W = nn.Parameter(0.01 * torch.randn(1, in_caps, out_caps, out_dim, in_dim))
        self.iters = iters
    def forward(self, u):                              # u: (B, in_caps, in_dim)
        u_hat = (self.W @ u[:, :, None, :, None]).squeeze(-1)   # (B, in, out, out_dim)
        b = torch.zeros(u_hat.shape[:3], device=u.device)
        for r in range(self.iters):
            c = b.softmax(dim=2)
            v = squash((c[..., None] * u_hat).sum(1))  # (B, out, out_dim)
            if r < self.iters - 1:
                b = b + (u_hat * v[:, None]).sum(-1)
        return v

torch.manual_seed(0)
layer = CapsuleLayer(in_caps=32, in_dim=8, out_caps=10, out_dim=16)
v = layer(squash(torch.randn(4, 32, 8)))
print("Output shape:", tuple(v.shape))
print("Capsule lengths in [0,1):", bool(((v.norm(dim=-1) >= 0) & (v.norm(dim=-1) < 1)).all()))
print("Probabilities of the first sample:", v[0].norm(dim=-1).round(decimals=3).tolist())

Code explanation

Each capsule is a vector whose length is the probability that the entity exists. The squash function compresses the length into the range [0, 1) while keeping the direction. In routing, the coupling coefficients c are updated through a softmax over b, and b grows by the agreement of the prediction û with the output v. This is a teaching implementation of the layer only, not a complete trained model.

Requirements

Shell
pip install torch

How to run

Save the code in a file example.py then run it:

Shell
python example.py

Output recorded during testing

Output shape: (4, 10, 16)
Capsule lengths in [0,1): True
Probabilities of the first sample: [0.0, 0.0010000000474974513, 0.0, 0.0010000000474974513, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

This example was actually run and tested on 21 November 2026 on a CPU — torch 2.15.0, python 3.13.18

Example page
AppliedExporting a PyTorch model to ONNX and running it with ONNX RuntimeRun and tested

Goal: Moving the model from the training environment to a standalone inference environment and checking that the outputs match.

Python — example.py
import numpy as np, onnxruntime as ort, torch
from torch import nn

torch.manual_seed(0)
model = nn.Sequential(nn.Linear(4, 16), nn.ReLU(), nn.Linear(16, 3)).eval()
x = torch.randn(5, 4)
torch.onnx.export(model, (x,), "model.onnx", input_names=["x"], output_names=["y"],
                  dynamic_axes={"x": {0: "batch"}, "y": {0: "batch"}}, dynamo=False)

sess = ort.InferenceSession("model.onnx", providers=["CPUExecutionProvider"])
y_onnx = sess.run(None, {"x": x.numpy()})[0]
with torch.no_grad():
    y_torch = model(x).numpy()
print("Output shape:", y_onnx.shape)
print("Largest absolute difference:", float(np.abs(y_onnx - y_torch).max()))
print("Identical:", np.allclose(y_onnx, y_torch, atol=1e-5))

Code explanation

We export the model with torch.onnx.export with a dynamic batch axis, then load it in an InferenceSession and compare the outputs with PyTorch's. The difference should be very small (rounding errors only).

Requirements

Shell
pip install torch onnx onnxscript onnxruntime

How to run

Save the code in a file example.py then run it:

Shell
python example.py

Output recorded during testing

Output shape: (5, 3)
Largest absolute difference: 4.470348358154297e-08
Identical: True

This example was actually run and tested on 21 November 2026 on a CPU — torch 2.15.0, onnx 1.23.2, onnxscript 0.7.2, onnxruntime 1.32.0, python 3.13.18

Example page

All PyTorch examples in the Code Explorer

Pros

  • High flexibility and natural Python code
  • A huge library ecosystem that dominates research
  • Easy debugging

Limitations

  • The training loop is usually written by hand (or through helper libraries)
  • Deploying on constrained devices needs extra steps

References and verification

  • Package page on PyPI (source of the version, licence and release date) Checked directly

    https://pypi.org/project/torch/

    Checked on: 21 November 2026 — version checked: 2.15.0 — reviewed by: the DLArabic team

  • Package page on PyPI (source of the version, licence and release date) Checked directly

    https://pypi.org/project/torch/

    Checked on: 21 November 2026 — version checked: 2.15.0 — reviewed by: the DLArabic team

  • Official website A link the package officially declares on PyPI

    https://pytorch.org

    Checked on: 21 November 2026 — reviewed by: the DLArabic team

  • Official website A link the package officially declares on PyPI

    https://pytorch.org

    Checked on: 21 November 2026 — reviewed by: the DLArabic team

  • Official documentation A link the package officially declares on PyPI

    https://pytorch.org/docs

    Checked on: 21 November 2026 — reviewed by: the DLArabic team

  • Official documentation A link the package officially declares on PyPI

    https://pytorch.org/docs

    Checked on: 21 November 2026 — reviewed by: the DLArabic team

  • Official repository A link the package officially declares on PyPI

    https://github.com/pytorch/pytorch

    Checked on: 21 November 2026 — reviewed by: the DLArabic team

  • Official repository A link the package officially declares on PyPI

    https://github.com/pytorch/pytorch

    Checked on: 21 November 2026 — reviewed by: the DLArabic team

History of verified versions

VersionRelease dateChecked on
2.15.028 October 202621 November 2026

Page information last reviewed: 21 November 2026. The description was written originally for the encyclopedia and is not copied from the official websites.

Report information that needs updating