【问题标题】:How to fix: 'RuntimeError: The Session graph is empty. Add operations to the graph before calling run().'如何修复:'RuntimeError:会话图为空。在调用 run() 之前向图中添加操作。
【发布时间】:2021-03-10 15:31:17
【问题描述】:

我正在通过 website 学习 Tensorflow,并输入以下代码:

import tensorflow as tf

x = tf.constant(35, name='x')
y = tf.Variable(x+5, name='y')

model = tf.compat.v1.global_variables_initializer()

with tf.compat.v1.Session() as session :
  session.run(model)
  print(session.run(y))

我收到如下错误:

我该如何解决这个错误?

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    如果你正在学习 tf,为什么不从 tf2 开始,而不是 1.x

    无论如何,这是你的代码

    import tensorflow.compat.v1 as tf
    
    tf.disable_v2_behavior()
    
    x = tf.constant(35, name='x')
    y = tf.constant(5, name='y')
    add = tf.add(x,y)
    
    tf.compat.v1.global_variables_initializer()
    
    with tf.compat.v1.Session() as session :
      result = session.run(add)
      print(result)
    

    【讨论】:

    猜你喜欢
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2017-07-24
    • 1970-01-01
    相关资源
    最近更新 更多