Python 如何解决属性错误:模块';tensorflow._api.v2.分发';没有属性';t插图';

Python 如何解决属性错误:模块';tensorflow._api.v2.分发';没有属性';t插图';,python,tensorflow,runtime-error,cpu,keras-tuner,Python,Tensorflow,Runtime Error,Cpu,Keras Tuner,我正在与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

我正在与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'
或:

或:

什么都不管用。 这是我的信息设备:

>>> 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版或更高版本-这就是添加的地方。以下是你的线索:

AttributeError: module 'tensorflow._api.v2.distribute' has no attribute 'TPUStrategy'
在这种情况下,这几乎无关紧要,因为您将使用CPU。

我也有同样的问题

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

if (isinstance(self.distribution_strategy, tf.distribute.TPUStrategy) and
如果您在机器上本地查看源代码,您将看到上面的代码行实际上从第325行开始,如下所示

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

成功了

有用信息:

  • 我用TensorFlow 2.2进行了测试
  • 到目前为止,我只测试了一个简单的模型(例如Conv2D、flatte和稠密层),因此可能会出现其他问题。然而,若您并没有使用TPU,那个么不管怎样,if语句的计算结果似乎都是
    False
    。但如果keras tuner使用的TensorFlow版本的某些部分与您使用的TensorFlow版本不兼容,则可能会出现不同的错误

您使用的是什么版本的
tensorflow
?使用2.2.0版本的Tanks-我的建议有效吗?此建议对我无效。使用版本2.3.1时,出现了相同的错误。@DarienSchettler很可能您使用的版本与您想象的不同。如果确定它是正确的,那么它就是一个bug,获得帮助的最佳位置是Github上的项目页面。
>>> 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"
]
AttributeError: module 'tensorflow._api.v2.distribute' has no attribute 'TPUStrategy'
if (isinstance(self.distribution_strategy, tf.distribute.TPUStrategy) and