解决module ‘tensorflow_core._api.v2.config‘ has no attribute ‘experimental_list_devices‘

一、错误提示(AttributeError: module ‘tensorflow_core._api.v2.config’ has no attribute ‘experimental_list_devices’)

AttributeError: module ‘tensorflow_core._api.v2.config’ has no attribute ‘experimental_list_devices’
在这里插入图片描述

二、原因分析

keras建立model报错,网上类似的错误没找到,最终还是比对了下可以运行的这个代码的电脑的文件跟我本机之前下的文件的差别。发现文件内容变了,基本可以确定就是官方内置代码更新了。

怎么解决?很简单,过去的版本可以,那就换回过去的版本好了,没有影响(对应解决方案3.1,如果担心会出问题,大家也可以备份一下自己的文件)。既然是内置代码的问题,是否其他版本的Tensorflow可以解决这个问题呢?答案是:YES(对应解决方案3.2)

三、解决方案(二选一)

3.1 替换配置代码

方案一对应的解决方案
进入Anaconda\Lib\site-packages\keras\backend下的tensorflow_backend里在这里插入图片描述
将以下代码

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 _LOCAL_DEVICES is None:
        if _is_tf_1():
            devices = get_session().list_devices()
            _LOCAL_DEVICES = [x.name for x in devices]
        else:
            _LOCAL_DEVICES = tf.config.experimental_list_devices()
    return [x for x in _LOCAL_DEVICES if 'device:gpu' in x.lower()]

替换为:

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 _LOCAL_DEVICES is None:
        if _is_tf_1():
            devices = get_session().list_devices()
            _LOCAL_DEVICES = [x.name for x in devices]
        else:
            devices = tf.config.list_logical_devices()

            _LOCAL_DEVICES = [x.name for x in devices]
    return [x for x in _LOCAL_DEVICES if 'device:gpu' in x.lower()]

保存文件后,记得清空并重启下程序(如下图jupyter notebook)。如果不行就把编辑器关掉重新打开。
在这里插入图片描述

显示成功在这里插入图片描述

3.2 更换tensorflow版本(治标治本)

我报错版本的Tensorflow版本为2.1.0,事实上2.3也有这个问题。其他版本没有测试过

报错版本版本
Tensorflow(CPU/GPU)2.1.0

经过一系列的测试,解决方案就是更换为Tensorflow2.5,完美解决。另外还有很多问题Tensorflow2.5也给出了解决,太好用了!
具体可见Tensorflow2.5安装与配置

  • 17
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sinysama

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值