【问题标题】:Getting `FailedPreconditionError: Attempting to use uninitialized value Variable` Error while initializing variables in tensor flow在张量流中初始化变量时出现“FailedPreconditionError:尝试使用未初始化的值变量”错误
【发布时间】:2017-12-26 15:33:29
【问题描述】:

当我尝试在张量流中初始化变量时,我遇到了异常。下面是代码。有人可以帮忙吗?

import tensorflow as tf


node1 = tf.constant(3.0, dtype=tf.float32)
node2 = tf.constant(4.0) # also tf.float32 implicitly


print(node1, node2)
init_g = tf.global_variables_initializer()
init_l = tf.local_variables_initializer()
sess = tf.Session()
sess.run(init_g)
sess.run(init_l)
print(sess.run([node1, node2]))
node3 = tf.add(node1, node2)

print(node3) #prints type in tensorflow
print(sess.run([node3]))


node4 = tf.Variable([.3], dtype=tf.float32)
print(sess.run([node4]))

【问题讨论】:

    标签: python-3.x tensorflow


    【解决方案1】:

    这发生在我初始化全局变量并在之后创建新变量/操作时

    试试这个

        import tensorflow as tf
    
        node1 = tf.constant(3.0, dtype=tf.float32)
        node2 = tf.constant(4.0) # also tf.float32 implicitly
        node3 = tf.add(node1, node2)
        node4 = tf.Variable([.3], dtype=tf.float32)
    
        init = tf.global_variables_initializer()
        sess = tf.Session()
        sess.run(init)
    
        print(node1, node2)
        print(sess.run([node1, node2]))
        print(node3) #prints type in tensorflow
        print(sess.run([node3]))
        print(sess.run([node4]))
    

    想法是首先构建计算,当然包括变量,然后使用sess.run()调用您需要的操作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-10
      • 2011-03-02
      • 2016-06-30
      • 1970-01-01
      • 1970-01-01
      • 2017-12-21
      相关资源
      最近更新 更多