Tensorflow—Fetch and Feed

代码:

import tensorflow as tf 


#Fetch 可以在会话中运行多个op

#创建3个常量op
input1 = tf.constant(3.0)
input2 = tf.constant(2.0)
input3 = tf.constant(5.0)
#创建一个加法op和一个减法op
add = tf.add(input2, input3)
mul = tf.multiply(input1, add)


#定义会话
with tf.Session() as sess:
    #运行多个op
    result = sess.run([mul, add])
    print(result)

运行结果:

[21.0, 7.0]

代码:

#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:[8.], input2:[2.]}))

运行结果:

[ 16.]

你可能感兴趣的:(Tensorflow—Fetch and Feed)