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 deep learning projects. Its core data structure is the tensor, a multidimensional array for holding numbers, vectors, matrices, and more. The torch module provides operations on tensors, so you can express and run computations in familiar Python code. section 1.3 section 1.5

To create a tensor, import torch and call a function such as torch.ones. You can then read or change individual values using zero-based indexing: section 3.2.2

# 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.])

Create a three-element tensor, inspect one value, and change another. section 3.2.2

The torch library also provides mathematical functions that operate element-wise on tensors. For practice, find a function such as cosine or square root, apply it to a tensor, and investigate whether an in-place version is available. section 3.15

Tensors and their operations can run on a CPU or GPU. PyTorch can also track numerical operations and use that history to calculate how an output changes when inputs change. Its automatic differentiation engine, autograd, handles these calculations for optimization during training. section 1.5

A typical project loads data, defines and trains a model, and evaluates or deploys it. PyTorch emphasizes immediate execution and an approachable Python API; much of its performance-oriented implementation is written in C++ and CUDA. section 1.5 section 1.8

Figure 1.2 Basic, high-level structure of a PyTorch project, with data loading, training, and deployment to production
figure

See how data loading, training, and deployment fit together in a PyTorch project. section 1.5

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Introduces tensor creation and indexing, outlines autograd and accelerated operations, and sets exercises for mathematical functions.
Examples
Creates and edits a tensor of ones and proposes exercises with views, storage, cosine, and square root.
What it assumes
Assumes Python programming experience, including data types, classes, and floating-point numbers, while starting deep learning from the basics.
What you can do after
Apply PyTorch to construct tensors and build, train, and deploy deep learning models.
How it connects
Connects tensors and tensor operations with automatic differentiation, numerical optimization, neural networks, and GPU acceleration.

Also explained in

Compare how 14 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.