如何将值注入 TensorFlow 图的中间? [英] How to inject values into the middle of TensorFlow graph?

查看:17
本文介绍了如何将值注入 TensorFlow 图的中间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

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 开始,并希望将此值注入图中.所以 z 需要表现得像一个部分占位符.我该怎么做?

The resulting graph is x -> z -> y. Sometimes I'm interested in computing y all the way from from x but sometimes I have z to start and would like inject this value into the graph. So the z needs to behave like a partial placeholder. How can I do that?

(对于任何对我为什么需要这个感兴趣的人.我正在使用一个自动编码器网络,它观察图像 x,生成一个中间压缩表示 z,然后计算图像 y 的重建.我想看看网络在什么时候重建我为 z 注入了不同的值.)

(For anyone interested why I need this. I am working with an autoencoder network which observes an image x, generates an intermediate compressed representation z, then computes reconstruction of image y. I'd like to see what the network reconstructs when I inject different values for 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}))

骗我.

这篇关于如何将值注入 TensorFlow 图的中间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆