AttributeError: module ‘tensorflow‘ has no attribute ‘GPUOptions‘

AttributeError: module ‘tensorflow’ has no attribute ‘GPUOptions’

报错原因

Tensorflow 1.X和 2.X不兼容。

Tensorflow 1.X:

gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction)

Tensorflow 2.X:

gpu_options =tf.compat.v1.GPUOptions(per_process_gpu_memory_fraction)

附Tensorflow 1.X和 2.X区别:

Tensorflow 1.X:

gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333)
sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)

Tensorflow 2.X:

gpu_options = tf.compat.v1.GPUOptions(per_process_gpu_memory_fraction=0.333)
sess = tf.compat.v1.Session(config=tf.ConfigProto(gpu_options=gpu_options))
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.compat.v1.Session(config=config)

Tensorflow 1.X:

from keras.optimizers import Adam
from keras import backend as K

Tensorflow 2.X:

from tensorflow.python.keras.optimizers import Adam
from tensorflow.python.keras import backend as K

你可能感兴趣的:(Python,tensorflow,keras,深度学习)