liveBook · Manning

Array

As explained in Deep Learning with Python, Third Edition

A numerical container whose elements are organized over one or more axes and manipulated by vectorized operations.

Neural-network data needs a predictable numerical form. An array, often called a tensor in machine learning, holds data across one or more axes. A matrix is a two-axis tensor; tensors can have any number of axes. Arrays hold both the examples a model processes and the weights it learns. section 2.2 section 2.6

Read an image array

The MNIST training images have shape (60000, 28, 28): 60,000 images, each a 28 × 28 grid. This array has rank 3 and dtype uint8. Its shape tells you how values are arranged; its dtype tells you what kind of values it contains. section 2.2.5

Check three attributes before working with an array:

  • Rank (ndim) counts axes. A vector has one; a matrix has two. A five-entry vector still has rank 1, not rank 5. section 2.2.2 section 2.2.3
  • Shape gives the size of each axis in order. A matrix with three rows and five columns has shape (3, 5). section 2.2.5
  • Dtype identifies the stored value type, such as uint8, float32, or bool. section 2.2.5

Let the data determine the axes. Batches of feature vectors use (samples, features); sequences add a timesteps axis; images commonly use (samples, height, width, channels); video adds frames. These shapes make each axis’s job explicit. section 2.2.8

Arrays are meant to be transformed, not just stored. A Dense layer can compute `relu(matmul(input, W) + b)`: multiply an input array by weight matrix W, add bias vector b, then apply relu. During training, the gradient of the loss with respect to W has the same shape as W and guides weight updates. section 2.3 section 2.4.2

In practice, prepare inputs in a shape the network can batch. Variable-length lists of word indices cannot be fed directly as contiguous batches: pad them into a fixed-length integer tensor, or multi-hot encode them as equal-length vectors. Likewise, MNIST images can be reshaped from 28 × 28 grids into 784-value vectors for a Dense model. section 4.1.2 section 2.5

Listing 4.3 Encoding the integer sequences via multi-hot encoding
import numpy as np

def multi_hot_encode(sequences, num_classes):
    results = np.zeros((len(sequences), num_classes))                #1
    for i, sequence in enumerate(sequences):
        results[i][sequence] = 1.0                                    #2
    return results

x_train = multi_hot_encode(train_data, num_classes=10000)             #3
x_test = multi_hot_encode(test_data, num_classes=10000)               #4

The multi-hot encoder creates a two-axis NumPy array with one row per sequence, then marks the indices present in each sequence. section 4.1.2

Framework APIs differ, but the checks remain useful. PyTorch can create constant or random tensors and perform operations such as addition and matrix multiplication. Unlike TensorFlow tensors, PyTorch tensors also support assignment to individual entries. section 3.4.1

How Deep Learning with Python, Third Edition covers it

How deep it goes
Defines rank, shape, and dtype, then works through data layouts, layer operations, and input preparation without dense mathematical notation.
Examples
Uses MNIST image shapes, common dataset layouts, a Dense-layer expression, and multi-hot encoding of word-index sequences.
What it assumes
Assumes comfort reading short Python snippets while teaching tensor terminology without requiring a mathematics background.
What you can do after
Apply it via NumPy or PyTorch to inspect array attributes, choose data shapes, and prepare batchable inputs.
How it connects
Connects arrays to vectors, matrices, tensor operations, Dense layers, gradients, and mini-batch gradient descent.

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 Python, Third Edition. The section links point to the parts of the book it draws on.