在TensorFlow中如果构建了一个包含placeholder操作的计算图,在程序执行当在session中调用run方法时,placeholder占用的变量必须通过feed_dict参数传递进去,否则报错。图12提供了一个Feed的样例。
import tensorflow as tf
a = tf.placeholder(tf.float32, name='a')
b = tf.placeholder(tf.float32, name='b')
c = tf.multiply(a, b, name='c')
init = tf.global_variables_initializer()
with tf.Session() as sess:
# sess.run(init)
# 通过feed_dict的参数传值,按字典格式
result = sess.run(c, feed_dict={a:10.0, b:3.5})
print(result)
import tensorflow as tf
a = tf.placeholder(tf.float32, name='a')
b = tf.placeholder(tf.float32, name='b')
c = tf.multiply(a, b, name='c')
d = tf.subtract(a, b, name='d')
with tf.Session() as sess:
#返回的两个值分别赋给两个变量
rc,rd = sess.run([c,d], feed_dict={a:[8.0,2.0,3.5], b:[1.5,2.0,4.]})
print("value of c=",rc,"value of d=",rd)