mindspore.ops.var
- mindspore.ops.var(input, axis=None, ddof=0, keepdims=False)[源代码]
计算tensor在指定轴上的方差。
- 参数:
input (Tensor[Number]) - 输入tensor。
axis (Union[int, tuple(int)],可选) - 指定计算轴。如果为
None
,计算 input 中的所有元素。默认None
。ddof (Union[int, bool],可选) - δ自由度。默认
0
。如果为整数,计算中使用的除数是
,其中 表示元素的数量。如果为bool值,
True
与False
分别对应ddof为整数时的1
与0
。如果取值为0、1、True或False,支持的平台只有 Ascend 和 CPU 。其他情况下,支持平台是 Ascend 、 GPU 和 CPU 。
keepdims (bool,可选) - 输出tensor是否保留维度。默认
False
。
- 返回:
Tensor
- 支持平台:
Ascend
GPU
CPU
样例:
>>> import mindspore >>> input = mindspore.tensor([[1., 3, 4, 2], ... [4, 2, 5, 3], ... [5, 4, 2, 3]]) >>> # case 1: By default, compute the variance of all elements. >>> output = mindspore.ops.var(input) >>> print(output) 1.4722221 >>> >>> # case 2: Compute the variance along axis 0. >>> output = mindspore.ops.var(input, axis=0) >>> print(output) [2.8888888 0.6666667 1.5555557 0.2222222] >>> >>> # case 3: If keepdims=True, the output shape will be same of that of the input. >>> output = mindspore.ops.var(input, axis=0, keepdims=True) >>> print(output) [[2.8888888 0.6666667 1.5555557 0.2222222]] >>> >>> # case 4: If ddof=1: >>> output = mindspore.ops.var(input, axis=0, keepdims=True, ddof=1) >>> print(output) [[4.3333335 1. 2.3333335 0.3333333]] >>> >>> # case 5: If ddof=True, same as ddof=1: >>> output = mindspore.ops.var(input, axis=0, keepdims=True, ddof=True) >>> print(output) [[4.3333335 1. 2.3333335 0.3333333]] >>> >>> # case 6: If ddof=False, same as ddof=0: >>> output = mindspore.ops.var(input, axis=0, keepdims=True, ddof=False) >>> print(output) [[2.8888888 0.6666667 1.5555557 0.2222222]]