How to inject values into the middle of TensorFlow graph?

扶醉桌前 提交于 2019-12-05 10:43:47

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.

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