Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Function

torch.randn

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.

How it works

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.

Syntax

torch.randn(*size, *, generator=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False, pin_memory=False) → Tensor

Parameters

NameTypeDescription
sizeint...Dimensions of the output; provide integers separately or as a list or tuple.
generatortorch.GeneratorOptional pseudorandom number generator.
outTensorOptional destination tensor.
dtypetorch.dtypeOptional returned-tensor data type; omitted values use the global default dtype.
layouttorch.layoutOptional returned-tensor layout; default is torch.strided.
devicetorch.deviceOptional allocation device.
requires_gradboolWhether autograd records operations on the result; default is False.
pin_memoryboolWhether to allocate CPU output in pinned memory; default is False.

Returns

Tensor filled with random values sampled from a normal distribution with mean 0 and variance 1.

Key points

  • Values follow the standard normal distribution: zero mean and unit variance.
  • The required size describes every output dimension.
  • Dimensions can be supplied individually or packed in a list or tuple.
  • The generator option lets callers choose the pseudorandom generator used for sampling.
  • requires_grad enables autograd recording for operations involving the result.
  • The device option controls where the result is allocated.

Common mistakes

  • pin_memory is applicable only when the returned tensor is on the CPU.
  • If dtype is omitted, PyTorch derives it from the current global default dtype rather than always choosing a fixed type.

Example: Create a standard-normal tensor

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.

When to use it

Normal-distributed tensor creation

Initialize model parameters or temporary activations with Gaussian-valued tensors.

Synthetic multidimensional inputs

Create test inputs with explicit batch, channel, or feature dimensions.

Controlled tensor placement

Allocate samples directly with a chosen dtype or device.

Interview questions

QWhat distribution does torch.randn use, and how is its shape specified?

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.

QHow can callers provide the output dimensions?

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.

QWhich options control autograd and placement?

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.rand
  • torch.rand_like
  • torch.randint
  • torch.randperm
  • torch.Generator

Source: torch.randn — PyTorch 2.14 documentation. Checked 2026-10-16.