liveBook · Manning

Array

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

An array with any number of dimensions, including scalars, vectors, matrices, and higher-rank arrays.

A tensor is an array with any number of dimensions. In PyTorch, tensors hold both the data flowing through a neural network and the parameters that control it. This common representation makes tensor operations available to GPU computation and automatic differentiation. section 1.2 section 1.7

The number of axes tells you what kind of array you have:

  • Zero axes: a scalar, such as `torch.tensor(3.14)`, with shape `torch.Size([])`. section 1.2
  • One axis: a vector. `torch.tensor([1, 2, 3, 4])` has shape `torch.Size([4])`. section 1.2
  • Two axes: a matrix. Four rows of two values have shape `torch.Size([4, 2])`. Adding an axis gives a three-dimensional array; the supplied example has shape `torch.Size([4, 2, 3])`. section 1.2
print(torch_scalar.shape) 
print(torch_vector.shape) 
print(torch_matrix.shape) 
print(torch_tensor3d.shape)

torch.Size([]) 
torch.Size([4]) 
torch.Size([4, 2]) 
torch.Size([4, 2, 3])

Compare the four printed shapes: each added axis contributes another size to `torch.Size`. section 1.2

Shape names make code easier to check. For example, `(B, C, W, H)` names four axes; a weight matrix written `W` with shape `(d, C)` has `d` feature rows and `C` output columns. In a linear layer, `nn.Linear(d, C)` creates the corresponding input-to-output mapping. For matrix multiplication, shapes `(n, m)` and `(m, p)` produce `(n, p)`: the shared `m` dimension must line up. section 1.2 section 2.1.5 section 1.2.1

Move between NumPy and PyTorch

Given a NumPy array `x_np`, `x_pt = torch.tensor(x_np)` makes a PyTorch tensor. Check `x_pt.dtype` rather than assuming its numeric type: a NumPy `float64` array produces a `torch.float64` tensor in the example. To request single precision, use `torch.tensor(x_np, dtype=torch.float32)`. Familiar operations carry over: `torch.sum(x_pt)` sums values, while `torch.transpose(x_pt, 0, 1)` exchanges two axes. For a tensor of shape `(4, 2, 3)`, exchanging axes 0 and 2 yields `(3, 2, 4)`. section 1.2

Slices can select a row or column from a matrix, producing a smaller tensor. Which axis means what depends on the data: a matrix might hold a dataset, while a three-dimensional tensor might hold a dataset of sequences. section 1.2

A GPU can speed up large tensor calculations, but moving data between CPU and GPU has a cost. Benchmark small workloads rather than assuming a GPU is faster. Move a tensor with `x = x.to(device)`; tensors used together must be on the same device. A CUDA tensor must be copied back to CPU before NumPy can use it. section 1.2.1

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

How deep it goes
Defines arrays by axis count and works through shapes, dtypes, conversion, operations, and GPU placement without developing storage internals.
Examples
Shows scalar-to-three-dimensional shapes, NumPy conversion, dtype checks, transposition, and CPU–CUDA device errors.
What it assumes
Assumes Python machine-learning experience and likely NumPy familiarity, while introducing PyTorch tensor construction and dimensions from the basics.
What you can do after
Apply it via PyTorch to create tensors, inspect shapes and dtypes, perform basic operations, and place tensors on a GPU.
How it connects
Connects tensors to NumPy arrays, matrix multiplication, GPU computation, automatic differentiation, and linear layers.

Also explained in

Compare how 48 books explain array →

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.