【问题标题】:Tensor.graph is meaningless when eager execution is enabled启用 Eager Execution 时,Tensor.graph 毫无意义
【发布时间】:2019-11-12 09:32:25
【问题描述】:
a = tf.compat.v1.constant(5.0)
b = tf.compat.v1.constant(6.0)

sum1 = a + b
g = tf.compat.v1.Graph()
with g.as_default():
    # Define operations and tensors in `g`.
    hello = tf.compat.v1.constant('hello')
    assert hello.graph is g

sess = tf.compat.v1.Session(graph=g)

print(sess.run(sum1))


tensorflow-gpu2.0 我不知道为什么。我是张量流的初学者

【问题讨论】:

  • 您能否详细说明您面临的问题?很暧昧
  • 请阅读一些教程(如 this)以熟悉 TensorFlow。在版本 2.x 中,eager execution 默认启用,因此,正如错误指出的那样,您不能使用图形和会话。您可以禁用 Eager Execution 并返回到 1.x 的处理方式,但如果您刚开始使用 TensorFlow,您可能最好学习 2.x 并使用它。
  • import tensorflow.compat.v1 as tf and tf.disable_v2_behavior() 试试这个并检查一次。

标签: python tensorflow


【解决方案1】:

导入 tensorflow 后需要禁用 Eager Execution,如下所示:

import tensorflow as tf
tf.compat.v1.disable_eager_execution()

它对我有用。

【讨论】:

    【解决方案2】:

    我不知道你想做什么!但是我做了一些猜测,这就是我想出的结果。

    import tensorflow as tf
    
    g = tf.compat.v1.Graph()
    with g.as_default():
        a = tf.compat.v1.constant(5.0)
        b = tf.compat.v1.constant(6.0)
        sum1 = tf.add(a, b) # instead of sum1 = a + b
        
        hello = tf.compat.v1.constant('hello')
        assert hello.graph is g
    
    sess = tf.compat.v1.Session(graph=g)
    
    print(sess.run(sum1))
    

    我相信它适用于 Tensorflow 2.x,如果您仍然遇到急切的异常,那么您应该添加:

    tf.compat.v1.disable_eager_execution()
    

    之前

    assert hello.graph is g
    

    【讨论】:

      【解决方案3】:

      我试过 tensorflow 版本 1.14.0

      执行以下操作

      pip install tensorflow==1.14..0
      

      试试下面的代码

      import tensorflow as tf
      a = tf.compat.v1.constant(5.0)
      b = tf.compat.v1.constant(6.0)
      
      sum1 = a + b
      g = tf.compat.v1.Graph()
      with g.as_default():
      # Define operations and tensors in `g`.
          hello = tf.compat.v1.constant('hello')
          assert hello.graph is g
      sess = tf.compat.v1.Session()
      #sess = tf.compat.v1.Session(graph=g)
      print(sess.run(sum1))
      

      【讨论】:

        猜你喜欢
        • 2020-09-21
        • 2020-02-16
        • 1970-01-01
        • 1970-01-01
        • 2011-09-02
        • 2019-02-13
        • 1970-01-01
        • 2020-02-23
        • 1970-01-01
        相关资源
        最近更新 更多