tensorflow学习笔记——Fetch and Feed

Fetch——会话运行完成之后,如果我们想查看会话运行的结果,就需要使用fetch来实现。一个会话可以同时执行多个op,得到运行结果

例如:

input1 = tf.constant(3)
input2 = tf.constant(2)
input3 = tf.constant(5)

add = tf.add(input2,input3)
mul = tf.multiply(add,input1)

with tf.Session() as sess:
    result = sess.run([mul,add])
    print(result)

这里result就相当于是fetch到的值。

最后结果:

[21, 7]

Feed——为未赋值的变量先创建占位符,之后通过feed给占位符赋值。

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]}))

得到结果:

[14.]
发布了33 篇原创文章 · 获赞 148 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_40692109/article/details/104083608