paddle. mode ( x, axis=- 1, keepdim=False, name=None ) [source]

This OP is used to find values and indices of the modes at the optional axis.

  • x (Tensor) – Tensor, an input N-D Tensor with type float32, float64, int32, int64.

  • axis (int, optional) – Axis to compute indices along. The effective range is [-R, R), where R is x.ndim. when axis < 0, it works the same way as axis + R. Default is -1.

  • keepdim (bool, optional) – Whether to keep the given axis in output. If it is True, the dimensions will be same as input x and with size one in the axis. Otherwise the output dimentions is one fewer than x since the axis is squeezed. Default is False.

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


tuple(Tensor), return the values and indices. The value data type is the same as the input x. The indices data type is int64.


import paddle

tensor = paddle.to_tensor([[[1,2,2],[2,3,3]],[[0,5,5],[9,9,0]]], dtype=paddle.float32)
res = paddle.mode(tensor, 2)
# (Tensor(shape=[2, 2], dtype=float32, place=CUDAPlace(0), stop_gradient=True,
#   [[2., 3.],
#    [5., 9.]]), Tensor(shape=[2, 2], dtype=int64, place=CUDAPlace(0), stop_gradient=True,
#   [[1, 1],
#    [1, 0]]))