mindspore.Tensor.std

Tensor.std(axis=None, ddof=0, keepdims=False) Tensor[source]

For details, please refer to mindspore.ops.std().

Tensor.std(dim=None, *, correction=1, keepdim=False) Tensor[source]

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:

\[\sigma =\sqrt{\frac{1}{N-\delta N}\sum_{j-1}^{N-1}\left(self_{ij}-\overline{x_{i}}\right)^{2}}\]

where \(x\) is the sample set of elements, \(\bar{x}\) is the sample mean, \(N\) is the number of samples and \(\delta N\) is the correction.

Warning

Non-backward-compatible change after version 2.9.0: the std(axis=None, ddof=0, keepdims=False) overload will be removed, and only std(dim=None, *, correction=1, keepdim=False) will be supported (the default value of ddof changes from 0 to 1).

Parameters

dim (None, int, tuple(int), optional) – The dimension or dimensions to reduce. Defaults to None. If None, all dimensions are reduced.

Keyword Arguments
  • correction (int, optional) – The difference between the sample size and sample degrees of freedom. Defaults to 1, which means Bessel's correction.

  • keepdim (bool, optional) – Whether the output tensor has dim retained or not. If True , keep these reduced dimensions and the length is 1. If False, don't keep these dimensions. Defaults to False.

Returns

Tensor, the standard deviation. Suppose the shape of self is \((x_0, x_1, ..., x_R)\):

  • If dim is () and keepdim is set to False , returns a 0-D Tensor, indicating the standard deviation of all elements in self.

  • If dim is int, e.g. 1 and keepdim is set to False , then the returned Tensor has shape \((x_0, x_2, ..., x_R)\).

  • If dim is tuple(int) or list(int), e.g. (1, 2) and keepdim is set to False , then the returned Tensor has shape \((x_0, x_3, ..., x_R)\).

Raises
  • TypeError – If self is not a Tensor.

  • TypeError – If self is not in bfloat16, float16, float32.

  • TypeError – If dim is not one of the followings: None, int, tuple.

  • TypeError – If correction is not an int.

  • TypeError – If keepdim is not a bool.

  • ValueError – If dim is out of range \([-self.ndim, self.ndim)\).

Supported Platforms:

Ascend

Examples

>>> import numpy as np
>>> from mindspore import mint, Tensor
>>> input = Tensor(np.array([[1, 2, 3], [-1, 1, 4]]).astype(np.float32))
>>> output = input.std(dim=1, correction=1, keepdim=False)
>>> print(output)
[1.      2.5166113]