当前位置:网站首页>Pytorch selects the first k maximum (minimum) values and their indexes in the data

Pytorch selects the first k maximum (minimum) values and their indexes in the data

2022-04-23 20:48:00 NuerNuer

Select the maximum value and its index , Everyone knows how to use max(),argmax() function .

So how to return to the previous k It's a maximum , This is when we calculate topK Accuracy is necessary :

stay torch in , We can use sort Function to implement :

a, idx1 = torch.sort(data, descending=True)#descending by alse, Ascending , by True, Descending 
idx = idx1[:k]

Return:
a: Ordered data
idx1: The index corresponding to the sorted data

So just set k Size , You can intercept the front k An index with a maximum value . If the data here is tensor Then use torch, if list or ndarray, It can be used numpy.

版权声明
本文为[NuerNuer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210545522718.html