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

PyTorch · Function

torch.std

Calculates standard deviation over selected tensor dimensions. By default it uses Bessel’s correction; choose correction=0 for population standard deviation and keepdim=True to retain reduced axes.

How it works

torch.std reduces the selected dimensions by computing standard deviation with a denominator based on the number of values minus correction. Set dim to an integer or tuple, use correction to choose sample or population behavior, and use keepdim to preserve reduced axes as size-one dimensions.

Syntax

torch.std(input, dim=None, *, correction=1, keepdim=False, out=None) → Tensor

Parameters

NameTypeDescription
inputTensorInput tensor whose standard deviation is calculated.
dimint or tuple of ints, optionalDimension or dimensions to reduce; None reduces all dimensions.
correctionintDifference between sample size and sample degrees of freedom; defaults to 1.
keepdimbool, optionalWhether reduced dimensions are retained as size-one dimensions.
outTensor, optionalOptional tensor in which to write the result.

Returns

Tensor containing the standard deviation over the selected dimensions.

Key points

  • Use torch.std to calculate standard deviation along selected tensor dimensions.
  • dim accepts one dimension, multiple dimensions, or None for a full-tensor reduction.
  • The documented computation uses the square root of corrected mean squared deviations from the mean.
  • keepdim=True preserves rank and changes every reduced axis to length one.
  • An optional out tensor can receive the result.

Common mistakes

  • The default is sample standard deviation, not population standard deviation; pass correction=0 for population standard deviation.
  • With keepdim=False, reduced dimensions disappear, which can change the shape expected by later operations.

Example: Standard deviation by row

import torch

x = torch.tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
row_std = torch.std(x, dim=1, correction=0, keepdim=True)
print(row_std)

Computes each row's population standard deviation while retaining the reduced dimension.

When to use it

Measure dispersion along a feature or batch dimension.

Feature scaling

Summarize variation across all elements when dim is None.

Tensor reduction

Retain reduced axes with keepdim=True for shape-compatible downstream operations.

Broadcasting

Select sample or population standard deviation through correction.

Statistical convention

Interview questions

QWhat does torch.std reduce?

torch.std computes standard deviation across the dimensions selected by dim. If dim is None, it reduces all dimensions.

Follow-up: How would you preserve the reduced axes for broadcasting? Set keepdim=True, which leaves each reduced axis with size 1.

QWhat does correction control?

correction is subtracted from the number of reduced elements in the denominator. Its default is 1, Bessel’s correction, so correction=0 gives population standard deviation.

Follow-up: What happens when the reduced count is no greater than correction? The denominator is clamped through max(0, N - correction), as shown by the documented formula.

QHow do dim and keepdim affect output shape?

dim may be one dimension, a tuple of dimensions, or None. keepdim=True retains reduced dimensions as size-one axes; otherwise those axes are squeezed.

Follow-up: How can the result remain aligned with the input rank? Use keepdim=True when subsequent tensor operations rely on broadcasting or positional dimensions.

  • torch.Tensor.std
  • torch.std_mean
  • torch.squeeze

Source: torch.std — PyTorch main documentation. Checked 2026-10-16.