Tensors: The Language of GPUs

Tensors: the term that sounds more intimidating than the thing actually is.

Fundamentals 12 min Intermediate May 11, 2026

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

AnalogyDefinition
Think of an Excel workbook. A scalar is a single cell (A1 = 42). A vector is a column (A1:A10). A matrix is a worksheet. A 3D tensor is the entire workbook with multiple stacked sheets — like R, G, B color channels. A 4D tensor? A folder of workbooks.

Example

Excel sheets can have different sizes — in a tensor, every "sheet" must be exactly the same size. And Excel cannot perform mathematical operations across all sheets simultaneously — tensors are designed exactly for that.
1
Scalar (0D) A single value. No indices needed. Shape: () — e.g. temperature = 23.5
2
Vector (1D) A row of values. One index needed. Shape: (n) — e.g. weights = [0.3, 0.7, 0.1]
3
Matrix (2D) A grid of values. Two indices. Shape: (m, n) — e.g. grayscale image (4, 4) = 16 values
4
3D Tensor Stacked matrices. Three indices. Shape: (k, m, n) — e.g. RGB image (3, 4, 4) = 48 values
5
4D Tensor Stack of 3D tensors. Shape: (b, k, m, n) — e.g. batch of 32 images (32, 3, 256, 256)

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

In physics, a tensor has specific transformation rules under coordinate changes (e.g., stress tensor, metric tensor). In PyTorch and TensorFlow, a tensor is simply a multi-dimensional array with GPU support and automatic gradient tracking. Same name, different meaning.

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.

Scalar (Rank 0): 42
Vector (Rank 1): [3, 7, 1, 9]
Matrix (Rank 2): [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
Tensor (Rank 3): [[[1,2],[3,4]],
[[5,6],[7,8]]]
= 2 Layers x 2 Rows x 2 Columns
Concrete Example

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].

Strength of this Analogy

Makes the structure tangible: How many dimensions does the tensor have? What shape does it have?

Limitation of this Analogy

Doesn't explain why tensors are useful for computation — only how they store data.

Tensor Dimensions Compared

RankMathematicsData CubeAI Example
0ScalarSingle pointLearning rate: 0.001
1VectorLine of numbersWord Embedding: [768]
2MatrixSheet with rows + columnsWeight matrix: [768, 512]
33D TensorCube of numbersColor image: [256, 256, 3]
44D TensorShelf full of cubesImage batch: [32, 256, 256, 3]
Active Analogy:1 / 3Exploring all perspectives helps with understanding

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.

(3, 256, 256)
Image
(5, 768)
Text
(128, 80)
Audio
(1000, 10)
Table

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

Shape mismatch errors are the most common runtime error in deep learning. You will inevitably encounter them:

RuntimeError: mat1 and mat2 shapes cannot be multiplied
  (32x768 and 512x256)

# Expected: (32, 512) @ (512, 256) -> (32, 256)
# Got: (32, 768) @ (512, 256) -> Error!

Tip: Print the shape at every intermediate step. print(tensor.shape) is your best friend when debugging.

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

PyTorch uses Channels First: (Batch, Channels, Height, Width) = (B, C, H, W). TensorFlow uses Channels Last by default: (Batch, Height, Width, Channels) = (B, H, W, C). Same pixel values, completely different memory layout. When porting between frameworks, you must explicitly permute — otherwise the network interprets channels as pixels and vice versa.

# PyTorch: Channels First (B, C, H, W)
pytorch_image = torch.randn(1, 3, 256, 256)

# TensorFlow: Channels Last (B, H, W, C)
tf_image = tf.random.normal([1, 256, 256, 3])

# Convert PyTorch -> TensorFlow
tf_image = pytorch_image.permute(0, 2, 3, 1)
# (1, 3, 256, 256) -> (1, 256, 256, 3)

Deep Dive: Tensor vs. NumPy Array

Both are n-dimensional arrays with a shape. The difference: tensors run on the GPU and support automatic differentiation (autograd). NumPy arrays run on the CPU only and have no gradient tracking. In practice, you often convert between the two: NumPy for data preparation, tensors for training.

import numpy as np
import torch

# NumPy -> Tensor (on GPU)
np_array = np.array([[1.0, 2.0], [3.0, 4.0]])
tensor = torch.from_numpy(np_array).cuda()

# Tensor -> NumPy (back to CPU)
np_back = tensor.cpu().numpy()

# Tensor has gradient tracking
w = torch.tensor([2.0], requires_grad=True)
loss = (w * 3 - 6) ** 2
loss.backward()  # Gradient computed automatically
print(w.grad)    # tensor([0.])

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.

786 KB
1 RGB image (256x256): 3 x 256 x 256 x 4 bytes
~25 MB
Batch of 32 images: 32 x 786 KB
24 GB
RTX 4090 VRAM: hundreds of images at once

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

Each new dimension has clear meaning
Image (3, 256, 256) → Batch (32, 3, 256, 256) → Video (32, 30, 3, 256, 256)
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 MB

Misconception: Larger Batch = Always Better

More is not always better. Batch size 1 wastes GPU capacity — but batches that are too large cause out-of-memory errors. Additionally, very large batches can hurt generalization: the model converges faster but tends to land in sharp minima that transfer poorly to new data.

Key Takeaways

  • A tensor is an n-dimensional array with a fixed shape. Scalar = 0D, vector = 1D, matrix = 2D, and beyond. The shape tuple tells you exactly what each dimension represents.
  • Every AI input — image, text, audio — must be encoded as a tensor of fixed shape before a GPU can process it. Shape mismatches are the most common bug in deep learning.
  • GPUs process batches, not individual examples. The batch dimension is prepended, turning a 3D image tensor into a 4D batch tensor — so thousands of cores can work in parallel.

Quiz: Tensors

Question 1 / 4
Not completed

What is a tensor?

Select one answer
Answer Key: 1) B · 2) C · 3) D · 4) B

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?