The PyTorch API

Author

Marie-Hélène Burle

PyTorch is a free and open-source machine learning and scientific computing framework based on Torch. While Torch uses a scripting language based on Lua, PyTorch has a Python and a C++ interface.

Created by Meta AI (formerly Facebook, Inc.) in 2017, it is now a project of The Linux Foundation.

PyTorch is widely used in academia and research. Part of its popularity stems from the fact that the Python interface is truly pythonic in nature, making it easier to learn than other popular frameworks such as TensorFlow.

The PyTorch API is vast and complex. This section links to the key components to get you started.

Domain-specific libraries

PyTorch is a large framework with domain-specific libraries:

These libraries contain standard datasets and utilities specific to the data in those fields.

Loading data

torch.utils.data contains everything you need create data loaders (iterables that present the data to a model).

Building models

torch.nn contains the elements you need to build your model architecture and chose a loss function.

Training

Training a model consists of optimizing the model parameters.

torch.autograd contains the tools for automatic differentiation (to compute the gradients, that is the tensors containing the partial derivatives of the error with respect to the parameters of the functions in the model) and torch.optim contains optimization algorithms that can be used for gradient descent.