Rate this Page

torch.kthvalue#

torch.kthvalue(input,k,dim=None,keepdim=False,*,out=None)#

Returns a namedtuple(values,indices) wherevalues is thek thsmallest element of each row of theinput tensor in the given dimensiondim. Andindices is the index location of each element found.

Ifdim is not given, the last dimension of theinput is chosen.

Ifkeepdim isTrue, both thevalues andindices tensorsare the same size asinput, except in the dimensiondim wherethey are of size 1. Otherwise,dim is squeezed(seetorch.squeeze()), resulting in both thevalues andindices tensors having 1 fewer dimension than theinput tensor.

Note

Wheninput is a CUDA tensor and there are multiple validk th values, this function may nondeterministically returnindices for any of them.

Parameters
  • input (Tensor) – the input tensor.

  • k (int) – k for the k-th smallest element

  • dim (int,optional) – the dimension to find the kth value along

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

Keyword Arguments

out (tuple,optional) – the output tuple of (Tensor, LongTensor)can be optionally given to be used as output buffers

Example:

>>>x=torch.arange(1.,6.)>>>xtensor([ 1.,  2.,  3.,  4.,  5.])>>>torch.kthvalue(x,4)torch.return_types.kthvalue(values=tensor(4.), indices=tensor(3))>>>x=torch.arange(1.,7.).resize_(2,3)>>>xtensor([[ 1.,  2.,  3.],        [ 4.,  5.,  6.]])>>>torch.kthvalue(x,2,0,True)torch.return_types.kthvalue(values=tensor([[4., 5., 6.]]), indices=tensor([[1, 1, 1]]))