知乎TensorFlow入門學習記錄
阿新 • • 發佈:2018-06-29
nbsp 網絡 lan ID span tar orf 形參 sta
知乎地址:https://zhuanlan.zhihu.com/p/30487008
import tensorflow as tf a=tf.placeholder(tf.int16) # 接受的數據類型 b=tf.placeholder(tf.int16) add=tf.add(a,b) mul=tf.mul(a,b) with tf.Session() as sess: print("Addition with variables:%i" %sess.run(add,feed_dict={a:2,b:3})) #餵數據圖譜,這裏的數據類相要符合上面的類型。把2賦給a,3賦給bprint("multiplication with variables:%i"%sess.run(mul,feed_dict={a:2,b:3}))
placeholder是TensorFlow的占位符節點,由placeholder方法創建,其也是一種常量,但是由用戶在調用run方法是傳遞的,也可以將placeholder理解為一種形參。即其不像constant那樣直接可以使用,需要用戶傳遞常數值。
在tensorflow中的placeholder 定義如下
tf.placeholder(dtype, shape=None, name=None)
簡單理解下就是占位符的意思,先放在這裏,然後在需要的時候給網絡傳輸數據
直接傳遞給run()回報錯哦,必須通過 feed_dict方法 傳遞給 Session.run()
, Tensor.eval()
,或者Operation.run()
import tensorflow as tf m1=tf.constant([[3.,3.]]) m2=tf.constant([[2.],[2.]]) product=tf.multiply(matrix1,matrix2) #矩陣乘法 with tf.Session() as sess: result=sess.run(product) print(result)
知乎TensorFlow入門學習記錄