考虑以下代码:
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 的重建。我想看看网络重建什么时候我为 z 注入(inject)了不同的值。)
最佳答案
按以下方式使用默认占位符:
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/