評價此頁

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)