目录
一、错误提示(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安装与配置