zoukankan      html  css  js  c++  java
  • RuntimeError: The Session graph is empty. Add operations to the graph before calling run().解决方法

    首先tf1.的开启回话语句是这样的:

    # 开启会话
        with tf.Session() as sess:
            c_t_value = sess.run(c_t)
            print("c_t_value:
    ", c_t_value)

    现在对于tf2.的用户应该这样做:

    # 开启会话
        tf.compat.v1.disable_eager_execution()
        sess = tf.compat.v1.Session()
        c_t_value = sess.run(c_t)
        print("c_t_value:
    ", c_t_value)

    运行之后报这样的错:

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

    一个小的测试例子:

    import tensorflow as tf
    import os
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
    
    def tensorflow_demo():
        tf.compat.v1.disable_eager_execution()
        #保证sess.run()能够正常运行
        """
        TensorFlow的基本结构
        :return:
        """
        # 原生python加法运算
        a = 2
        b = 3
        c = a + b
        print("普通加法运算的结果:
    ", c)
        # TensorFlow实现加法运算
        a_t = tf.constant(2)
        b_t = tf.constant(3)
        c_t = a_t + b_t
        print("TensorFlow加法运算的结果:
    ", c_t)
        # 开启会话
        tf.compat.v1.disable_eager_execution()
        sess = tf.compat.v1.Session()
        c_t_value = sess.run(c_t)
        print("c_t_value:
    ", c_t_value)
        return None
    
    if __name__ == "__main__":
        # 代码1:TensorFlow的基本结构
        tensorflow_demo()
        

     

  • 相关阅读:
    HDOJ 2689
    UVALive 3635 Pie 切糕大师 二分
    黑马程序员 Java基础<十八>---> 网路编程
    C# 数据库dataGridView刷新数据和主外键判断
    影视-纪录片:《生死洄游》
    汉语-词语:旅行
    汉语-词语:探险
    风水学:龙脉
    人物-探险家:斯文·赫定
    影视-纪录片:《河西走廊之嘉峪关》
  • 原文地址:https://www.cnblogs.com/dazhi151/p/14342364.html
Copyright © 2011-2022 走看看