Python 使用Keras在Google colab上运行3D CNN时出现问题
我正在尝试使用google colab上的keras库来训练一个包含3D Conv层的模型。我遇到了以下错误:Python 使用Keras在Google colab上运行3D CNN时出现问题,python,tensorflow,keras,google-colaboratory,Python,Tensorflow,Keras,Google Colaboratory,我正在尝试使用google colab上的keras库来训练一个包含3D Conv层的模型。我遇到了以下错误: AttributeError Traceback (most recent call last) <ipython-input-5-c6ef25f2bc4a> in <module>() 8 9 model = Sequential() ---> 10 model.add(C
AttributeError Traceback (most recent call last)
<ipython-input-5-c6ef25f2bc4a> in <module>()
8
9 model = Sequential()
---> 10 model.add(Conv3D(16,kernel_size=(3,5,3),padding='same', activation='relu', kernel_initializer='he_normal', input_shape=(20,25,3,1),data_format='channels_first'))
11 model.add(Conv3D(32,kernel_size=(3,3,3),padding='same', activation='relu', kernel_initializer='he_normal',data_format='channels_first'))
12 model.add(Dropout(0.5))
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in _get_available_gpus()
504 _LOCAL_DEVICES = [x.name for x in devices]
505 else:
--> 506 _LOCAL_DEVICES = tf.config.experimental_list_devices()
507 return [x for x in _LOCAL_DEVICES if 'device:gpu' in x.lower()]
508
AttributeError: module 'tensorflow._api.v2.config' has no attribute 'experimental_list_devices'
尝试添加此代码
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
您还应该查看此代码以获得进一步的解决方案:尝试添加此代码
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
您还应该查看以下内容以获得进一步的解决方案:在本地机器和Google collab中检查您的Keras版本。最有可能的情况是,在本地机器和Google collab中检查您的Keras版本是否不匹配。很可能存在某种不匹配