liveBook · Manning

PyTorch

As explained in Deep Learning with PyTorch, Second Edition

The PyTorch core Python library namespace that exposes tensor operations and functions such as mathematical elementwise operations.

PyTorch is a Python library for building and training deep learning models. Its core data structure is the tensor, a multidimensional array. The `torch` module provides operations on tensors, so you can express calculations in familiar Python while running them on a CPU or GPU. section 1.0 section 1.5

Tensors can record the operations performed on them. PyTorch’s automatic differentiation engine, autograd, uses that history to calculate how changes to earlier values affect an output—an essential step in training a model. PyTorch also provides facilities for designing neural networks and moving from data loading through training to deployment. section 1.5

# In[4]:
import torch     #1
a = torch.ones(3)      #2
a

# Out[4]:
tensor([1., 1., 1.])

# In[5]:
a[1]         #3

# Out[5]:
tensor(1.)

# In[6]:
float(a[1])

# Out[6]:
1.0

# In[7]:
a[2] = 2.0     #4
a

# Out[7]:
tensor([1., 1., 2.])

This short listing imports `torch`, creates a tensor with `torch.ones(3)`, then reads and changes individual elements. It is a starting point for trying tensor operations interactively. section 3.2.2

For practice, look up a mathematical function such as cosine or square root in `torch` and try applying it element-wise to a tensor. Check whether it also has an in-place version, and investigate any error rather than assuming every input tensor will work. section 3.15

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Defines the library’s core components and works through first tensor operations, while deferring detailed treatment of tensors and autograd.
Examples
Demonstrates `torch.ones(3)` with element access and assignment, then asks readers to find and test an element-wise mathematical function.
What it assumes
Assumes experience with Python data types, classes, and floating-point numbers, but starts tensor work with a simple example.
What you can do after
Apply it via the `torch` module to create and modify a tensor and explore mathematical operations on it.
How it connects
Connects tensors and tensor operations to GPU computation, automatic differentiation, numerical optimization, and neural networks.

Also explained in

Compare how 11 books explain PyTorch →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with PyTorch, Second Edition. The section links point to the parts of the book it draws on.