oneflow.in_top_k

oneflow.in_top_k(targets, predictions, k)Tensor

Says whether the targets are in the top K predictions.

Parameters
  • targets (Tensor) – the target tensor of type int32 or int64.

  • predictions (Tensor) – the predictions tensor of type float32 .

  • k (int) – Number of top elements to look at for computing precision.

Returns

A Tensor of type bool. Computed Precision at k as a bool Tensor.

Return type

oneflow.Tensor

For example:

>>> import oneflow as flow
>>> import numpy as np
>>> targets1 = flow.tensor(np.array([3, 1]), dtype=flow.int32)
>>> predictions1 = flow.tensor(np.array([[0.0, 1.0, 2.0, 3.0], [3.0, 2.0, 1.0, 0.0],]), dtype=flow.float32)
>>> out1 = flow.in_top_k(targets1, predictions1, k=1)
>>> out1
tensor([ True, False], dtype=oneflow.bool)
>>> out2 = flow.in_top_k(targets1, predictions1, k=2)
>>> out2
tensor([True, True], dtype=oneflow.bool)
>>> targets2 = flow.tensor(np.array([3, 1]), dtype=flow.int32, device=flow.device('cuda'))
>>> predictions2 = flow.tensor(np.array([[0.0, 1.0, 2.0, 3.0], [3.0, 2.0, 1.0, 0.0],]), dtype=flow.float32, device=flow.device('cuda'))
>>> out3 = flow.in_top_k(targets2, predictions2, k=1)
>>> out3
tensor([ True, False], device='cuda:0', dtype=oneflow.bool)