megengine.functional.mean

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

Calculates the mean 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 means must be computed. By default, the mean must be computed over the entire tensor. If a sequence of integers, means 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 mean was computed over the entire tensor, a zero-dimensional tensor containing the mean; otherwise, a non-zero-dimensional tensor containing the means. The returned tensor must have a data type determined by 类型提升规则.

Special Cases

Let N equal the number of elements over which to compute the mean.

  • If N is 0, the mean is NaN.

  • If \(x_i\) is NaN, the mean is NaN (i.e., NaN values propagate).

实际案例

>>> F.mean(Tensor([1, 2, 3]))
Tensor(2.0, device=xpux:0)
>>> import numpy as np
>>> F.mean(Tensor([1, np.nan, 3]))
Tensor(nan, device=xpux:0)

Along an axis:

>>> F.mean(Tensor([[1, 2, 3], [4, 5, 6]]), axis=0)
Tensor([2.5 3.5 4.5], device=xpux:0)
>>> F.mean(Tensor([[1, 2, 3], [4, 5, 6]]), axis=1)
Tensor([2. 5.], device=xpux:0)