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

在代码:

   config=tf.ConfigProto()

   sess=tf.compat.v1.Session(config=config)

执行过程中会报错

  config=tf.ConfigProto()

AttributeError: module 'tensorflow' has no attribute 'ConfigProto'

问题原因:因为是tensorflow 2.0版本与1.0的用法不兼容

  解决办法:

修改为 config=tf.compat.v1.ConfigProto() 和 sess=tf.compat.v1.Session(config=config)

修改后代码:

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 # @File  : TensorFlow入门操作.py
 4 # @Author: 赵路仓
 5 # @Date  : 2020/3/26
 6 # @Desc  :
 7 # @Contact : 398333404@qq.com 
 8 
 9 import tensorflow as tf
10 import os
11 os.environ["CUDA_VISIBLE_DEVICES"]="0"
12 
13 tf.compat.v1.disable_eager_execution()
14 hello=tf.constant('Hello,TensorFlow')
15 config=tf.compat.v1.ConfigProto()
16 config.gpu_options.per_process_gpu_memory_fraction = 0.9
17 sess=tf.compat.v1.Session(config=config)
18 print(sess.run(hello))