Tensors: The Language of GPUs
Tensors: the term that sounds more intimidating than the thing actually is.
You already know scalars, vectors, and matrices. But deep learning does not stop at 2D. A color image needs three layers (Red, Green, Blue) — that is a 3D tensor. A batch of images adds another dimension — 4D. A batch of videos? 5D.
In this article, you will climb the dimension ladder, understand why EVERY AI input gets converted to a tensor, and learn why GPUs demand batches of data rather than one example at a time.
The Dimension Ladder
Tensor
A tiny 4x4 RGB image shows the dimension ladder in action. Grayscale: a matrix with shape (4, 4) = 16 values. Color: a 3D tensor with shape (3, 4, 4) = 48 values. Three layers stacked: Red, Green, Blue. Pixel at position (2, 3): Red=220, Green=45, Blue=180 — purple.
import numpy as np
# Scalar (0D)
scalar = np.array(42) # Shape: ()
# Vector (1D)
vector = np.array([1.0, 2.0, 3.0]) # Shape: (3,)
# Matrix (2D)
matrix = np.zeros((4, 4)) # Shape: (4, 4)
# 3D Tensor (RGB image)
rgb_image = np.random.randint(0, 256, (3, 4, 4)) # Shape: (3, 4, 4)
print(rgb_image.shape) # (3, 4, 4)
print(rgb_image[0, 2, 3]) # Red value at position (2,3)Misconception: Tensor in Code = Tensor in Physics
A 0D scalar holds a single value. But with each new dimension, information density grows: a 768-dimensional vector encodes the meaning of an entire word, a matrix transforms that meaning — and a 4D tensor can push 32 images through a neural network simultaneously.
Interactive: Three Views of Tensors
Tensors are abstract — but different analogies make them tangible. Switch between three perspectives to understand what a tensor is: a data cube for stacking, a mathematical transformation, or a numbered storage system.
What is a Tensor?
A tensor is the fundamental data structure in modern AI systems. Depending on the perspective, tensors can be understood in very different ways. Switch between three analogies to experience different perspectives.
Tensor as a Multi-Dimensional Data Cube
Think of a tensor as a container that organizes numbers across multiple dimensions. A scalar is a single point. A vector is a line of numbers. A matrix is a sheet with rows and columns. A 3D tensor is a cube. And it goes further: a 4D tensor is a shelf full of cubes.
A color image of 256 x 256 pixels is stored as a rank-3 tensor: [256, 256, 3] — height, width, and three color channels (red, green, blue). A batch of 32 images creates a rank-4 tensor: [32, 256, 256, 3].
Tensor Dimensions Compared
| Rank | Mathematics | Data Cube | AI Example |
|---|---|---|---|
| 0 | Scalar | Single point | Learning rate: 0.001 |
| 1 | Vector | Line of numbers | Word Embedding: [768] |
| 2 | Matrix | Sheet with rows + columns | Weight matrix: [768, 512] |
| 3 | 3D Tensor | Cube of numbers | Color image: [256, 256, 3] |
| 4 | 4D Tensor | Shelf full of cubes | Image batch: [32, 256, 256, 3] |
Why ML Thinks in Tensors
GPUs and TPUs process dense, fixed-size numerical arrays with vectorized instructions. Therefore every input — image, text, audio, tabular data — must be "translated" into a tensor with a defined shape. The GPU speaks only one language: tensor.
The shape tells you what each dimension means. Wrong shape = wrong interpretation = bugs or nonsensical results. The shape of a tensor is the single most important piece of metadata in deep learning.
Shape Errors: Bug #1 in Deep Learning
Why fixed size? GPUs need to know the memory layout in advance. Variable-length inputs get padded to a fixed maximum length or truncated.
Deep Dive: Channels First vs. Channels Last
Deep Dive: Tensor vs. NumPy Array
Batching: Feeding the GPU
GPUs have thousands of cores all executing the same operation on different data simultaneously (SIMD — Same Instruction, Multiple Data). Processing one image at a time wastes most cores. That is why the batch dimension is prepended.
Three Reasons for Batching
GPU utilization: All cores busy simultaneously. A single image uses only a fraction of the available compute power.
Statistical stability: The gradient averaged over a batch is less noisy than the gradient of a single example. Training converges more smoothly.
Trade-off: Larger batch = better GPU utilization, but more memory usage. Batch size is a critical hyperparameter.
Shape Progression
import torch
# Single image: shape (3, 256, 256)
img = torch.randn(3, 256, 256)
# Batch of 32 images: shape (32, 3, 256, 256)
batch = torch.stack([torch.randn(3, 256, 256) for _ in range(32)])
print(batch.shape) # torch.Size([32, 3, 256, 256])
# Memory: 32 * 3 * 256 * 256 * 4 bytes = ~25 MBMisconception: Larger Batch = Always Better
Key Takeaways
Quiz: Tensors
Learning Goals
- An RGB image has 128x128 pixels. What shape does the tensor have (channels first), and how many individual values does it contain?
- Why must a text paragraph be converted to a tensor before a neural network can process it — and what information is lost?
- You have a batch of 16 RGB images at 224x224 pixels. What is the tensor's shape, and why doesn't the GPU process one image at a time?