Pytorch Tensor如何获取特定值的索引
How Pytorch Tensor get the index of specific value
有了 python 个列表,我们可以做:
a = [1, 2, 3]
assert a.index(2) == 1
pytorch 张量如何直接找到 .index()
?
我认为没有从 list.index()
到 pytorch 函数的直接翻译。但是,您可以使用 tensor==number
然后使用 nonzero()
函数获得类似的结果。例如:
t = torch.Tensor([1, 2, 3])
print ((t == 2).nonzero(as_tuple=True)[0])
这段代码returns
1
[torch.LongTensor of size 1x1]
可以通过如下转换为numpy来完成
import torch
x = torch.range(1,4)
print(x)
===> tensor([ 1., 2., 3., 4.])
nx = x.numpy()
np.where(nx == 3)[0][0]
===> 2
import torch
x_data = variable(torch.Tensor([[1.0], [2.0], [3.0]]))
print(x_data.data[0])
>>tensor([1.])
对于浮点张量,我用这个来获取张量中元素的索引
print((torch.abs((torch.max(your_tensor).item()-your_tensor))<0.0001).nonzero())
这里我想获取max_value在float tensor中的索引,你也可以这样输入你的值来获取tensor中任意元素的索引
print((torch.abs((YOUR_VALUE-your_tensor))<0.0001).nonzero())
用于查找 1d 中元素的索引 tensor/array
例子
mat=torch.tensor([1,8,5,3])
找到索引 5
five=5
numb_of_col=4
for o in range(numb_of_col):
if mat[o]==five:
print(torch.tensor([o]))
查找 2d/3d 张量的元素索引将其转换为 1d
#ie example.view(元素个数)
示例
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]))
对于多维张量你可以这样做:
(tensor == target_value).nonzero(as_tuple=True)
生成的张量的形状为 number_of_matches x tensor_dimension
。例如,假设 tensor
是一个 3 x 4
张量(这意味着维度为 2),结果将是一个二维张量,其中包含行中匹配项的索引。
tensor = torch.Tensor([[1, 2, 2, 7], [3, 1, 2, 4], [3, 1, 9, 4]])
(tensor == 2).nonzero(as_tuple=False)
>>> tensor([[0, 1],
[0, 2],
[1, 2]])
根据其他人的回答:
t = torch.Tensor([1, 2, 3])
print((t==1).nonzero().item())
已经给出的答案很好,但是当我在没有匹配的情况下尝试时,它们无法处理。为此,请参阅:
def index(tensor: Tensor, value, ith_match:int =0) -> Tensor:
"""
Returns generalized index (i.e. location/coordinate) of the first occurence of value
in Tensor. For flat tensors (i.e. arrays/lists) it returns the indices of the occurrences
of the value you are looking for. Otherwise, it returns the "index" as a coordinate.
If there are multiple occurences then you need to choose which one you want with ith_index.
e.g. ith_index=0 gives first occurence.
Reference:
:return:
"""
# bool tensor of where value occurred
places_where_value_occurs = (tensor == value)
# get matches as a "coordinate list" where occurence happened
matches = (tensor == value).nonzero() # [number_of_matches, tensor_dimension]
if matches.size(0) == 0: # no matches
return -1
else:
# get index/coordinate of the occurence you want (e.g. 1st occurence ith_match=0)
index = matches[ith_match]
return index
归功于这个出色的答案:
x = torch.Tensor([11, 22, 33, 22])
print((x==22).nonzero().squeeze())
tensor([1, 3])
有了 python 个列表,我们可以做:
a = [1, 2, 3]
assert a.index(2) == 1
pytorch 张量如何直接找到 .index()
?
我认为没有从 list.index()
到 pytorch 函数的直接翻译。但是,您可以使用 tensor==number
然后使用 nonzero()
函数获得类似的结果。例如:
t = torch.Tensor([1, 2, 3])
print ((t == 2).nonzero(as_tuple=True)[0])
这段代码returns
1
[torch.LongTensor of size 1x1]
可以通过如下转换为numpy来完成
import torch
x = torch.range(1,4)
print(x)
===> tensor([ 1., 2., 3., 4.])
nx = x.numpy()
np.where(nx == 3)[0][0]
===> 2
import torch
x_data = variable(torch.Tensor([[1.0], [2.0], [3.0]]))
print(x_data.data[0])
>>tensor([1.])
对于浮点张量,我用这个来获取张量中元素的索引
print((torch.abs((torch.max(your_tensor).item()-your_tensor))<0.0001).nonzero())
这里我想获取max_value在float tensor中的索引,你也可以这样输入你的值来获取tensor中任意元素的索引
print((torch.abs((YOUR_VALUE-your_tensor))<0.0001).nonzero())
用于查找 1d 中元素的索引 tensor/array 例子
mat=torch.tensor([1,8,5,3])
找到索引 5
five=5
numb_of_col=4
for o in range(numb_of_col):
if mat[o]==five:
print(torch.tensor([o]))
查找 2d/3d 张量的元素索引将其转换为 1d #ie example.view(元素个数)
示例
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]))
对于多维张量你可以这样做:
(tensor == target_value).nonzero(as_tuple=True)
生成的张量的形状为 number_of_matches x tensor_dimension
。例如,假设 tensor
是一个 3 x 4
张量(这意味着维度为 2),结果将是一个二维张量,其中包含行中匹配项的索引。
tensor = torch.Tensor([[1, 2, 2, 7], [3, 1, 2, 4], [3, 1, 9, 4]])
(tensor == 2).nonzero(as_tuple=False)
>>> tensor([[0, 1],
[0, 2],
[1, 2]])
根据其他人的回答:
t = torch.Tensor([1, 2, 3])
print((t==1).nonzero().item())
已经给出的答案很好,但是当我在没有匹配的情况下尝试时,它们无法处理。为此,请参阅:
def index(tensor: Tensor, value, ith_match:int =0) -> Tensor:
"""
Returns generalized index (i.e. location/coordinate) of the first occurence of value
in Tensor. For flat tensors (i.e. arrays/lists) it returns the indices of the occurrences
of the value you are looking for. Otherwise, it returns the "index" as a coordinate.
If there are multiple occurences then you need to choose which one you want with ith_index.
e.g. ith_index=0 gives first occurence.
Reference:
:return:
"""
# bool tensor of where value occurred
places_where_value_occurs = (tensor == value)
# get matches as a "coordinate list" where occurence happened
matches = (tensor == value).nonzero() # [number_of_matches, tensor_dimension]
if matches.size(0) == 0: # no matches
return -1
else:
# get index/coordinate of the occurence you want (e.g. 1st occurence ith_match=0)
index = matches[ith_match]
return index
归功于这个出色的答案:
x = torch.Tensor([11, 22, 33, 22])
print((x==22).nonzero().squeeze())
tensor([1, 3])