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

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

我正在使用 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"
]

非常感谢您的帮助。

您必须将 Tensorflow 更新到版本 2.3 或更高版本 - 这是添加 TPUStrategy 的地方。这是你的线索:

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

在这种情况下,这无关紧要,因为您将使用 CPU。

我遇到了同样的问题。

错误提到第 17 行 tuner.py 和第

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

如果您在本地机器上查看源代码,您会看到上面的代码行实际上是从第 325 行开始的,如下所示。

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

成功了!

有用信息:

  • 我用 TensorFlow 2.2 测试过
  • 到目前为止,我只测试了一个简单的模型(例如 Conv2D、Flatten 和 Dense 层),因此可能会出现其他问题。但是,如果您不使用 TPU,那么 if 语句似乎无论如何都会计算为 False。但是,如果 keras-tuner 使用的 TensorFlow 版本的某些部分与您起诉的 TensorFlow 版本不兼容,您可能会遇到不同的错误。