pfhedge.nn.functional.topp

pfhedge.nn.functional.topp(input, p, dim=None, largest=True)[source]

Returns the largest \(p * N\) elements of the given input tensor, where \(N\) stands for the total number of elements in the input tensor.

If dim is not given, the last dimension of the input is chosen.

If largest is False then the smallest elements are returned.

A namedtuple of (values, indices) is returned, where the indices are the indices of the elements in the original input tensor.

See also

  • torch.topk(): Returns the k largest elements of the given input tensor along a given dimension.

Parameters
  • input (torch.Tensor) – The input tensor.

  • p (float) – The quantile level.

  • dim (int, optional) – The dimension to sort along.

  • largest (bool, default=True) – Controls whether to return largest or smallest elements.

Returns

Tuple[Tensor, LongTensor] (named tuple)

Examples

>>> from pfhedge.nn.functional import topp
>>>
>>> input = torch.arange(1.0, 6.0)
>>> input
tensor([1., 2., 3., 4., 5.])
>>> topp(input, 3 / 5)
torch.return_types.topk(
values=tensor([5., 4., 3.]),
indices=tensor([4, 3, 2]))