评价此页

torch.hash_tensor#

torch.hash_tensor(input, *, mode=0) Tensor#

返回 input 张量中所有元素的哈希值。

目前只支持 mode=0(通过 xor 规约)。输出将始终为 torch.uint64 类型。在通过 xor 进行规约之前,input 的元素将被提升为它们等效的 64 位浮点数/整数,并被位转换为 torch.uint64

参数

input (Tensor) – 输入张量。

关键字参数

mode (int) – 要使用的哈希。默认值:0 (xor_reduction)

示例

>>> a = torch.randn(1, 3)
>>> a
tensor([[ 1.1918, -1.1813,  0.3373]])
>>> torch.hash_tensor(a)
tensor(13822780554648485888, dtype=torch.uint64)
torch.hash_tensor(input, dim, *, keepdim=False, mode=0) Tensor

根据给定的 dim 维度,返回 input 张量每一行的哈希值(由 mode 指定)。如果 dim 是一个维度列表,则在所有这些维度上进行规约。

如果 keepdimTrue,则输出张量的大小与 input 相同,只有在 dim 维度上大小为 1。否则,dim 将被挤压(参见 torch.squeeze()),导致输出张量维度减少 1(或 len(dim))个。

参数
  • input (Tensor) – 输入张量。

  • dim (inttuple of ints, optional) – 要规约的维度或维度。如果为 None,则规约所有维度。

  • keepdim (bool, optional) – 输出张量是否保留 dim。默认为 False

关键字参数

mode (int) – 要使用的哈希。默认值:0 (xor_reduction)

示例

>>> a = torch.randn(2, 4)
>>> a
tensor([[ 0.1317, -0.5554, -1.4724, -1.1391],
        [ 0.0778, -0.6070,  0.6375,  0.1798]])
>>> torch.hash_tensor(a, 1)
tensor([9233691267014066176, 9255993250844508160], dtype=torch.uint64)