liveBook · Manning

Chapter 3: It starts with a tensor

What you will learn in chapter 3 of Deep Learning with PyTorch, Second Edition

The introduction frames deep learning as learning transformations between data representations and explains why PyTorch begins with numeric inputs and tensors.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction frames deep learning as learning transformations between data representations and explains why PyTorch begins with numeric inputs and tensors.

Key terms: representation, input-to-output relationship, patterns, floating-point numbers, tensors

3.1 The world as floating-point numbers

The section explains why neural networks represent data as floating-point numbers and introduces PyTorch tensors as the structures used to hold those numbers.

Key terms: floating-point numbers, encode, decode, intermediate representations, weights, tensor, multidimensional array, dimension, shape, index

3.2 Tensors: Multidimensional arrays

Defines a PyTorch tensor as an array of numbers whose elements can be accessed by index, including with multiple indices.

Key terms: tensor, array, index, multiple indices

3.2.1 From Python lists to PyTorch tensors

The section uses a Python list to introduce zero-based indexing and explains why tensors are useful for representing and manipulating data.

Key terms: tensor, slice, Python lists, list indexing, zero-based index

3.2.2 Constructing our first tensors

The section constructs a simple PyTorch tensor and shows that its elements can be accessed and changed.

Key terms: torch module, one-dimensional tensor, zero-based index

3.2.3 The essence of tensors

This section explains how tensors store homogeneous numeric values, organize coordinates by dimensions, and return a view when a row is selected.

Key terms: Python objects, unboxed C numeric types, homogeneous, metadata, 2D tensor, shape, dimension, index, view, underlying data

3.3 Indexing tensors

This section shows that PyTorch tensors use Python-style range indexing to select elements, including along each dimension.

Key terms: range indexing notation, tensor’s dimensions

3.4 Broadcasting

Explains how broadcasting lets PyTorch perform element-wise operations on tensors with compatible but different shapes.

Key terms: broadcasting, compatible dimensions, right-align, element-wise, virtually duplicated, scalar

3.5 Named tensors

This section uses an image-conversion example to show why positional tensor dimensions can cause mistakes and how named tensors can make dimension alignment clearer.

Key terms: dimension, axis, channel dimension, batch dimension, named tensors, names, refine_names, rename, broadcasting, align_as

3.6 Tensor element types

This section explains why Python numbers and lists are inefficient for large-scale numerical work and why tensors track a single numeric type for their elements.

Key terms: boxing, Python lists, Python interpreter, tensor, numeric type, high-level API

3.6.1 Specifying the numeric type with dtype

This section explains how the dtype argument sets a tensor’s numeric type and identifies PyTorch’s default and available types.

Key terms: dtype, tensor constructors, torch.float32, torch.float16, torch.bfloat16, torch.uint8, torch.bool

3.6.2 A dtype for every occasion

This section explains why float32 and int64 are common tensor dtypes and how tensor comparisons produce bool tensors.

Key terms: float32, 64-bit floating-point precision, 16-bit floating-point, half-precision, indexing tensors, 64-bit integer, int64, predicates, bool tensors

3.6.3 Managing a tensor’s dtype attribute

This section shows how to set, inspect, and change a tensor’s dtype, and explains what happens when operations mix input types.

Key terms: dtype, constructor, casting method, to, input types, larger type, 32-bit computation

3.7 The tensor API

Introduces the two ways to call most tensor operations and shows how the online documentation organizes the tensor API.

Key terms: torch module, methods of a tensor object, transpose, online docs, Creation ops, Math ops, Random sampling, Serialization

3.8 Tensors: Scenic views of storage

This section explains how tensors view numerical data in storage and why multiple views can share memory efficiently.

Key terms: torch.Storage, Tensor, view, offset, per-dimension strides, storage, tensor views, points[0]

3.8.1 Indexing into storage

This section shows how a multidimensional tensor refers to one-dimensional storage and how changes to that storage affect the tensor.

Key terms: tensor, storage, contiguous array, referring tensor

3.8.2 Modifying stored values: In-place operations

This section explains how a trailing underscore distinguishes in-place tensor methods from operations that return a new tensor.

Key terms: in place, trailing underscore, zero_, source tensor

3.9 Tensor metadata: Size, offset, and stride

Introduces the size, storage offset, and stride metadata that, together with storage, define a tensor and how its elements are indexed.

Key terms: storage, size, shape, storage offset, stride

3.9.1 Views of another tensor’s storage

This section explains how tensor views use size, storage offset, and stride to access shared storage without copying data.

Key terms: storage offset, stride, storage, subtensor, view, size, clone

3.9.2 Transposing without copying

The section shows how transposing a two-dimensional tensor changes its shape and stride while sharing the original storage.

Key terms: stride, storage, points, transpose, t function, shape, points_t

3.9.3 Transposing in higher dimensions

The section extends transposition to multidimensional tensors and explains why contiguous storage can make ordered access efficient.

Key terms: transpose, shape, stride, contiguous, storage, data locality

3.9.4 Contiguous tensors

This section explains how to make a noncontiguous tensor contiguous and why some PyTorch operations require that layout.

Key terms: contiguous, transpose, stride, storage, view

3.10 Moving tensors to the GPU

The section explains that PyTorch tensors can be stored and operated on using GPUs, and describes PyTorch’s support for different accelerator hardware.

Key terms: tensor, CPU, GPU, GPU-specific routines, CUDA, ROCm, Metal Performance Shaders (MPS), torch_xla, hardware vendors

3.10.1 Managing a tensor’s device attribute

Explains how a tensor’s device controls where its data lives and how moving data to a GPU affects subsequent operations.

Key terms: device, GPU, CPU, to method, cuda, GPU memory, GPU-based tensors, user-facing API

3.11 NumPy interoperability

This section explains how PyTorch tensors and NumPy arrays interoperate through shared CPU memory, with a GPU-copy exception and a numeric-type consideration.

Key terms: NumPy array, tensor storage, underlying buffer, CPU RAM, GPU, 32-bit floating-point, 64-bit, dtype torch.float

3.12 Generalized tensors are tensors, too

The section explains that PyTorch’s tensor API can work with different storage representations and hardware through dispatching.

Key terms: tensor API, tensor, multidimensional arrays, dispatcher, user-facing API, backend functions, numeric types, dense, strided, sparse tensors

3.13 Serializing tensors

This section explains how to save and reload tensors with PyTorch and why its serialization format may not suit software outside PyTorch.

Key terms: serialize, pickle, storage, file descriptor, interoperable, file format

3.13.1 Serializing to HDF5 with h5py

This section shows how h5py saves tensors in interoperable HDF5 files and loads selected data back into PyTorch tensors.

Key terms: HDF5, h5py, NumPy arrays, create_dataset, coords, NumPy array, dataset, torch.from_numpy, tensor’s storage

3.14 Conclusion

The section closes the tensor groundwork and previews further tensor topics and real-world data representation.

3.15 Exercises

3.16 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 2: Pretrained networks Chapter 4: Real-world data representation using tensors →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.