{AttributeError:模块'tensorflow_core._api.v2.config'没有属性'experimental_list_devices'}我如何解决此错误?

时间:2020-03-07 19:57:58

标签: python tensorflow keras spyder

Am在Windows 10上使用tensorflow 2.1。跑步

model.add(Conv3D(16, (22, 5, 5), strides=(1, 2, 2), padding='valid',activation='relu',data_format= "channels_first", input_shape=input_shape))

在spyder上出现上述错误。我该如何解决这个错误?

3 个答案:

答案 0 :(得分:11)

我在这里找到了答案-https://github.com/keras-team/keras/issues/13684。 我在Anaconda的keras遇到load_model()的相同问题:

AttributeError:模块'tensorflow_core._api.v2.config'没有属性'experimental_list_devices'

我在

中发现了问题根源

... \ anaconda3 \ envs \ tf_env \ Lib \ site-packages \ keras \ backend \ tensorflow_backend.py

在506行中,我更改了行

_LOCAL_DEVICES = tf.config.experimental_list_devices()

devices = tf.config.list_logical_devices()

_LOCAL_DEVICES = [x.name for x in devices]

它有效

答案 1 :(得分:5)

对于jupyter用户,您可以使用此功能:-

import tensorflow as tf
import keras.backend.tensorflow_backend as tfback

print("tf.__version__ is", tf.__version__)
print("tf.keras.__version__ is:", tf.keras.__version__)

def _get_available_gpus():
    """Get a list of available gpu devices (formatted as strings).

    # Returns
        A list of available GPU devices.
    """
    #global _LOCAL_DEVICES
    if tfback._LOCAL_DEVICES is None:
        devices = tf.config.list_logical_devices()
        tfback._LOCAL_DEVICES = [x.name for x in devices]
    return [x for x in tfback._LOCAL_DEVICES if 'device:gpu' in x.lower()]

tfback._get_available_gpus = _get_available_gpus

答案 2 :(得分:1)

我有类似的问题。 就我而言,我的代码是这样的:

from keras import load_model

看来keras有问题。所以我改用了这个。

from tensorflow.keras.models import load_model

那解决了我的问题,希望对您有所帮助。