How to inject values into the middle of TensorFlow graph?

北城余情 提交于 2019-12-22 06:44:36

问题


Consider the following code:

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}))

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?

(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.)


回答1:


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}))

Silly me.




回答2:


I'm not allowed to comment on your post, @iramusa, so I'll give an answer. You don't need to use a placeholder_with_default. You can just feed in the values to whatever node you want:

import tensorflow as tf

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

with tf.Session() as sess:
    print(sess.run(y, feed_dict={x: 2}))  # get 3.5
    print(sess.run(y, feed_dict={z: 5}))  # get 2.5
    print(sess.run(y, feed_dict={y: 5}))  # get 5


来源:https://stackoverflow.com/questions/41621053/how-to-inject-values-into-the-middle-of-tensorflow-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!