mindspore.ops.var
- mindspore.ops.var(input, axis=None, ddof=0, keepdims=False)[source]
Compute the variance of the tensor along a specified axis.
- Parameters
input (Tensor[Number]) – The input tensor.
axis (Union[int, tuple(int)], optional) – Specify the axis for computation. If
None, compute all elements in the input . DefaultNone.ddof (Union[int, bool], optional) –
Means Delta Degrees of Freedom. Default
0.If ddof is an integer, the divisor used in calculations is \(N - ddof\), where \(N\) represents the number of elements.
If ddof is a boolean,
TrueandFalsecorrespond to when ddof is an integer1and0respectively.If ddof is 0, 1, True or False, the supported device is only Ascend and CPU. In other cases, the supported device is Ascend, GPU and CPU.
keepdims (bool, optional) – Whether the output tensor has dim retained. Default
False.
- Returns
Tensor
- Supported Platforms:
AscendGPUCPU
Examples
>>> 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]]