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

查看:132
本文介绍了如何将值注入到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.)

推荐答案

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

Use placeholder with default in the following way:

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天全站免登陆