在张量流中初始化变量时出现`FailedPreconditionError:尝试使用未初始化的值变量`错误

问题描述 投票:0回答:1

当我试图在张量流中初始化变量时,我正在超越异常。下面是代码。有人可以帮忙吗?

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个回答
2
投票

当我初始化全局变量并在之后创建新的变量/操作时,会发生这种情况

试试这个

    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()调用所需的操作

© www.soinside.com 2019 - 2024. All rights reserved.