1.前言
這次我們講 Tensorflow 中的 placeholder , placeholder 是 Tensorflow 中的佔位符,暫時儲存變量.
2.矩陣計算
Tensorflow 如果想要從外部傳入data, 那就需要用到 tf.placeholder(), 然後以這種形式傳輸數據 sess.run(***, feed_dict={input: **}).
import tensorflow as tf
input1 = tf.placeholder(tf.float32) #佔位符,暫時儲存變量
input2 = tf.placeholder(tf.float32)
output_add = tf.add(input1,input2) #加法
output_multi = tf.multiply(input1,input2) #乘法
接下來, 傳值的工作交給了 sess.run() , 需要傳入的值放在了feed_dict={} 並一一對應每一個 input. placeholder 與 feed_dict={} 是綁定在一起出現的。
with tf.Session() as sess:
print('input1+input2:',sess.run(output_add, feed_dict={input1:[10],input2:[4]})) #在sess.run()中傳值
print('input1*input2:',sess.run(output_multi, feed_dict={input1:[10],input2:[4]}))