Probability: Tensor is unhashable if Tensor equality is enabled. Instead, use tensor.experimental_ref() as the key

Created on 1 Sep 2019  Â·  27Comments  Â·  Source: tensorflow/probability

https://github.com/tensorflow/tensorflow/issues/32139

Error occurs:
tf-gpu 2.0.0-rc0 with tfp 0.7

Code to reproduce:

import tensorflow_probability as tfp
tfp.distributions.MultivariateNormalDiag([0.], [1.]).sample()

Error returned:

Traceback (most recent call last):
File "/home/pycharm_project/VAE/save_issue_reproduction.py", line 3, in
tfp.distributions.MultivariateNormalDiag([0.], [1.]).sample()
File "/usr/local/lib/python3.5/dist-packages/tensorflow_probability/python/distributions/distribution.py", line 840, in sample
return self._call_sample_n(sample_shape, seed, name, *kwargs)
File "/usr/local/lib/python3.5/dist-packages/tensorflow_probability/python/distributions/transformed_distribution.py", line 391, in _call_sample_n
y = self.bijector.forward(x, *
bijector_kwargs)
File "/usr/local/lib/python3.5/dist-packages/tensorflow_probability/python/bijectors/bijector.py", line 933, in forward
return self._call_forward(x, name, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/tensorflow_probability/python/bijectors/bijector.py", line 904, in _call_forward
mapping = self._lookup(x=x, kwargs=kwargs)
File "/usr/local/lib/python3.5/dist-packages/tensorflow_probability/python/bijectors/bijector.py", line 1343, in _lookup
mapping = self._from_x[x].get(subkey, mapping).merge(x=x)
File "/usr/local/lib/python3.5/dist-packages/tensorflow_probability/python/bijectors/bijector.py", line 151, in __getitem__
return super(WeakKeyDefaultDict, self).__getitem__(weak_key)
File "/usr/local/lib/python3.5/dist-packages/tensorflow_probability/python/bijectors/bijector.py", line 181, in __hash__
return hash(x)
File "/usr/local/lib/python3.5/dist-packages/tensorflow_core/python/framework/ops.py", line 713, in __hash__
raise TypeError("Tensor is unhashable if Tensor equality is enabled. "
TypeError: Tensor is unhashable if Tensor equality is enabled. Instead, use tensor.experimental_ref() as the key.

Most helpful comment

I have updated to TF 2.1 and TFP 0.9 and the error persists. I am trying to use shap

All 27 comments

same thing.
using candidate release1
While running probabilistic layers regression official tutorial.

Can you try with tensorflow-probability==0.8.0rc0 ?

On Thu, Sep 19, 2019, 5:05 AM Alex notifications@github.com wrote:

same thing.
using candidate release1
While running probabilistic layers regression official tutorial.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/tensorflow/probability/issues/540?email_source=notifications&email_token=AFJFSI2PKCGXUNN6RR3F4HDQKM6ERA5CNFSM4ISXNHXKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD7CYSHA#issuecomment-533039388,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AFJFSIZV6U42QI7UZ56YVJTQKM6ERANCNFSM4ISXNHXA
.

@brianwa84 Hi. I can confirm 0.8rc0 works like a dream. Thanks.

@brianwa84 I did install tensorflow-probability==0.8.0rc0 but it did not resolve the issue.

Can you share an exception stack trace?

We have released 0.8.0 alongside TF 2.0.0, and I would be somewhat
surprised if this remains an issue.

On Wed, Oct 2, 2019, 6:38 PM Kleyer Sosa Mazier notifications@github.com
wrote:

@brianwa84 https://github.com/brianwa84 I did install
tensorflow-probability==0.8.0rc0 but it did not resolve the issue.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/tensorflow/probability/issues/540?email_source=notifications&email_token=AFJFSIYFDKKQSPCQDF64Q5LQMUPIBA5CNFSM4ISXNHXKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAGNBUY#issuecomment-537710803,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AFJFSI7YDM2I63TO6WT4OM3QMUPIBANCNFSM4ISXNHXA
.

Same issue here. Doesn't work even after installing "tensorflow_probability==0.8.0rc0"
Code to Reproduce:
new_model = Model (inputs=base_model.input, outputs=final_model)

where, 'base_model' is an old model
'final_model' is new model
while applying Transfer Learning on a Neural Net

Can you share an exception stack trace, and pip freeze (or equivalent
python pkg version dump).
0.8.0 is has a final version (you don't need rc0), and is meant to work
with tensorflow==2.0.0 or tensorflow==1.15.0

Brian Patton | Software Engineer | [email protected]

On Mon, Oct 28, 2019 at 4:55 AM Nabin Adhikari notifications@github.com
wrote:

Same issue here. Doesn't work even after installing
"tensorflow_probability==0.8.0rc0"
Code to Reproduce:
new_model = Model (inputs=base_model.input, outputs=final_model)

where, 'base_model' is an old model
'final_model' is new model
in a Neural Net

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/tensorflow/probability/issues/540?email_source=notifications&email_token=AFJFSI6CK2TEHLBT2FYXNA3QQ2SI3A5CNFSM4ISXNHXKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOECMEROY#issuecomment-546851003,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AFJFSI7MP65XJPSNL6ACTGLQQ2SI3ANCNFSM4ISXNHXA
.

pip install tf-hub-nightly

Same error description with the tutorial Code (using Shap 0.31.0)

from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras.applications.vgg16 import preprocess_input
import tensorflow.keras.backend as K
import numpy as np
import json
import shap

# load pre-trained model and choose two images to explain
model = VGG16(weights='imagenet', include_top=True)
X,y = shap.datasets.imagenet50()
to_explain = X[[39,41]]

# load the ImageNet class names
url = "https://s3.amazonaws.com/deep-learning-models/image-models/imagenet_class_index.json"
fname = shap.datasets.cache(url)
with open(fname) as f:
    class_names = json.load(f)

# explain how the input to the 7th layer of the model explains the top two classes
def map2layer(x, layer):
    feed_dict = dict(zip([model.layers[0].input], [preprocess_input(x.copy())]))
    return K.get_session().run(model.layers[layer].input, feed_dict)
e = shap.GradientExplainer(
    (model.layers[7].input, model.layers[-1].output),
    map2layer(X, 7),
    local_smoothing=0 # std dev of smoothing noise
)
shap_values,indexes = e.shap_values(map2layer(to_explain, 7), ranked_outputs=2)

# get the names for the classes
index_names = np.vectorize(lambda x: class_names[str(x)][1])(indexes)

# plot the explanations
shap.image_plot(shap_values, to_explain, index_names)

print(shap.__version__)
'0.31.0'

I am running something like:
import tensorflow.keras.backend as K

np.sum([K.count_params(p) for p in set(model.trainable_weights)]))

and facing the same issue.

Can you try with tensorflow-probability==0.8.0rc0 ?
…
On Thu, Sep 19, 2019, 5:05 AM Alex @.*> wrote: same thing. using candidate release1 While running probabilistic layers regression official tutorial. — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <#540?email_source=notifications&email_token=AFJFSI2PKCGXUNN6RR3F4HDQKM6ERA5CNFSM4ISXNHXKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD7CYSHA#issuecomment-533039388>, or mute the thread https://github.com/notifications/unsubscribe-auth/AFJFSIZV6U42QI7UZ56YVJTQKM6ERANCNFSM4ISXNHXA .

pip install tensorflow-probability==0.8.0rc0
This seems to work

Also
!pip uninstall -y tf-nightly tensorflow
!pip install tf-nightly-2.0-preview --quiet
!pip install "tensorflow-hub>=0.4"

If you are on Colab first reset all runtimes also.

Same error description with the tutorial Code (using Shap 0.31.0)

from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras.applications.vgg16 import preprocess_input
import tensorflow.keras.backend as K
import numpy as np
import json
import shap

# load pre-trained model and choose two images to explain
model = VGG16(weights='imagenet', include_top=True)
X,y = shap.datasets.imagenet50()
to_explain = X[[39,41]]

# load the ImageNet class names
url = "https://s3.amazonaws.com/deep-learning-models/image-models/imagenet_class_index.json"
fname = shap.datasets.cache(url)
with open(fname) as f:
    class_names = json.load(f)

# explain how the input to the 7th layer of the model explains the top two classes
def map2layer(x, layer):
    feed_dict = dict(zip([model.layers[0].input], [preprocess_input(x.copy())]))
    return K.get_session().run(model.layers[layer].input, feed_dict)
e = shap.GradientExplainer(
    (model.layers[7].input, model.layers[-1].output),
    map2layer(X, 7),
    local_smoothing=0 # std dev of smoothing noise
)
shap_values,indexes = e.shap_values(map2layer(to_explain, 7), ranked_outputs=2)

# get the names for the classes
index_names = np.vectorize(lambda x: class_names[str(x)][1])(indexes)

# plot the explanations
shap.image_plot(shap_values, to_explain, index_names)

print(shap.__version__)
'0.31.0'

@tomalbrecht have you found the solution? I am running into the same issue with shap 0.34.0

@shun-lin Well, kinda - but not within shap. I switched to what-tool. Its included in tensorboard and has way more functionality. https://pair-code.github.io/what-if-tool/

@shun-lin Well, kinda - but not within shap. I switched to what-tool. Its included in tensorboard and has way more functionality. https://pair-code.github.io/what-if-tool/

@tomalbrecht thanks for response, I will look into what-if-tool!

Going to close this as it seems to be a version issue. Latest versions are TF 2.1 and TFP 0.9

I have updated to TF 2.1 and TFP 0.9 and the error persists. I am trying to use shap

facing the same problem ... where is this going wrong ?

Same here ..

  • ERROR: No matching distribution found for tf-nightly-2.0-preview

These install commands
pip install --upgrade tf-nightly tfp-nightly
or
pip install --upgrade tensorflow tensorflow-probability
or
pip install --upgrade tf-nightly-cpu tfp-nightly
or
pip install --upgrade tensorflow-cpu tensorflow-probability
should work, but make sure your pip is up to date or you'll get a ~July version of TF.

Then
pip freeze

import tensorflow as tf, tensorflow_probability as tfp
print(tf.__version__, tfp.__version__)

If pip freeze shows multiple TF or TFP versions, or the versions printed by the python above are not >TF 2.0, TFP 0.8, then the exception here is supported.

I see that shap is still requiring disable_v2_behavior here https://github.com/slundberg/shap/issues/850 . That may also be a cause of this issue.

Same error description with the tutorial Code (using Shap 0.31.0)

from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras.applications.vgg16 import preprocess_input
import tensorflow.keras.backend as K
import numpy as np
import json
import shap

# load pre-trained model and choose two images to explain
model = VGG16(weights='imagenet', include_top=True)
X,y = shap.datasets.imagenet50()
to_explain = X[[39,41]]

# load the ImageNet class names
url = "https://s3.amazonaws.com/deep-learning-models/image-models/imagenet_class_index.json"
fname = shap.datasets.cache(url)
with open(fname) as f:
    class_names = json.load(f)

# explain how the input to the 7th layer of the model explains the top two classes
def map2layer(x, layer):
    feed_dict = dict(zip([model.layers[0].input], [preprocess_input(x.copy())]))
    return K.get_session().run(model.layers[layer].input, feed_dict)
e = shap.GradientExplainer(
    (model.layers[7].input, model.layers[-1].output),
    map2layer(X, 7),
    local_smoothing=0 # std dev of smoothing noise
)
shap_values,indexes = e.shap_values(map2layer(to_explain, 7), ranked_outputs=2)

# get the names for the classes
index_names = np.vectorize(lambda x: class_names[str(x)][1])(indexes)

# plot the explanations
shap.image_plot(shap_values, to_explain, index_names)

print(shap.__version__)
'0.31.0'

@tomalbrecht have you found the solution? I am running into the same issue with shap 0.34.0

create new virtual env and use the below:
!pip install tensorflow==1.14.0
!pip install keras==2.2.5
!pip install shap==0.29.3

!pip install tensorflow==1.14.0
!pip install keras==2.2.5
!pip install shap==0.29.3

Installed packages: 'tensorflow==2.1.0'

'tensorflow-probability==0.8.0'

solves the issue

Installing tensorflow==2.1.0 and tensorflow_probability==0.8.0 did not solve the issue, not sure what to try from here

Could be you have a nightly install of TF or TFP lingering?
What does pip freeze show?

Brian Patton | Software Engineer | [email protected]

On Sun, Nov 29, 2020 at 6:07 PM abihall notifications@github.com wrote:

Installing tensorflow==2.1.0 and tensorflow_probability==0.8.0 did not
solve the issue, not sure what to try from here

—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
https://github.com/tensorflow/probability/issues/540#issuecomment-735472369,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AFJFSI57PPODUHVRTRXFYFLSSLH3ZANCNFSM4ISXNHXA
.

pip_freeze.txt

This is what pip freeze shows. I haven't touched nightly at all and I am not sure if TFP is lingering or how to check if it is

while running shap, I am still getting the error. pip freeze shows below output. The problem seems to be in "dict"

tensorboard==2.1.1
tensorflow-estimator==2.1.0
tensorflow-gpu==2.1.0
tensorflow-probability==0.8.0
termcolor==1.1.0
terminado==0.8.1
testpath==0.3.1
toolz==0.9.0
torch==1.4.0
tornado==5.1.1
tqdm==4.42.0
traitlets==4.3.2
tslearn==0.3.0

I also got the same problem previously because my tensorflow is 2.0

And can solve the problem by adding this code to disable tensorflow v2 behaviour

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
tf.reset_default_graph()

Was this page helpful?
0 / 5 - 0 ratings