Normal-distributed tensor creation
Initialize model parameters or temporary activations with Gaussian-valued tensors.
PyTorch · Function
Creates a tensor of a requested shape with values sampled from the standard normal distribution. Shape dimensions may be separate arguments or a list/tuple, while keyword arguments control dtype, device, autograd, storage, and randomness.
torch.randn creates a tensor with the requested shape and fills it with independently sampled values from a normal distribution centered at zero with variance one. Shape dimensions may be passed as separate integer arguments or as one sequence; optional keywords control randomness, output storage, dtype, layout, device, autograd tracking, and CPU pinned memory.
torch.randn(*size, *, generator=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False, pin_memory=False) → Tensor| Name | Type | Description |
|---|---|---|
size | int... | Dimensions of the output; provide integers separately or as a list or tuple. |
generator | torch.Generator | Optional pseudorandom number generator. |
out | Tensor | Optional destination tensor. |
dtype | torch.dtype | Optional returned-tensor data type; omitted values use the global default dtype. |
layout | torch.layout | Optional returned-tensor layout; default is torch.strided. |
device | torch.device | Optional allocation device. |
requires_grad | bool | Whether autograd records operations on the result; default is False. |
pin_memory | bool | Whether to allocate CPU output in pinned memory; default is False. |
Tensor filled with random values sampled from a normal distribution with mean 0 and variance 1.
import torch
x = torch.randn(2, 3, dtype=torch.float32)
print(x.shape)
print(x.mean().item())Creates a 2×3 CPU tensor whose entries are sampled from the standard normal distribution, then inspects its shape and sample mean.
Initialize model parameters or temporary activations with Gaussian-valued tensors.
Create test inputs with explicit batch, channel, or feature dimensions.
Allocate samples directly with a chosen dtype or device.
It returns a tensor filled with samples from the standard normal distribution: mean 0 and variance 1. The output shape comes from the supplied size arguments or size sequence.
Follow-up: How would you generate a tensor matching another tensor's shape? Use torch.randn_like(input), which bases the output size on input and also provides related dtype, layout, and device controls.
size accepts a variable number of integers or a collection such as a list or tuple. The function returns a Tensor with that shape.
Follow-up: What if the target dimensions are already stored dynamically? Pass the list or tuple directly as the size argument.
requires_grad controls whether autograd records operations on the returned tensor, and its default is False. device selects where the tensor is allocated, while dtype selects its data type.
Follow-up: When is pin_memory useful? It requests pinned allocation for CPU tensors, commonly to support transfers to accelerators; the documentation limits it to CPU tensors.
torch.randtorch.rand_liketorch.randinttorch.randpermtorch.Generator