如何解决 AttributeError: module 'tensorflow._api.v2.distribute' has no attribute 'TPUStrategy' ?

3

我正在使用Keras和Tensorflow创建一个预测模型。我只有CPU设备,无法执行我的代码。在代码中仅使用Keras和Kerastuner来搜索超参数。以下是错误追踪信息:

File "file.py", line 537, in <Module>
    params,tuner = search_model(X_train,y_train,trials=t,executions=e)
  File "file.py", line 503, in search_model
    verbose = 0
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/base_tuner.py", line 131, in search
    self.run_trial(trial, *fit_args, **fit_kwargs)
  File "file.py", line 476, in run_trial
    super(MyTuner, self).run_trial(trial, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/multi_execution_tuner.py", line 78,
    trial.trial_id, self._reported_step),
  File "/usr/local/lib/python3.6/dist-packages/kerastuner/engine/tuner.py", line 317, in _get_checkp
    if (isinstance(self.distribution_strategy, tf.distribute.TPUStrategy) and
AttributeError: module 'tensorflow._api.v2.distribute' has no attribute 'TPUStrategy'

我尝试了以下方法:

import os
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'

或者:
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

或:
with tf.device("/cpu:0"):

什么都不工作。 这是我的信息设备:

>>> from tensorflow.python.client import device_lib
>>> print(device_lib.list_local_devices())
2021-01-04 20:10:34.173749: I tensorflow/core/platform/cpu_feature_guard.cc:143] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 AVX512F FMA
2021-01-04 20:10:34.217597: I tensorflow/core/platform/profile_utils/cpu_utils.cc:102] CPU Frequency: 2100000000 Hz
2021-01-04 20:10:34.225175: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7f3c8c000b20 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2021-01-04 20:10:34.225243: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
2021-01-04 20:10:34.229506: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory
2021-01-04 20:10:34.229542: E tensorflow/stream_executor/cuda/cuda_driver.cc:313] failed call to cuInit: UNKNOWN ERROR (303)
2021-01-04 20:10:34.229584: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (estio108): /proc/driver/nvidia/version does not exist
[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 5882703391360358162
, name: "/device:XLA_CPU:0"
device_type: "XLA_CPU"
memory_limit: 17179869184
locality {
}
incarnation: 18109344296496597660
physical_device_desc: "device: XLA_CPU device"
]

非常感谢您的帮助。

1
你正在使用哪个版本的 tensorflow - Lukasz Tracewski
使用2.2.0版本。 - Miguel Toquero
谢谢 - 我的建议有效吗? - Lukasz Tracewski
2个回答

1

您需要将Tensorflow更新到2.3或更高版本-这是TPUStrategy添加的地方。以下是提示:

AttributeError: module 'tensorflow._api.v2.distribute' has no attribute 'TPUStrategy'

在这种情况下,无论如何都不重要,因为你将使用CPU。

这个建议对我没有用。我正在使用2.3.1版本,但仍然遇到了同样的错误。 - Darien Schettler
@DarienSchettler 更有可能的是你正在使用一个与你认为的不同的版本。如果确定是正确的版本,那么这就是一个 bug,获取帮助的最佳地点应该是 Github 上的项目页面。 - Lukasz Tracewski

0

我遇到了同样的问题。

错误提示在tuner.py文件的第17行和该行代码。

if (isinstance(self.distribution_strategy, tf.distribute.TPUStrategy) and

如果您在本地计算机上查看源代码,您将看到上述代码行实际上从以下第325行开始。

tuner.py method _get_checkpoint_fname

由于我没有使用TPU,所以我决定注释掉有问题的if语句(即图片中的325-229行),寻找TPUStrategy。

它奏效了!

有用的信息:

  • 我使用TensorFlow 2.2进行测试
  • 到目前为止,我只测试了一个简单的模型(例如Conv2D、Flatten和Dense层),因此可能会出现其他问题。但是,如果您不使用TPU,似乎那个if语句将始终评估为False。但是,如果keras-tuner使用与您使用的TensorFlow版本不兼容的TensorFlow版本的某些部分,则可能会出现不同的错误。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接