PyTorch explained

PyTorch
Developer:Meta AI
Released:[1]
Platform:IA-32, x86-64, ARM64
Language:English
Genre:Library for deep learning
License:BSD-3[2]

PyTorch is an open-source deep learning library, originally developed by Meta Platforms and currently developed with support from the Linux Foundation. The successor to Torch, PyTorch provides a high-level API that builds upon optimised, low-level implementations of deep learning algorithms and architectures, such as the Transformer, or SGD. Notably, this API simplifies model training and inference to a few lines of code. PyTorch allows for automatic parallelization of training and, internally, implements CUDA bindings that speed training further by leveraging GPU resources.

PyTorch utilises the tensor as a fundamental data type, similarly to NumPy. Training is facilitated by a reversed automatic differentiation system, Autograd, that constructs a directed acyclic graph of the operations (and their arguments) executed by a model during its forward pass. With a loss, backpropagation is then undertaken.[3]

, PyTorch remains one of the most popular deep learning libraries, alongside others such as TensorFlow and Keras.[4] A number of commercial deep learning architectures are built on top of PyTorch, including ChatGPT,[5] Tesla Autopilot,[6] Uber's Pyro,[7] Hugging Face's Transformers,[8] and Catalyst.[9]

History

In 2001, Torch was written and released under a GPL. It was a machine-learning library written in C++ and CUDA, supporting methods including neural networks, support vector machines (SVM), hidden Markov models, etc.[10] [11] [12] Around 2010, it was rewritten to by Ronan Collobert, Clement Farabet and Koray Kavuckuoglu. This was known as Torch7 or LuaTorch. This was written so that the backend was in C and the frontend was in Lua. In mid-2016, some developers refactored it to decouple the frontend and the backend, with strong influence from torch-autograd and Chainer. In turn, torch-autograd was influenced by HIPS/autograd. Development on Torch7 ceased in 2018 and was subsumed by the PyTorch project.[13] [14]

Meta (formerly known as Facebook) operates both PyTorch and Convolutional Architecture for Fast Feature Embedding (Caffe2), but models defined by the two frameworks were mutually incompatible. The Open Neural Network Exchange (ONNX) project was created by Meta and Microsoft in September 2017 for converting models between frameworks. Caffe2 was merged into PyTorch at the end of March 2018.[15] In September 2022, Meta announced that PyTorch would be governed by the independent PyTorch Foundation, a newly created subsidiary of the Linux Foundation.[16]

PyTorch 2.0 was released on 15 March 2023, introducing TorchDynamo, a Python-level compiler that makes code run up to two times faster, along with significant improvements in training and inference performance across major cloud platforms.[17] [18]

PyTorch tensors

See main article: Tensor (machine learning). PyTorch defines a class called Tensor (torch.Tensor) to store and operate on homogeneous multidimensional rectangular arrays of numbers. PyTorch Tensors are similar to NumPy Arrays, but can also be operated on by a CUDA-capable NVIDIA GPU. PyTorch has also been developing support for other GPU platforms, for example, AMD's ROCm[19] and Apple's Metal Framework.[20]

PyTorch supports various sub-types of Tensors.[21]

The meaning of the word "tensor" in machine learning is only superficially related to its original meaning in mathematics or physics as a certain kind of object in linear algebra. Tensors in PyTorch are simply multi-dimensional arrays.

PyTorch neural networks

See main article: Neural network (machine learning).

PyTorch defines a module called nn (torch.nn) to describe neural networks and to support training. This module offers a comprehensive collection of building blocks for neural networks, including various layers and activation functions, enabling the construction of complex models. Networks are built by inheriting from the torch.nn module and defining the sequence of operations in the forward function.

Example

The following program shows the low-level functionality of the library with a simple example.

import torchdtype = torch.floatdevice = torch.device("cpu") # Execute all calculations on the CPU

  1. device = torch.device("cuda:0") # Executes all calculations on the GPU
  2. Create a tensor and fill it with random numbers

a = torch.randn(2, 3, device=device, dtype=dtype)print(a)

  1. Output: tensor(-1.1884, 0.8498, -1.7129,
  2. [-0.8816, 0.1944, 0.5847]])

b = torch.randn(2, 3, device=device, dtype=dtype)print(b)

  1. Output: tensor(0.7178, -0.8453, -1.3403,
  2. [1.3262, 1.1512, -1.7070]])

print(a * b)

  1. Output: tensor(-0.8530, -0.7183, 2.58,
  2. [-1.1692, 0.2238, -0.9981]])

print(a.sum)

  1. Output: tensor(-2.1540)

print(a[1, 2]) # Output of the element in the third column of the second row (zero-based)

  1. Output: tensor(0.5847)

print(a.max)

  1. Output: tensor(0.8498)

The following code block defines a neural network with linear layers using the nn module.from torch import nn # Import the nn sub-module from PyTorch

class NeuralNetwork(nn.Module): # Neural networks are defined as classes def __init__(self): # Layers and variables are defined in the __init__ method super.__init__ # Must be in every network. self.flatten = nn.Flatten # Construct a flattening layer. self.linear_relu_stack = nn.Sequential(# Construct a stack of layers. nn.Linear(28 * 28, 512), # Linear Layers have an input and output shape nn.ReLU, # ReLU is one of many activation functions provided by nn nn.Linear(512, 512), nn.ReLU, nn.Linear(512, 10),)

def forward(self, x): # This function defines the forward pass. x = self.flatten(x) logits = self.linear_relu_stack(x) return logits

See also

Notes and References

  1. Web site: PyTorch Alpha-1 release. Chintala. Soumith. GitHub. 1 September 2016. 19 August 2020. 29 August 2021. https://web.archive.org/web/20210829055231/https://github.com/pytorch/pytorch/releases/tag/v0.1.1. live.
  2. Web site: Claburn . Thomas . 12 September 2022 . PyTorch gets lit under The Linux Foundation . . 18 October 2022 . 18 October 2022 . https://web.archive.org/web/20221018040848/https://www.theregister.com/2022/09/12/pytorch_meta_linux_foundation/ . live .
  3. Web site: Autograd Mechanics . PyTorch Documentation . 13 November 2025.
  4. Web site: Top 30 Open Source Projects . github.com . 13 November 2025.
  5. Web site: OpenAI standardizes on PyTorch . 2020-01-30 . 2026-01-08.
  6. Web site: Karpathy. Andrej. PyTorch at Tesla - Andrej Karpathy, Tesla. YouTube. 6 November 2019. 2 June 2020. 24 March 2023. https://web.archive.org/web/20230324144838/https://www.youtube.com/watch?v=oBklltKXtDE. live.
  7. News: Uber AI Labs Open Sources Pyro, a Deep Probabilistic Programming Language. 3 November 2017. Uber Engineering Blog. 18 December 2017. en-US. 25 December 2017. https://web.archive.org/web/20171225034106/https://eng.uber.com/pyro/. live.
  8. Web site: Ecosystem Tools. pytorch.org. en. 18 June 2020. 18 July 2023. https://web.archive.org/web/20230718105354/https://pytorch.org/ecosystem/. live.
  9. Web site: Ecosystem Tools. pytorch.org. en. 4 April 2020. 18 July 2023. https://web.archive.org/web/20230718105354/https://pytorch.org/ecosystem/. live.
  10. http://torch.ch/torch3/matos/tutorial.pdf "Torch Tutorial", Ronan Collobert, IDIAP, 2002-10-02
  11. R. Collobert, S. Bengio and J. Mariéthoz. Torch: a modular machine learning software library. Technical Report IDIAP-RR 02-46, IDIAP, 2002.
  12. Web site: Torch Library . https://web.archive.org/web/20011031104036/http://www.torch.ch/ . 31 October 2001 .
  13. https://github.com/torch/torch7/commit/fd0ee3bbf7bfdd21ab10c5ee70b74afaef9409e1 torch/torch7, Commit fd0ee3b, 2018-07-02
  14. Web site: Chintala . Soumith . 2023-12-17 . PyTorch's design origins . 2026-02-07 . soumith.ch.
  15. Web site: Caffe2 Merges With PyTorch. 2 April 2018. 2 January 2019. 30 March 2019. https://web.archive.org/web/20190330143500/https://medium.com/@Synced/caffe2-merges-with-pytorch-a89c70ad9eb7. live.
  16. Web site: Meta spins off PyTorch Foundation to make AI framework vendor neutral . 12 September 2022 . . Edwards . Benj . 13 September 2022 . 13 September 2022 . https://web.archive.org/web/20220913034850/https://arstechnica.com/information-technology/2022/09/meta-spins-off-pytorch-foundation-to-make-ai-framework-vendor-neutral/ . live .
  17. Web site: Dynamo Overview .
  18. News: PyTorch 2.0 brings new fire to open-source machine learning . 16 March 2023 . VentureBeat . 15 March 2023 . 16 March 2023 . https://web.archive.org/web/20230316004808/https://venturebeat.com/ai/pytorch-2-0-brings-new-fire-to-open-source-machine-learning/ . live .
  19. Web site: Installing PyTorch for ROCm. 9 February 2024. rocm.docs.amd.com.
  20. Web site: Introducing Accelerated PyTorch Training on Mac . 4 June 2022 . pytorch.org . en . 29 January 2024 . https://web.archive.org/web/20240129141050/https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/ . live .
  21. Web site: An Introduction to PyTorch – A Simple yet Powerful Deep Learning Library . analyticsvidhya.com . 11 June 2018 . 22 February 2018 . 22 October 2019 . https://web.archive.org/web/20191022200858/https://www.analyticsvidhya.com/blog/2018/02/pytorch-tutorial/ . live .