megengine.functional.var

var(inp, axis=None, keepdims=False)[源代码]

Calculates the variance of tensor elements over a given axis (or axes).

参数
  • inp (Tensor) – input tensor. Should have a numeric data type.

  • axis (Union[int, Sequence[int], None]) – axis or axes along which variances must be computed. By default, the variance must be computed over the entire tensor. If a sequence of integers, variances must be computed over multiple axes.

  • keepdims (bool) – if True, the reduced axes (dimensions) must be included in the result as singleton dimensions, and, accordingly, the result must be compatible with the input tensor (see 广播机制与规则). Otherwise, if False, the reduced axes (dimensions) must not be included in the result.

返回类型

Tensor

返回

if the variance was computed over the entire tensor, a zero-dimensional tensor containing the variance; otherwise, a non-zero-dimensional tensor containing the variances. The returned tensor must have a data type determined by 类型提升规则.

注解

The variance is the average of the squared deviations from the mean, i.e., var = mean(x), where x = abs(a - a.mean())**2.

实际案例

>>> x = Tensor([[1, 2], [3, 4]])
>>> F.var(x)
Tensor(1.25, device=xpux:0)
>>> x = Tensor([[14, 8, 11, 10], [7, 9, 10, 11], [10, 15, 5, 10]])
>>> F.var(x)
Tensor(6.8333335, device=xpux:0)