Why tensor is hashable?

Hi, I am confused that why torch.tensor is hashable, while list is not hashable?

suppose I have a tensor T = torch.randn(5,5). Then I get its hash value via hash(T), say it is 140676925984200, then assign it to another variable, say c. Below is the code.

T = torch.randn(5,5)
c = hash(T)  # i.e. c = 140676925984200
dic = dict()
dic[T] = 100
dic[c] 

The last line caused an error:

RuntimeError: bool value of Tensor with more than one value is ambiguous.

Obviously,

hash(c) == hash(T)
1 Like

What is k? What do you mean by “list is not hashable”?

sorry, it’s a typo. It should be dic[c] instead.
list is unhashable type, which means it cannot be used as a key.

Obvious c is not a key in dic, and thus accessing the dictionary using the key c should raise KeyError instead. But indeed, it raised the following error, which is confusing to me…

RuntimeError: bool value of Tensor with more than one value is ambiguous.

I opened an issue for this here: Inserting a tensor into a python dict causes strange behavior · Issue #7733 · pytorch/pytorch · GitHub . This is strange behavior, thank you for reporting it.