Initialising weights, masks, ranges and test inputs all start with these factory functions.
Besides torch.tensor(data), PyTorch has factory functions that create tensors of a given shape directly. They mirror NumPy:
torch.zeros(2, 3),torch.ones(...),torch.full((2, 2), 7.0)fill with a constant.torch.arange(start, end, step)gives evenly spaced integers (end excluded);torch.linspace(a, b, n)givesnfloats including both ends.torch.eye(n)is the identity matrix.torch.rand(uniform in [0, 1)),torch.randn(standard normal) andtorch.randint(low, high, shape)produce random values.
Every factory accepts keyword arguments dtype= and device=, so you can create a tensor directly where it is needed instead of creating it on the CPU and copying it.
The *_like variants (torch.zeros_like(x), torch.randn_like(x)) copy the shape, dtype and device of an existing tensor, which keeps code device-agnostic.
Random numbers come from a global generator. Call torch.manual_seed(0) at the start of a script to make runs repeatable. For finer control you can create a torch.Generator, seed it, and pass generator=g to a random function.
Prefer factories over building big Python lists and converting them: they are faster and never go through Python objects.
Example
import torch
torch.manual_seed(0) # repeatable random numbers
print(torch.zeros(2, 3).shape) # torch.Size([2, 3])
print(torch.arange(0, 10, 3)) # tensor([0, 3, 6, 9])
print(torch.linspace(0, 1, 5)) # tensor([0.0000, 0.2500, 0.5000, 0.7500, 1.0000])
print(torch.full((2,), 7.0)) # tensor([7., 7.])
print(torch.eye(2)) # tensor([[1., 0.], [0., 1.]])
print(torch.ones(2, dtype=torch.int32)) # tensor([1, 1], dtype=torch.int32)
x = torch.randn(3, 4) # standard normal samples
print(torch.zeros_like(x).shape) # torch.Size([3, 4])
print(torch.randint(0, 10, (5,)).dtype) # torch.int64Remember
- Factories take a shape plus optional
dtype=anddevice= *_likecopies shape, dtype and device from another tensortorch.manual_seedmakes random tensors repeatable
Watch out for
- Forgetting that
arangeexcludes the end value whilelinspaceincludes it.