Error when trying to import tensorflow_probability twice

Hi!

Every time I run a code more than once with tensorflow_probability imported I get the following error:

import tensorflow_probability
Traceback (most recent call last):

File “”, line 1, in
import tensorflow_probability

File “/home/slevy/Desktop/probability/tensorflow_probability/init.py”, line 23, in
from tensorflow_probability.python import * # pylint: disable=wildcard-import

File “/home/slevy/Desktop/probability/tensorflow_probability/python/init.py”, line 142, in
dir(globals()[pkg_name]) # Forces loading the package from its lazy loader.

File “/home/slevy/Desktop/probability/tensorflow_probability/python/internal/lazy_loader.py”, line 61, in dir
module = self._load()

File “/home/slevy/Desktop/probability/tensorflow_probability/python/internal/lazy_loader.py”, line 44, in _load
module = importlib.import_module(self.name)

File “/home/slevy/anaconda3/lib/python3.7/importlib/init.py”, line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)

File “/home/slevy/Desktop/probability/tensorflow_probability/python/experimental/init.py”, line 35, in
from tensorflow_probability.python.experimental import bijectors

File “/home/slevy/Desktop/probability/tensorflow_probability/python/experimental/bijectors/init.py”, line 19, in
from tensorflow_probability.python.experimental.bijectors.distribution_bijectors import make_distribution_bijector

File “/home/slevy/Desktop/probability/tensorflow_probability/python/experimental/bijectors/distribution_bijectors.py”, line 25, in
from tensorflow_probability.python.distributions import deterministic

File “/home/slevy/Desktop/probability/tensorflow_probability/python/distributions/init.py”, line 23, in
from tensorflow_probability.python.distributions.autoregressive import Autoregressive

File “/home/slevy/Desktop/probability/tensorflow_probability/python/distributions/autoregressive.py”, line 30, in
from tensorflow_probability.python.util.seed_stream import SeedStream

File “/home/slevy/Desktop/probability/tensorflow_probability/python/util/init.py”, line 23, in
from tensorflow_probability.python.util.deferred_tensor import DeferredTensor

File “/home/slevy/Desktop/probability/tensorflow_probability/python/util/deferred_tensor.py”, line 738, in
class _DeferredTensorSpec(_DeferredTensorSpecBase, type_spec.BatchableTypeSpec):

File “/home/slevy/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/type_spec.py”, line 713, in decorator_fn
_NAME_TO_TYPE_SPEC[name].name))

ValueError: Name tfp.util.DeferredTensorSpec has already been registered for class tensorflow_probability.python.util.deferred_tensor._DeferredTensorSpec.

I couldn’t find any discussions on this error anywhere, what can I do to fix it? It’s really annoying to restart the kernel each time to run my code.

Thanks in advance!

2 Likes

Hi, can you provide a bit more context? If you can provide a link to a colab that reproduces this problem we’ll have an easier time helping out.

1 Like

Hi again – I think we found the issue, so this should be fixed in tomorrow’s tfp-nightly. (Please let us know if not!)

1 Like

Ok so this error appears when I import first tensorflow.compat.v1 and than import also tensorflow_probability. It only appear when I run this twice (even if there is not other code involved).
I can’t reproduce this error in colab so I guess this error has to do with some clash in packages on my laptop.

1 Like

Still having the same issue. Should I reinstall tensorflow and tf-nightly?

1 Like

Hi shi_le, thanks for the additional info. It’s important to make sure your tensorflow and TFP versions are in alignment, and that there is only one version of each installed.

There are two release patterns for TF and TFP: nightly and stable, which I think you’ve noticed given your latest comment. You should avoid ever having both tensorflow and tf-nightly installed side by side, and similarly with TFP.

If you want to use tf-nightly, you should also use tfp-nightly. Every once in a while one of these fails to be pushed out on a given day, but usually they should both be there and even if they’re off by one or two days they should be compatible.

If you are using tensorflow==1.15, you will need to install an older TFP version, since we stopped supporting the 1.x line in new stable releases when TF stopped incrementing the stable 1.x versions. The last TFP version that should work with TF 1.15 was tensorflow_probability==0.8 (see the release notes here).

In general, when TFP publishes a new release, we note the compatible TF version in the release notes (see all of them here). Generally speaking we release stables in lock-step with TF. When they push a new stable, we do, too.

So, final advice: uninstall all the TF’s TFP’s and TF nightly’s and TFP nightly’s you have and then just install exactly the ones you want to use. Then things should work! Please let us know if not, or if I can clarify any of the above.

3 Likes

Sorry I just realized that this error comes from the tensorflow_probability package I took from the tensorflow/probability github repository (unless it is the same as TFP).
But I still don’t understand why I get the error when importing this package (after I already imported it one time) without doing anything else. I get the error

ValueError: Name tfp.util.DeferredTensorSpec has already been registered for class tensorflow_probability.python.util.deferred_tensor._DeferredTensorSpec.

or more specifically from this part of type_spec.py in the package :

def register(name):
“”"Decorator used to register a globally unique name for a TypeSpec subclass.

Args:
name: The name of the type spec. Must be globally unique. Must have
the form "{project_name}.{type_name}". E.g. "my_project.MyTypeSpec".

Returns:
A class decorator that registers the decorated class with the given name.
“”"
if not isinstance(name, str):
raise TypeError(“Expected name to be a string; got %r” % (name,))
if not _REGISTERED_NAME_RE.match(name):
raise ValueError(
"Registered name must have the form ‘{project_name}.{type_name}’ "
“(e.g. ‘my_project.MyTypeSpec’); got %r.” % name)

def decorator_fn(cls):
if not (isinstance(cls, type) and issubclass(cls, TypeSpec)):
raise TypeError(“Expected cls to be a TypeSpec; got %r” % (cls,))
if cls in _TYPE_SPEC_TO_NAME:
raise ValueError(“Class %s.%s has already been registered with name %s.”
% (cls.module, cls.name,
_TYPE_SPEC_TO_NAME[cls]))
if name in _NAME_TO_TYPE_SPEC:
raise ValueError(“Name %s has already been registered for class %s.%s.”
% (name, _NAME_TO_TYPE_SPEC[name].module,
_NAME_TO_TYPE_SPEC[name].name))
_TYPE_SPEC_TO_NAME[cls] = name
_NAME_TO_TYPE_SPEC[name] = cls
return cls

return decorator_fn

Any idea why would importing this package twice leads to this error?
Sorry again for the confusion!

1 Like

Hi shi_le, the TFP github repo code should generally be the same as tfp-nightly, modulo changes within the last day. It’s possible you had a few days old snapshot of the TFP repo, since I believe Emily’s recent changes fixed this (real!) bug.

If you can, please try with tfp-nightly from pip, and/or rebuilt .whl from the github repo, synced to the most recent HEAD, and see if this alleviates your issue. Let us know if not!

Thanks for catching this error and discussing it with us, helping us to improve TFP. Sorry for the confusing bug!

1 Like