mindspore.mint.var_mean

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

Compute the variance and the mean of the tensor along a specified dimension.

The variance (\(\sigma ^2\)) is calculated as:

\[\sigma ^2 = \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. Default None.

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

  • keepdim (bool, optional) – Whether the output tensor has dim retained. Default False.

Returns

Tuple(var, mean) of 2 tensors.

Supported Platforms:

Ascend

Examples

>>> import mindspore
>>> input = mindspore.tensor([[1, 3, 4, 2], [4, 2, 5, 3], [5, 4, 2, 3]], mindspore.float32)
>>> output = mindspore.mint.var_mean(input, 0, correction=1)
>>> print(output)
(Tensor(shape=[4], dtype=Float32, value=
[ 4.33333302e+00,  1.00000000e+00,  2.33333349e+00,  3.33333313e-01]),
Tensor(shape=[4], dtype=Float32, value=
[ 3.33333349e+00,  3.00000000e+00,  3.66666675e+00,  2.66666675e+00]))
>>> output = mindspore.mint.var_mean(input, 1, correction=2, keepdim=True)
>>> print(output)
(Tensor(shape=[3, 1], dtype=Float32, value=
[[ 2.50000000e+00],
 [ 2.50000000e+00],
 [ 2.50000000e+00]]), Tensor(shape=[3, 1], dtype=Float32, value=
 [[ 2.50000000e+00],
  [ 3.50000000e+00],
  [ 3.50000000e+00]]))