Calculating the top k accuracy using Sklearn

I want to calculate the top k accuracy using the sklearn implementation:I was wondring if using this is correct

output = model(data)

target_top_numpy=target.cpu().detach().numpy()

predicted_top_numpy=output.cpu().detach().numpy()

top2=top2+top_k_accuracy_score(target_top_numpy, predicted_top_numpy, k=2,normalize=False,labels=range(11))

my question much more precisly should I apply softmax function on the output before fiding it to the top k score function or it is working fine this way?

From the docs:

y_score array-like of shape (n_samples,) or (n_samples, n_classes)
Target scores. These can be either probability estimates or non-thresholded decision values (as returned by decision_function on some classifiers). …

If you copy/paste their example and e.g. scale or shift y_score, the results will still be the same so you wouldn’t need to use softmax on your model output.

1 Like