placeholder是Tensorflow中的占位符,暂时存储变量。Tensorflow如果想从外部传入data,那就必须要用到tf.placeholder(),然后以这种形式传输数据:sess.run(***,feed_dict={input1:data1,input2:data2})
例子:用Tensorflow实现两个数字相乘。
import tensorflow as tf input1 = tf.placeholder(tf.float32) input2 = tf.placeholder(tf.float32) output = tf.multiply(input1, input2) with tf.Session() as sess: print(sess.run(output, feed_dict={input1:7,input2:2.}))
我们可以发现:传值的工作交给了sess.run(),需要传入的值放在了feed_dict={},并一一对应每一个input。牢记:placeholder与feed_dict={}是绑在一起出现的。
视频笔记:https://morvanzhou.github.io/tutorials/machine-learning/tensorflow/2-5-placeholde/