mindspore.mint.var_mean

查看源文件
mindspore.mint.var_mean(input, dim=None, *, correction=1, keepdim=False)[源代码]

计算tensor在指定维度上的方差及平均值。

方差 (\(\sigma ^2\)) 计算如下:

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

其中 \(x\) 表示用来计算方差的样本集, \(\bar{x}\) 表示样本的均值, \(N\) 表示样本的数量, \(\delta N\)correction 的值。

警告

这是一个实验性API,后续可能修改或删除。

参数:
  • input (Tensor) - 输入tensor。

  • dim (Union[int, tuple(int), list(int)], 可选) - 指定计算维度。默认 None

关键字参数:
  • correction (int, 可选) - 样本大小和样本自由度之间的差异。默认采用贝塞尔校正,取值 1

  • keepdim (bool, 可选) - 输出tensor是否保留维度。默认 False

返回:

两个tensor组成的tuple(var, mean)。

支持平台:

Ascend

样例:

>>> 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]]))