(4) fetch 和 feed

1.fetch 就是用sess.run()同时运行多个op

import tensorflow as tf
v1=tf.constant(3)
v2=tf.constant(4)
v3=tf.constant(5)
ad=tf.add(v2,v2)
mul=tf.multiply(v1,ad)
with tf.Session() as sess:
    res=sess.run([mul,ad])
    print(res)
[24, 8]

2.feed 占位符

就是刚开始定义操作时候没有具体的值,在运行操作时候再实时把值传入,传值采用字典的形式。(相当于函数传参)

import tensorflow as tf
#创建32浮点型占位符,即下面的input此时没有具体的值
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:[3.4],input2:[6.0]}))
[20.400002]

发布了65 篇原创文章 · 获赞 3 · 访问量 1682

猜你喜欢

转载自blog.csdn.net/qq_34405401/article/details/104334650
今日推荐