mean

paddle. mean ( x: Tensor, axis: int | Sequence[int] | None = None, keepdim: bool = False, name: str | None = None, *, dtype: DTypeLike | None = None, out: Tensor | None = None ) Tensor [source]

Computes the mean of the input tensor’s elements along axis.

Parameters
  • x (Tensor) – The input Tensor with data type bool, bfloat16, float16, float32, float64, int32, int64, complex64, complex128. alias: input

  • axis (int|list|tuple|None, optional) – The axis along which to perform mean calculations. axis should be int, list(int) or tuple(int). If axis is a list/tuple of dimension(s), mean is calculated along all element(s) of axis . axis or element(s) of axis should be in range [-D, D), where D is the dimensions of x . If axis or element(s) of axis is less than 0, it works the same way as \(axis + D\) . If axis is None, mean is calculated over all elements of x. Default is None. alias: dim

  • keepdim (bool, optional) – Whether to reserve the reduced dimension(s) in the output Tensor. If keepdim is True, the dimensions of the output Tensor is the same as x except in the reduced dimensions(it is of size 1 in this case). Otherwise, the shape of the output Tensor is squeezed in axis . Default is False.

  • name (str|None, optional) – Name for the operation (optional, default is None). For more information, please refer to api_guide_Name.

  • dtype (str) – The desired data type of returned tensor. Default: None.

  • out (Tensor|None, optional) – The output tensor. Default: None.

Returns

Tensor, results of average along axis of x, with the same data type as x.

Examples

>>> import paddle

>>> x = paddle.to_tensor([[[1., 2., 3., 4.],
...                        [5., 6., 7., 8.],
...                        [9., 10., 11., 12.]],
...                       [[13., 14., 15., 16.],
...                        [17., 18., 19., 20.],
...                        [21., 22., 23., 24.]]])
>>> out1 = paddle.mean(x)
>>> print(out1.numpy())
12.5
>>> out2 = paddle.mean(x, axis=-1)
>>> print(out2.numpy())
[[ 2.5  6.5 10.5]
 [14.5 18.5 22.5]]
>>> out3 = paddle.mean(x, axis=-1, keepdim=True)
>>> print(out3.numpy())
[[[ 2.5]
  [ 6.5]
  [10.5]]
 [[14.5]
  [18.5]
  [22.5]]]
>>> out4 = paddle.mean(x, axis=[0, 2])
>>> print(out4.numpy())
[ 8.5 12.5 16.5]
>>> out5 = paddle.mean(x, dtype='float64')
>>> out5
Tensor(shape=[], dtype=float64, place=Place(gpu:0), stop_gradient=True,
    12.50000000)