Tensorflow学习:Fetch and Feed
import tensorflow as tf
# Fetch:在会话里面同时运行多个op
input1 = tf.constant(3.0)
input2 = tf.constant(2.0)
input3 = tf.constant(5.0)
# 定义一个加法op
add = tf.add(input2, input3)
# 定义一个乘法op
mul = tf.multiply(input1, add)
# 定义一个会话,启动默认图,不用手动关闭会话
with tf.Session() as sess:
# 在run里面可以同时运行多个op
result = sess.run([add,mul])
print(result)
# Feed:
# 定义两个占位符:在会话中调用使用
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.multiply(input1, input2)
with tf.Session() as sess:
# feed的数据以字典的形式传入
print(sess.run(output, feed_dict={input1: [7.0], input2: [2.0]}))