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)

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: https://github.com/pytorch/pytorch/issues/7733 . This is strange behavior, thank you for reporting it.