arrow_back All articles

The Atoms of AI: Understanding Tensors

William Golovlev · 2 min read AI/ML
The Atoms of AI: Understanding Tensors

Start digging into how AI works and you hit the word “tensor” almost immediately. It sounds like something out of a physics textbook, but the idea is simple, and everything else in machine learning is built on it.

This is part one of my learning series. Here I cover what a tensor is and why we use them instead of Python lists or spreadsheet-style tables.

What is a tensor?

A tensor is a block of numbers with any number of dimensions. A matrix is just the 2D case.

  • A scalar (a single number like 99) is a rank zero tensor.
  • A list of numbers is a rank one tensor.
  • A grid, or matrix, is a rank two tensor.

It keeps going from there. A batch of 32 color images, each 224 by 224 pixels, is a rank four tensor with shape [32, 3, 224, 224]: images, color channels, height, width.

We usually picture them as grids, but in ML libraries they do more than store data.

Tensors vs. DataFrames

If you’ve done data work in Python, you know Pandas DataFrames and NumPy arrays. So why not just use those?

DataFrames are built for humans. Labels and indexes make them easy to read, but the computer doesn’t care about labels. It wants raw numbers, fast. NumPy arrays are already just raw numbers, though. What tensors add on top is GPU support and automatic gradient tracking (autograd), which is the important part.

The secret sauce: tracking history

The most important thing I learned is that tensors in PyTorch or TensorFlow have a “memory.”

When you do math on a tensor, you don’t just get a result. The library records every operation that led to it, in something called a computational graph.

In PyTorch, that recording is controlled by requires_grad. When it’s on, the tensor keeps its history. That’s what makes learning possible:

  1. The model makes a prediction.
  2. It measures the error.
  3. It uses that recorded history to work backward and figure out which numbers (weights) to adjust for a better result next time.

Without that history, the model would have no way to learn from its mistakes.

Turning the knobs

Picture a model as an old radio. Training means turning the knobs until the signal comes in clear. Tensors are what let the computer work out how much to turn each knob.

Sometimes you want to pause the recording. When you’re actually updating the knobs, you don’t want that update tracked as part of the history. That’s what torch.no_grad is for: it turns tracking off while you make the adjustment.

The takeaway

Tensors are fast, they’re purely numbers, and they remember the path that produced a result. That recorded history is what lets a machine learn.

I’m still learning this as I go, and the jargon is a lot at first. But once you see a tensor as “a grid of numbers with a memory,” the rest of ML gets much easier to follow. Next up: loss functions, the scorecard that tells a model how wrong it is.

Let me know what you think on LinkedIn, and stay tuned for more videos in this series!

#Machine Learning#Tensors#PyTorch#AI Tutorial#Neural Networks