考虑以下代码:

x = tf.placeholder(tf.float32, (), name='x')
z = x + tf.constant(5.0)
y = tf.mul(z, tf.constant(0.5))

with tf.Session() as sess:
    print(sess.run(y, feed_dict={x: 30}))

结果图为x-> z-> y。有时我对从x开始一直计算y感兴趣,但有时我需要从z开始,并且希望将此值注入(inject)到图形中。因此,z需要表现得像部分占位符。我怎样才能做到这一点?

(对于有兴趣的人为什么我需要这样做。我正在与一个自动编码器网络合作,该网络观察图像x,生成中间压缩表示z,然后计算图像y的重构。我想看看当我注入(inject)不同的图像时网络重构的内容z的值。)

最佳答案

通过以下方式将占位符与默认值一起使用:

x = tf.placeholder(tf.float32, (), name='x')
# z is a placeholder with default value
z = tf.placeholder_with_default(x+tf.constant(5.0), (), name='z')
y = tf.mul(z, tf.constant(0.5))

with tf.Session() as sess:
    # and feed the z in
    print(sess.run(y, feed_dict={z: 5}))

傻我

关于python - 如何将值注入(inject)到TensorFlow图的中间?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41621053/

10-15 23:35