promptdojo_

Tensors and shapes, before anything else

Open any deep-learning script an AI wrote and you'll hit torch.tensor, .shape, and errors like "mat1 and mat2 shapes cannot be multiplied (2x3 and 2x3)". You don't need the math to read these — you need the one idea underneath them: a tensor is a grid of numbers with a shape.

  • A single number is a scalar — shape ().
  • A list of numbers is a vector — shape (3,). Think: one customer's feature row, or one embedding for search.
  • A grid is a matrix — shape (2, 3) means 2 rows by 3 columns. Think: a batch of 2 customers, 3 features each.
  • Stack matrices and you get 3-D tensors and beyond (e.g. a batch of images). The pattern never changes: shape is just the size along each dimension.

We use plain Python lists here so everything runs in your browser, but the shape rules are exactly PyTorch's. In real code you'd write t = torch.tensor([[1,2,3],[4,5,6]]) and read t.shape(2, 3).

Why shape is the thing you actually debug

Most "AI wrote broken tensor code" moments are shape mismatches: you fed a (2, 3) where the next step expected (3, 2), or you forgot a batch dimension. The fix is never calculus — it's reading the shapes and making them line up. For a 2-D tensor stored as a list of rows, the shape is (number of rows, length of one row) = (len(t), len(t[0])). Learn to read that on sight and tensor tracebacks stop being scary.