關于tf.matmul() 和tf.multiply() 的區(qū)別說明
我就廢話不多說了,大家還是直接看代碼吧~
flyfish
# a # [[1, 2, 3], # [4, 5, 6]] a = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3]) # b1 # [[ 7, 8], # [ 9, 10], # [11, 12]] b1 = tf.constant([7, 8, 9, 10, 11, 12], shape=[3, 2]) #b2 #[[ 7 8 9] # [10 11 12]] b2 = tf.constant([7, 8, 9, 10, 11, 12], shape=[2, 3]) # c矩陣相乘 第一個矩陣的列數(column)等于第二個矩陣的行數(row) # [[ 58, 64], # [139, 154]] c = tf.matmul(a, b1) # d`數元素各自相乘 #[[ 7 16 27] # [40 55 72]] d = tf.multiply(a, b2) #維度必須相等 with tf.Session(): print(d.eval())
關于其他計算
b3 = tf.constant([7, 8, 9,], shape=[1, 3]) tf.multiply(a, b3) 結果是 [[ 7 16 27] [28 40 54]] b4 = tf.constant([7, 8], shape=[2, 1]) tf.multiply(a, b4) 結果是 [[ 7 14 21] [32 40 48]] b5 = tf.constant([7], shape=[1, 1]) tf.multiply(a, b5) 結果是 [[ 7 14 21] [28 35 42]]
補充知識:tensor matmul的對3維張量的處理
torch.matmul(a,b)處理的一般是a和b的最后兩個維度,假設a的維度為B*F*M,b也為B*F*M, 在對a,b做相乘操作的時候,需要完成對B的維度順序的變換,通過permute(0, 2, 1)變換為B*M*F。
通過變換后進行torch.matmul(a,b)得到結果為B*F*F,在除了最后兩個維度的的之前維度上都被認為是Batch。
示例1:
>>> import torch >>> a=torch.rand((1000,5,10)) >>> b=torch.rand((1000,10,12)) >>> c=torch.matmul(a,b) >>> c.shape torch.Size([1000, 5, 12])
在處理不同維度時,會通過廣播來合并除最后兩個維度外的其他維度,如對于A*B*F*M與B*M*F的matmul,結果為A*B*F*F
示例2:
>>> a=torch.rand((50,1000,5,10)) >>> b=torch.rand((1000,10,12)) >>> c=torch.matmul(a,b) >>> c.shape torch.Size([50, 1000, 5, 12])
以上這篇關于tf.matmul() 和tf.multiply() 的區(qū)別說明就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
python計算機視覺OpenCV庫實現實時攝像頭人臉檢測示例
這篇文章主要為大家介紹了python使用OpenCV實現實時攝像頭人臉檢測的示例過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2021-10-10
Python+Selenium+PIL+Tesseract自動識別驗證碼進行一鍵登錄
本篇文章主要介紹了Python+Selenium+PIL+Tesseract自動識別驗證碼進行一鍵登錄,具有一定的參考價值,有興趣的可以了解下2017-09-09

