Tensorflow中的fetch與feed
阿新 • • 發佈:2019-02-15
在Tensorflow的使用中,取回訓練結果使用兩種方法:
(1)fetch
可以傳入一些tensor來傳回執行結果,比如下面程式碼中的mul指定傳入方法,intermed作為傳回結果的張量
input1 = tf.constant(3.0)
input2 = tf.constant(2.0)
input3 = tf.constant(5.0)
intermed = tf.add(input2, input3)
mul = tf.mul(input1, intermed)
with tf.Session():
result = sess.run([mul, intermed])
print result
# 輸出:
# [array([ 21.], dtype=float32), array([ 7.], dtype=float32)]
(2)feed
feed機制可以臨時替代圖中的任意操作中的 tensor 可以對圖中任何操作提交補丁, 直接插入一個 tensor.
比如,下面的inout1與input2只是佔位符,在傳入會話時才臨時指定具體的數值:
input1 = tf.placeholder(tf.types.float32)
input2 = tf.placeholder(tf.types.float32)
output = tf.mul(input1, input2)
with tf.Session() as sess:
print sess.run([output], feed_dict={input1:[7.], input2:[2.]})