Rate this Page

torch.mode#

torch.mode(input,dim=-1,keepdim=False,*,out=None)#

Returns a namedtuple(values,indices) wherevalues is the modevalue of each row of theinput tensor in the given dimensiondim, i.e. a value which appears most oftenin that row, andindices is the index location of each mode value found.

By default,dim is the last dimension of theinput tensor.

Ifkeepdim isTrue, the output tensors are of the same size asinput except in the dimensiondim where they are of size 1.Otherwise,dim is squeezed (seetorch.squeeze()), resultingin the output tensors having 1 fewer dimension thaninput.

Note

This function is not defined fortorch.cuda.Tensor yet.

Parameters
  • input (Tensor) – the input tensor.

  • dim (int,optional) – the dimension to reduce.

  • keepdim (bool,optional) – whether the output tensor hasdim retained or not. Default:False.

Keyword Arguments

out (tuple,optional) – the result tuple of two output tensors (values, indices)

Example:

>>>b=torch.tensor([[0,0,0,2,0,0,2],...[0,3,0,0,2,0,1],...[2,2,2,0,0,0,3],...[2,2,3,0,1,1,0],...[1,1,0,0,2,0,2]])>>>torch.mode(b,0)torch.return_types.mode(values=tensor([0, 2, 0, 0, 0, 0, 2]),indices=tensor([1, 3, 4, 4, 2, 4, 4]))