mindspore.mint.std_mean

View Source On AtomGit
mindspore.mint.std_mean(input, dim=None, *, correction=1, keepdim=False)[source]

Compute the standard deviation and mean of the tensor along a specified dimension.

The standard deviation (\(\sigma\)) is calculated as:

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

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

Warning

This is an experimental API that is subject to change or deletion.

Parameters
  • input (Tensor) – The input tensor.

  • dim (Union[int, tuple(int), list(int)], optional) – Specify the dimensions for computation. If None , compute all elements in the input . Default None .

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

  • keepdim (bool, optional) – Whether the output tensor has dim retained. If True , retain the reduced dimension with a size of 1. Otherwise, remove the dimensions. Default False .

Returns

Tuple(std, mean) of 2 tensors.

Raises

ValueError – If dim is out of range.

Supported Platforms:

Ascend

Examples

>>> import mindspore
>>> input = mindspore.tensor([[1.0, 2.0, 3.0, 4.0], [-1.0, 1.0, 4.0, -10.0]])
>>> output_std, output_mean = mindspore.mint.std_mean(input)
>>> output_std
Tensor(shape=[], dtype=Float32, value= 4.56696)
>>> output_mean
Tensor(shape=[], dtype=Float32, value= 0.5)
>>> output_std, output_mean = mindspore.mint.std_mean(input, dim=0)
>>> output_std
Tensor(shape=[4], dtype=Float32, value= [ 1.41421354e+00,  7.07106769e-01,  7.07106769e-01,  9.89949512e+00])
>>> output_mean
Tensor(shape=[4], dtype=Float32, value= [ 0.00000000e+00,  1.50000000e+00,  3.50000000e+00, -3.00000000e+00])
>>> output_std, output_mean = mindspore.mint.std_mean(input, dim=1, keepdim=True)
>>> output_std
Tensor(shape=[2, 1], dtype=Float32, value=
[[ 1.29099441e+00],
 [ 6.02771425e+00]])
>>> output_mean
Tensor(shape=[2, 1], dtype=Float32, value=
[[ 2.50000000e+00],
 [-1.50000000e+00]])
>>> output_std, output_mean = mindspore.mint.std_mean(input, dim=1, correction=2)
>>> output_std
Tensor(shape=[2], dtype=Float32, value= [ 1.58113885e+00,  7.38241148e+00])
>>> output_mean
Tensor(shape=[2], dtype=Float32, value= [ 2.50000000e+00, -1.50000000e+00])