【发布时间】:2023-03-05 16:30:01
【问题描述】:
张量/pytorch 的新手。
我有两个二维张量,A 和 B。
A 包含表示分配给某个索引的概率的浮点数。 B 在正确的索引中包含一个单热二元向量。
A
tensor([[0.1, 0.4, 0.5],
[0.5, 0.4, 0.1],
[0.4, 0.5, 0.1]])
B
tensor([[0, 0, 1],
[0, 1, 0],
[0, 0, 1]])
我想找出 A 的任何前 k 个值的索引与 B 中的 one-hot 索引匹配的行数。在这种情况下,k=2。
我的尝试:
tops = torch.topk(A, 2, dim=1)
top_idx = tops.indices
top_2_matches = torch.where((torch.any(top_idx, 1) == B.argmax(dim=1)))
如果操作正确,该示例应该返回一个张量([0, 1]),因为前 2 行有前 2 个匹配项,但我得到 (tensor([1]),) 作为返回。
不知道我在哪里出错了。感谢您的帮助!
【问题讨论】: