1
我正试图在PyTorch张量中找到不同的值。
有没有一些有效的方法来复制Tensorflow的unique op?PyTorch中的唯一张量值
我正试图在PyTorch张量中找到不同的值。
有没有一些有效的方法来复制Tensorflow的unique op?PyTorch中的唯一张量值
要做到这一点的最好方法(最简单的方法)将转换为numpy并使用numpy的内置unique
函数。像这样。
def unique(tensor1d):
t, idx = np.unique(tensor1d.numpy(), return_inverse=True)
return torch.from_numpy(t), torch.from_numpy(idx)
所以,当你试试吧:
t, idx = unique(torch.LongTensor([1, 1, 2, 4, 4, 4, 7, 8, 8]))
# t --> [1, 2, 4, 7, 8]
# idx --> [0, 0, 1, 2, 2, 2, 3, 4, 4]
我认为这会起作用,但我宁愿避免使用裸体操作,因为它可能需要太多时间。 无论如何,我认为这是目前唯一的解决方案,谢谢。 – arosa
只是以供将来参考 - 有这个功能请求上pytorch的github上,在这里:https://github.com/pytorch/pytorch/issues/2031 – cleros