AttributeError: module ‘tensorflow._api.v1.compat‘ has no attribute ‘v1‘

AttributeError: module ‘tensorflow._api.v1.compat’ has no attribute ‘v1’

  • 猜测原因:TensorFlow不同版本的API有出入,用法不兼容
  • 解决:修改API即可。

将代码
config = tf.compat.v1.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.8
sess = tf.compat.v1.session(config=config)
 
 
改为
gpu_options=tf.GPUOptions(per_process_gpu_memory_fraction=0.8)
config=tf.ConfigProto(gpu_options=gpu_options)
sess = tf.Session(config=config)

 
 
补充:
1.注意有三个地方的API都不同:①config;②gpu_options;③session
(其中,第二个如果不改,会报错:AttributeError: 'google.protobuf.pyext._message.FieldProperty' object has no attribute 'per_process_gpu_memory_fraction'

2.过程描述:一开始我直接在百度粘贴报错信息,但暂时没找到解决办法。后来直接搜索”tf.ConfigProto“查看其用法,对比不同版本tensorflow的API用法,才发现解决方法。

3.另外,有的文章可能说将import tensorflow as tf改为import tensorflow.compat.v1 as tf

但是我试了,会报错:ModuleNotFoundError: No module named 'tensorflow.compat.v1'
 
 

参考文章汇总

AttributeError: ‘google.protobuf.pyext._message.RepeatedScalarConta’ object has no attribute '_value

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

AttributeError: ‘google.protobuf.pyext._message.RepeatedScalarConta’ object has no attribute '_value

解决AttributeError: module ‘tensorflow’ has no attribute ‘ConfigProto’

[机器学习]AttributeError: module ‘tensorflow’ has no attribute ‘ConfigProto’ 报错解决方法

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

Tensorflow中tf.ConfigProto()详解

tensorflow学习笔记(二十五):ConfigProto&GPU

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