天天看點

tensorflow學習筆記——傳入值之placeholder,feed_dict用法描述相關公式源代碼

描述

tensorflow中需要傳入值時,要同時用到placeholder、feed_dict。

- placeholder定義輸入值的類型,以及資料結構

- feed_dict,表示python中的字典(python中dist是字典,元素是鍵值對的存儲模式),用于接收真實的輸入值。

相關公式

矩陣乘法中的兩個矩陣,以及輸出結果為:

[102131]×⎡⎣⎢310221⎤⎦⎥=[5193] [ 1 2 3 0 1 1 ] × [ 3 2 1 2 0 1 ] = [ 5 9 1 3 ]

源代碼

#coding:UTF-8
import tensorflow as tf

#輸入
input1 = tf.placeholder(tf.float32,(,))#placeholder(dtype,shape),定義一個3行2列的矩陣
input2 = tf.placeholder(tf.float32,(,))#定義一個2行3列的矩陣

#輸出
output = tf.matmul(input1,input2)#matmul(),矩陣乘法

#執行
with tf.Session() as sess:
    result = sess.run(output,feed_dict = {input1:[[,,],[,,]],input2:[[,],[,],[,]]})
    print result