Explains the fundamental differences between tensor dimensions, size, and shape, clarifying frequent misconceptions—such as the distinction between the number of features ("columns") and true data dimensions—while also demystifying reshaping operations like expand_dims, squeeze, and transpose in NumPy. Through practical examples from images and natural language processing, listeners learn how to manipulate tensors to match model requirements, including scenarios like adding dummy dimensions for grayscale images or reordering axes for sequence data.
Tensor: A general term for an array of any number of dimensions.
NDArray (NumPy): Stands for "N-dimensional array," the foundational array type in NumPy, synonymous with "tensor."
.ndim property (e.g., array.ndim).[1, 2, 3, 4, 5]).size property.shape = (256, 256, 3).(1_000_000, 50)(256, 256, 3)(batch_size, width, height, channels), e.g., (32, 256, 256, 3).Adding Dimensions:
(256, 256) to (256, 256, 1)).np.expand_dims or array.reshape.Removing Singleton Dimensions:
(N, 1) and single dimension should be removed to yield (N,).np.squeeze or array.reshape.Wildcard with -1:
-1 is a placeholder for NumPy to infer the correct size, useful when batch size or another dimension is variable.Flattening:
np.ravel to turn a multi-dimensional tensor into a contiguous 1D array.np.transpose for general axis permutations.np.swapaxes to swap two specific axes but prefer transpose for clarity and flexibility.(batch_size, sequence_length, embedding_dim) might need to be reordered to (batch_size, embedding_dim, sequence_length) for certain models.array.transpose(0, 2, 1)| Operation | NumPy Function | Purpose |
|---|---|---|
| Add dimension | np.expand_dims | Convert (256,256) to (256,256,1) |
| Remove dimension | np.squeeze | Convert (N,1) to (N,) |
| General reshape | np.reshape | Any change matching total size |
| Flatten | np.ravel | Convert (a,b) to (a*b,) |
| Swap axes | np.swapaxes | Exchange positions of two axes |
| Permute axes | np.transpose | Reorder any sequence of axes |