Placeholder

解释

A placeholder is simply a variable that we will assign data to at a later date. It allows us to create our operations and build our computation graph, without needing the data.

顾名思义,占位符,没有数据先占位。是一种变量。之后按照sess.run(***, feed_dict={input: **})形式传输数据。

1
2
3
4
5
6
7
import tensorflow as tf

with tf.Graph().as_default(), tf.Session() as sess:
x = tf.placeholder(tf.float32)
y = x**3
feed_dict = {x:[1, 2, 3, 4]}
print(sess.run(y, feed_dict=feed_dict))
[ 1.  8. 27. 64.]

总结

placeholderfeed_dict={} 总是一起出现的。placeholder允许没有数据先占位创建计算图,运行时通过feed_dict传入数据。