0PricingLogin
Deep Learning Academy · Lesson

Broadcasting Rules That Save You Loops

Combine different-shaped tensors elementwise.

Combine Without Matching Shapes

Broadcasting lets PyTorch stretch a smaller tensor to fit a bigger one, so you skip writing loops to repeat values.

Add a Scalar to Everything

The simplest broadcast: add one number to a whole tensor. PyTorch applies it to every element at once. ✨

x = torch.tensor([1, 2, 3])
print(x + 10)  # tensor([11, 12, 13])

All lessons in this course

  1. Shapes, Dtypes & Indexing
  2. Reshape, View, Squeeze & Unsqueeze
  3. Broadcasting Rules That Save You Loops
  4. Tensors Talk to NumPy
← Back to Deep Learning Academy