liveBook · Manning

PyTorch

As explained in Inside Deep Learning: Math, Algorithms, Models

A Python deep learning library that provides tensor computation, automatic differentiation, and data abstractions for neural network training.

PyTorch is a Python deep learning library built around tensors: multidimensional arrays that represent data, network inputs and outputs, and learnable parameters. Tensor operations also support GPU execution and automatic differentiation, providing the computational foundation for neural network training. section 1.2 section 1.7

Use PyTorch to:

  • Represent scalars, vectors, matrices, and higher-dimensional arrays with torch.tensor; inspect their shapes and slice sub-tensors. section 1.2
  • Move tensor calculations to a CUDA device when the workload is large enough to offset CPU-to-GPU transfer costs. section 1.2.1
  • Track tensor operations and compute derivatives automatically instead of deriving every gradient by hand. section 1.3.1 section 2.1.1
  • Minimize a loss by updating parameters in the opposite direction of their gradients through an optimizer such as stochastic gradient descent. section 1.4

A minimal optimization loop

For f(x) = (x − 2)², make x a learnable parameter, calculate the loss, backpropagate, and update it repeatedly: x_param = torch.nn.Parameter(torch.tensor([-3.5]), requires_grad=True); optimizer = torch.optim.SGD([x_param], lr=0.1). Each iteration calls optimizer.zero_grad(), evaluates f(x_param), calls loss.backward(), and finishes with optimizer.step(). The parameter approaches 2, the function's minimum. section 1.3.3 section 1.4

Figure 1.4 Examples of tensors, with more dimensions or axes as we move from left to right. A scalar represents a single value. A vector is a list of values and is how we often think about one datapoint. A matrix is a grid of values and is often used for a dataset. A three-dimensional tensor can be used to represent a dataset of sequences.

Compare scalar, vector, matrix, and three-dimensional tensor shapes to connect mathematical axes with the data structures used in PyTorch. section 1.2

The same pattern scales from one variable to neural networks: define a model and loss, let automatic differentiation produce the gradient for every parameter, and update those parameters to reduce the loss. PyTorch modules can also be composed into larger architectures, reuse weights, and reshape data between image and vector representations. section 2.1.1 section 7.1.2

How Inside Deep Learning: Math, Algorithms, Models covers it

How deep it goes
Explains tensor shapes, device placement, gradients, learning rates, and optimizer loops, while deferring broader Dataset usage details.
Examples
Uses tensor construction, NumPy conversion, matrix multiplication, device errors, quadratic minimization, SGD loops, and PCA autoencoder modules.
What it assumes
Assumes Python and prior NumPy or machine-learning familiarity, while teaching tensor notation, gradients, and optimization mechanics.
What you can do after
Build and optimize tensor-based models with PyTorch automatic differentiation, CUDA placement, Parameters, losses, and SGD.
How it connects
Connects tensors with NumPy, GPUs with CUDA devices, and training with automatic differentiation, loss functions, parameters, and optimizers.

Also explained in

Compare how 11 books explain PyTorch →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Inside Deep Learning: Math, Algorithms, Models. The section links point to the parts of the book it draws on.