tensorflow_core._api.v2.config 中缺少experimental_list_devices 属性

Eda*_*Eda 5 python spyder keras tensorflow

我在 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))

Run Code Online (Sandbox Code Playgroud)

在 spyder 上,我收到此错误:

{ AttributeError: module 'tensorflow_core._api.v2.config' has no attribute 'experimental_list_devices' }
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个错误?

小智 13

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

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()
Run Code Online (Sandbox Code Playgroud)

devices = tf.config.list_logical_devices()

_LOCAL_DEVICES = [x.name for x in devices]
Run Code Online (Sandbox Code Playgroud)

它有效


小智 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
Run Code Online (Sandbox Code Playgroud)