mindspore.mint.std_mean
- mindspore.mint.std_mean(input, dim=None, *, correction=1, keepdim=False)[源代码]
计算tensor在指定维度上的标准差及平均值。
标准差 (\(\sigma\)) 计算如下:
\[\sigma = \sqrt{\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,计算 input 中的所有元素。默认None。
- 关键字参数:
correction (int, 可选) - 样本大小和样本自由度之间的差异。默认采用贝塞尔校正。默认
1。keepdim (bool, 可选) - 输出tensor是否保留维度。如果为
True,则保留缩小的维度,大小为1。否则移除维度。默认False。
- 返回:
两个tensor组成的tuple(std, mean)。
- 异常:
ValueError - dim 超出范围。
- 支持平台:
Ascend
样例:
>>> 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])