0

i have seen the answer here. It is not what i am looking for.

I am running this on tensorflow2.0

I read the following sentence in the TensorFlow documentation:

With the exception of tf.Variable, the value of a tensor is immutable, which means that in the context of a single execution tensors only have a single value. However, evaluating the same tensor twice can return different values; for example that tensor can be the result of reading data from disk, or generating a random number.

I tried using tensors as a dictionary key and i get the following error:

Tensor is unhashable if Tensor equality is enabled. Instead, use tensor.experimental_ref() as the key
  1. What does this error mean?
  2. Are tf.Variables hashable as well? They also define a computation rather than being the computation, so why the distinction 'With the exception of tf.Variable, the value of a tensor is immutable' is there
figs_and_nuts
  • 4,870
  • 2
  • 31
  • 56

1 Answers1

1

What does this error mean?

I guess you could find answers for the first question in the source code.

Looking at this line and the comment above, you have to explicitly enable hashing for tensors as follows.

tf.compat.v1.disable_tensor_equality()

x = tf.ones(shape=[10,2], dtype=np.float32)
dct = {x: 1} # Works fine

What happens is that now __hash__ returns a proper ID instead of raising an error. Which allows you to use this as a key in a dictionary.

The downside to disabling this is that you can no longer perform element-wise comparisons with the tensor. For example,

x = tf.ones(shape=[10,2], dtype=np.float32)
print((x==1.0))

With Equality enabled returns,

>>> tf.Tensor(
[[ True  True]
 [ True  True]
 ...
 [ True  True]
 [ True  True]], shape=(10, 2), dtype=bool)

With Equality disabled returns,

>>> False

Are tf.Variables hashable as well? They also define a computation rather than being the computation, so why the distinction 'With the exception of tf.Variable, the value of a tensor is immutable' is there

Yes they are. If you look at __hash__ function, it returns id(self) which will be unique for that object during that object's lifetime (source: here). I don't know much about how hash ids are generated using id. But as long as it's unique shouldn't be a problem. And in fact you can change the variable value after it becomes a key in a dictionary too.

thushv89
  • 10,865
  • 1
  • 26
  • 39