all_max = tf.convert_to_tensor([[4, 2, 3], [3, 4, 5]], dtype=tf.float32)
如何[3,4,5]
从张量数组中获取元素的索引all_max
?
在列表中,我们只是list.index(element)
用来获取列表中存在的元素的索引。
谢谢
all_max = tf.convert_to_tensor([[4, 2, 3], [3, 4, 5]], dtype=tf.float32)
如何[3,4,5]
从张量数组中获取元素的索引all_max
?
在列表中,我们只是list.index(element)
用来获取列表中存在的元素的索引。
谢谢
我发现这个:https://www.py4u.net/discuss/147615 为我工作
要查找 2d/3d 张量的元素索引,请将其转换为 1d #ie example.view(number of elements)
例子:
mat=torch.tensor([[1,2],[4,3])
#to find index of 2
five = 2
mat=mat.view(4)
numb_of_col = 4
for o in range(numb_of_col):
if mat[o] == five:
print(torch.tensor([o]))