问题描述
我一直在尝试在我的计算机上运行 this TensorFlow tutorial,但是在运行以下代码时,我收到了标题中的错误:
import os
import shutil
import tensorflow as tf
import tensorflow_hub as hub
import tensorflow_text as text
from official.nlp import optimization # to create AdamW optmizer
import matplotlib.pyplot as plt
tf.get_logger().setLevel('ERROR')
os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true'
这是整个错误回溯。在教程的协作笔记本中运行它时,似乎没有任何问题。
AttributeErrorTraceback (most recent call last)
<ipython-input-7-3afaa91eeb1f> in <module>
3
4 import tensorflow as tf
----> 5 import tensorflow_hub as hub
6 import tensorflow_text as text
7 from official.nlp import optimization # to create AdamW optmizer
/usr/local/lib/python3.6/dist-packages/tensorflow_hub/__init__.py in <module>
86
87
---> 88 from tensorflow_hub.estimator import LatestModuleExporter
89 from tensorflow_hub.estimator import register_module_for_export
90 from tensorflow_hub.feature_column import image_embedding_column
/usr/local/lib/python3.6/dist-packages/tensorflow_hub/estimator.py in <module>
60
61
---> 62 class LatestModuleExporter(tf.compat.v1.estimator.Exporter):
63 """Regularly exports registered modules into timestamped directories.
64
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/lazy_loader.py in __getattr__(self,item)
60
61 def __getattr__(self,item):
---> 62 module = self._load()
63 return getattr(module,item)
64
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/lazy_loader.py in _load(self)
43 """Load the module and insert it into the parent's globals."""
44 # Import the target module and insert it into the parent's namespace
---> 45 module = importlib.import_module(self.__name__)
46 self._parent_module_globals[self._local_name] = module
47
/usr/lib/python3.6/importlib/__init__.py in import_module(name,package)
124 break
125 level += 1
--> 126 return _bootstrap._gcd_import(name[level:],package,level)
127
128
/usr/local/lib/python3.6/dist-packages/tensorflow_estimator/python/estimator/api/_v1/estimator/__init__.py in <module>
8 import sys as _sys
9
---> 10 from tensorflow_estimator.python.estimator.api._v1.estimator import experimental
11 from tensorflow_estimator.python.estimator.api._v1.estimator import export
12 from tensorflow_estimator.python.estimator.api._v1.estimator import inputs
/usr/local/lib/python3.6/dist-packages/tensorflow_estimator/python/estimator/api/_v1/estimator/experimental/__init__.py in <module>
8 import sys as _sys
9
---> 10 from tensorflow_estimator.python.estimator.canned.dnn import dnn_logit_fn_builder
11 from tensorflow_estimator.python.estimator.canned.kmeans import KMeansClustering as KMeans
12 from tensorflow_estimator.python.estimator.canned.linear import LinearSDCA
/usr/local/lib/python3.6/dist-packages/tensorflow_estimator/python/estimator/canned/dnn.py in <module>
29 from tensorflow.python.keras.utils import losses_utils
30 from tensorflow.python.util.tf_export import estimator_export
---> 31 from tensorflow_estimator.python.estimator import estimator
32 from tensorflow_estimator.python.estimator.canned import head as head_lib
33 from tensorflow_estimator.python.estimator.canned import optimizers
/usr/local/lib/python3.6/dist-packages/tensorflow_estimator/python/estimator/estimator.py in <module>
50 from tensorflow.python.util.tf_export import estimator_export
51 from tensorflow_estimator.python.estimator import model_fn as model_fn_lib
---> 52 from tensorflow_estimator.python.estimator import run_config
53 from tensorflow_estimator.python.estimator import util as estimator_util
54 from tensorflow_estimator.python.estimator.export import export_lib
/usr/local/lib/python3.6/dist-packages/tensorflow_estimator/python/estimator/run_config.py in <module>
28 from tensorflow.core.protobuf import rewriter_config_pb2
29 from tensorflow.python.distribute import estimator_training as distribute_coordinator_training
---> 30 from tensorflow.python.distribute import parameter_server_strategy_v2
31 from tensorflow.python.util import compat_internal
32 from tensorflow.python.util import function_utils
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/parameter_server_strategy_v2.py in <module>
26
27 from tensorflow.python.distribute import distribute_lib
---> 28 from tensorflow.python.distribute import distribute_utils
29 from tensorflow.python.distribute import parameter_server_strategy
30 from tensorflow.python.distribute import sharded_variable
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_utils.py in <module>
372 # (synchronization=ON_READ,aggregation=NONE,SUM,MEAN,ONLY_FirsT_REPLICA)
373 VARIABLE_POLICY_MAPPING = {
--> 374 vs.VariableSynchronization.AUTO: values_lib.Autopolicy,375 vs.VariableSynchronization.ON_WRITE: values_lib.OnWritePolicy,376 vs.VariableSynchronization.ON_READ: values_lib.OnReadPolicy,AttributeError: module 'tensorflow.python.distribute.values' has no attribute 'Autopolicy'
为什么会发生这种情况有什么特别的原因吗?
提前致谢。
解决方法
我猜这可能与您用于 tensorflow
和 tensorflow_hub
的版本之间的版本不匹配有关。
看起来 AutoPolicy 已从 a recent commit 中的 TF 中删除,因此如果您从源代码构建 tensorflow 并包含此提交,那么这可能就是问题所在。如果您想从源代码构建 TF,请在构建前查看最新的官方版本 (2.4.1)。