liveBook · Manning

Array

As explained in Deep Learning with PyTorch, Second Edition

A container for indexed collections of values.

An array stores values that you can access by index. In PyTorch, the everyday array is a tensor: a multidimensional collection of numbers. A one-dimensional tensor needs one index to select a value; a two-dimensional tensor needs two. Its shape gives the size of each dimension, in order. A tensor with shape (3, 2), for example, has three rows of two values. section 3.2 section 3.1

Three points in one array

```python import torch points = torch.tensor([[4.0, 1.0], [5.0, 3.0], [2.0, 1.0]]) points.shape # torch.Size([3, 2]) points[0, 1] # tensor(1.) points[0] # tensor([4., 1.]) ``` Each row holds a point’s two coordinates. The first index selects a row; the second selects a coordinate within it. Indices start at zero. Selecting a whole row returns another tensor. section 3.2.3

A Python list can also hold indexed numbers, but its elements are separately allocated Python objects. Ordinary PyTorch tensors hold numbers of one type in numerical storage, making large collections and mathematical operations more efficient. Like NumPy arrays, they support multidimensional indexing; unlike a simple list, they also provide operations such as broadcasting. Broadcasting applies an element-wise operation to compatible shapes—for example, adding one scalar to every value or multiplying a 1 × 3 array by a 3 × 1 array to produce a 3 × 3 result. section 3.6 section 3.4

Indexing need not copy the numbers. A tensor can be a view of the same underlying storage, described by its shape, storage offset, and strides. Changing a selected row can therefore change the original array; use `clone()` when an independent copy is needed. The same tensor operations can run on a CPU or GPU after placing the data on the chosen device. section 3.9.1 section 3.10.1

Figure 3.4 Tensors are views of a Storage instance.
figure

The storage diagram helps show why differently shaped tensor views can refer to the same numbers without copying them. section 3.8

Arrays make data usable by neural networks: images, time series, and text can be converted into appropriately shaped numerical tensors. Network inputs, outputs, intermediate values, and parameters are all represented this way. section 4.0 section 3.0

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Works through construction, indexing, broadcasting, and shared-storage views down to offsets and strides, without developing generalized tensor backends.
Examples
Uses indexed point coordinates, broadcast arithmetic on differently shaped tensors, and a shared-storage view changed through a selected row.
What it assumes
Assumes basic Python programming, including data types and numbers, while introducing tensor construction and indexing through simple examples.
What you can do after
Build it from scratch with `torch.tensor`, inspect its shape, index values, and distinguish shared views from copies.
How it connects
Connects arrays to PyTorch tensors, NumPy arrays, broadcasting, tensor views, and neural networks.

Also explained in

Compare how 40 books explain array →

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.