PyTorch Tensors: Your First Building Blocks
The Story of Tensors
Imagine you have a magic box of LEGO bricks. These bricks can be arranged in different ways:
- A single brick = a number
- A row of bricks = a list of numbers
- A flat mat of bricks = a grid of numbers
- A 3D tower of bricks = a cube of numbers
In PyTorch, these LEGO structures are called Tensors. They hold numbers, and computers use them to learn things—like recognizing cats in photos or understanding your voice!
What You’ll Learn
graph TD A[Creating Tensors] --> B[From Shape] A --> C[From Sequences] A --> D[Random Creation] B --> B1[zeros & ones] B --> B2[empty & full] C --> C1[Lists & Arrays] D --> D1[rand & randn] D --> D2[randint]
1. Tensor Creation from Shape
What Does “Shape” Mean?
Shape is like telling PyTorch how many rows and columns you want.
Think of it like ordering a chocolate bar:
- “I want a bar with 2 rows and 3 pieces in each row” → Shape is
(2, 3)
Creating Tensors Filled with Zeros
Why zeros? Sometimes you need an empty notebook before writing. Zeros are like blank pages.
import torch
# A tensor with 3 zeros in a row
zeros_1d = torch.zeros(3)
# Result: tensor([0., 0., 0.])
# A 2x3 grid of zeros
zeros_2d = torch.zeros(2, 3)
# Result:
# tensor([[0., 0., 0.],
# [0., 0., 0.]])
Creating Tensors Filled with Ones
Why ones? Ones are useful when you need a starting point that isn’t zero—like giving everyone one cookie to start!
# A tensor with 4 ones
ones_1d = torch.ones(4)
# Result: tensor([1., 1., 1., 1.])
# A 3x2 grid of ones
ones_2d = torch.ones(3, 2)
# Result:
# tensor([[1., 1.],
# [1., 1.],
# [1., 1.]])
Creating Empty Tensors
What’s “empty”? It’s like grabbing random sticky notes—the numbers inside are whatever was left in memory. NOT zeros!
# An empty 2x2 tensor (random junk inside)
empty = torch.empty(2, 2)
# Result: random numbers (unpredictable!)
Creating Tensors Filled with a Specific Number
Why? Sometimes you want all 7s, or all 42s. Use full()!
# A 2x3 tensor filled with 7
sevens = torch.full((2, 3), 7)
# Result:
# tensor([[7, 7, 7],
# [7, 7, 7]])
2. Tensor Creation with Sequences
What’s a Sequence?
A sequence is an ordered list of numbers you already have—like your toy collection lined up on a shelf.
From a Python List
The most common way! You have numbers, you turn them into a tensor.
# From a simple list
my_list = [1, 2, 3, 4, 5]
tensor_from_list = torch.tensor(my_list)
# Result: tensor([1, 2, 3, 4, 5])
# From a nested list (2D)
grid = [[1, 2], [3, 4], [5, 6]]
tensor_2d = torch.tensor(grid)
# Result:
# tensor([[1, 2],
# [3, 4],
# [5, 6]])
From a NumPy Array
NumPy is Python’s famous math library. PyTorch plays nicely with it!
import numpy as np
numpy_array = np.array([10, 20, 30])
tensor_from_numpy = torch.from_numpy(numpy_array)
# Result: tensor([10, 20, 30])
Magic trick: They share memory! Change one, the other changes too.
Using arange() - Numbers in a Row
Like counting: 0, 1, 2, 3, 4…
# Numbers from 0 to 4
count = torch.arange(5)
# Result: tensor([0, 1, 2, 3, 4])
# Numbers from 2 to 10, stepping by 2
evens = torch.arange(2, 11, 2)
# Result: tensor([2, 4, 6, 8, 10])
Using linspace() - Equal Spacing
Like cutting a cake into perfectly equal slices.
# 5 numbers between 0 and 1
slices = torch.linspace(0, 1, 5)
# Result: tensor([0.00, 0.25, 0.50, 0.75, 1.00])
3. Random Tensor Creation
Why Random Numbers?
Imagine teaching a robot to paint. It starts by making random brush strokes, then learns which ones look good. Neural networks start with random numbers and learn from there!
rand() - Random Between 0 and 1
Like rolling a special dice that gives you any number between 0 and 1.
# 3 random numbers between 0 and 1
random_3 = torch.rand(3)
# Result: tensor([0.4523, 0.8912, 0.1234])
# (your numbers will be different!)
# A 2x3 grid of random numbers
random_grid = torch.rand(2, 3)
# Result:
# tensor([[0.12, 0.89, 0.45],
# [0.67, 0.23, 0.91]])
randn() - Random from a Bell Curve
Numbers that cluster around zero. Most are near 0, few are very big or very small.
# Bell curve random numbers
bell_random = torch.randn(4)
# Result: tensor([-0.52, 1.23, 0.01, -1.87])
# Numbers centered around 0
randint() - Random Whole Numbers
Like rolling a regular dice! You pick the minimum, maximum, and how many.
# 5 random integers from 0 to 9
dice_rolls = torch.randint(0, 10, (5,))
# Result: tensor([3, 7, 1, 9, 4])
# A 2x3 grid of random integers 1-6
dice_grid = torch.randint(1, 7, (2, 3))
# Result:
# tensor([[2, 5, 1],
# [6, 3, 4]])
Quick Summary
| Method | What It Does | Example |
|---|---|---|
zeros(shape) |
All zeros | torch.zeros(3) |
ones(shape) |
All ones | torch.ones(2,2) |
empty(shape) |
Uninitialized | torch.empty(3) |
full(shape, n) |
All same number | torch.full((2,3), 7) |
tensor(data) |
From list/array | torch.tensor([1,2,3]) |
from_numpy(arr) |
From NumPy | torch.from_numpy(arr) |
arange(start,end,step) |
Counting | torch.arange(0,10,2) |
linspace(start,end,n) |
Equal spacing | torch.linspace(0,1,5) |
rand(shape) |
Random 0-1 | torch.rand(3,3) |
randn(shape) |
Bell curve random | torch.randn(4) |
randint(low,high,shape) |
Random integers | torch.randint(1,7,(5,)) |
You Did It!
You now know three ways to create tensors:
- From shape → Tell PyTorch the size, it fills with zeros, ones, or any number
- From sequences → Give PyTorch your existing numbers
- Randomly → Let PyTorch pick numbers for you
These are your LEGO bricks. Next, you’ll learn how to play with them!