[機器學習]RuntimeError: The Session graph is empty. Add operations to the graph before calling run(). 報錯解決方法


在進行機器學習的過程中

 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 
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))

會報如下錯誤

  raise RuntimeError('The Session graph is empty. Add operations to the '
RuntimeError: The Session graph is empty. Add operations to the graph before calling run().

  • 問題產生的原因:無法執行sess.run()的原因是tensorflow版本不同導致的,tensorflow版本2.0無法兼容版本1.0.
  • 解決辦法:
  •  tf.compat.v1.disable_eager_execution() 

修改后的代碼

 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))

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM