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"
]

非常感谢你的帮助。

4

2 回答 2

1

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

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

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

于 2021-01-05T09:53:44.210 回答
0

我遇到过同样的问题。

错误提到了 tuner.py 中的第 17 行和该行

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

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

tuner.py 方法 _get_checkpoint_fname

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

有效!

有用信息:

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