gpt4 book ai didi

python - 如何在pytorch中实现tf.nn.top_k

转载 作者:行者123 更新时间:2023-12-04 08:37:26 28 4
gpt4 key购买 nike

我想在 pytorch 中实现 tf.nn.in_top_k。这是tf.nn.in_top_k的链接,

tf.math.in_top_k(
targets, predictions, k, name=None
)

它将 k 处的精度计算为 bool Tensor,并将返回 bool 类型的 Tensor。

tf.nn.in_top_k

不知道pytorch中有没有类似的api?

最佳答案

据我所知,pytorch 中没有内置等效的 in_top_k 函数。写一个比较简单。例如

def in_top_k(targets, preds, k):
topk = preds.topk(k)[1]
return (targets.unsqueeze(1) == topk).any(dim=1)

关于python - 如何在pytorch中实现tf.nn.top_k,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64734480/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com