torch.std — PyTorch 2.7 documentation (original) (raw)

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

Calculates the standard deviation over the dimensions specified by dim.dim can be a single dimension, list of dimensions, or None to reduce over all dimensions.

The standard deviation (σ\sigma) is calculated as

σ=1max⁡(0, N−δN)∑i=0N−1(xi−xˉ)2\sigma = \sqrt{\frac{1}{\max(0,~N - \delta N)}\sum_{i=0}^{N-1}(x_i-\bar{x})^2}

where xx is the sample set of elements, xˉ\bar{x} is the sample mean, NN is the number of samples and δN\delta N is the correction.

If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see torch.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

Parameters

Keyword Arguments

Example

a = torch.tensor( ... [[ 0.2035, 1.2959, 1.8101, -0.4644], ... [ 1.5027, -0.3270, 0.5905, 0.6538], ... [-1.5745, 1.3330, -0.5596, -0.6548], ... [ 0.1264, -0.5080, 1.6420, 0.1992]]) torch.std(a, dim=1, keepdim=True) tensor([[1.0311], [0.7477], [1.2204], [0.9087]])